From ed8cee76d3a669c496061c5157aa7397af39a192 Mon Sep 17 00:00:00 2001 From: Thorarinn Sigurdsson Date: Thu, 16 Sep 2021 14:12:16 +0200 Subject: [PATCH] fix(core): fix handling of empty option values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To ensure that `command.params` behaves intuitively in template strings, we don't add option keys with `null`/`undefined` values to the command info object (which gets passed to `command.params` in template strings). For example, we don't want `${command.params contains 'dev-mode'}` to be `true` when running `garden deploy` unless the `--dev` flag was actually passed (since the user would expect the option value to be an array if present). --- core/src/cli/helpers.ts | 12 ++++++++++-- core/test/unit/src/cli/cli.ts | 8 -------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/cli/helpers.ts b/core/src/cli/helpers.ts index a94e140448..d7134a5eed 100644 --- a/core/src/cli/helpers.ts +++ b/core/src/cli/helpers.ts @@ -289,9 +289,17 @@ export function processCliArgs({ throw new ParameterError(chalk.red.bold(errors.join("\n")), { parsedArgs, processedArgs, processedOpts, errors }) } + // To ensure that `command.params` behaves intuitively in template strings, we don't want to add option keys with + // null/undefined values. + // + // For example, we don't want `${command.params contains 'dev-mode'}` to be `true` when running `garden deploy` + // unless the `--dev` flag was actually passed (since the user would expect the option value to be an array if + // present). + const cleanedProcessedOpts = pickBy(processedOpts, (value) => !(value === undefined || value === null)) + return { args: >processedArgs, - opts: & ParameterValues>processedOpts, + opts: & ParameterValues>cleanedProcessedOpts, } } @@ -301,7 +309,7 @@ export function optionsWithAliasValues { opts: { "root": process.cwd(), "silent": false, - "env": undefined, - "logger-type": undefined, "log-level": "info", - "output": undefined, "emoji": envSupportsEmoji(), "show-timestamps": false, "yes": false, "force-refresh": false, - "var": undefined, "version": false, "help": false, "floop": "floop-opt", @@ -762,15 +758,11 @@ describe("cli", () => { opts: { "root": process.cwd(), "silent": false, - "env": undefined, - "logger-type": undefined, "log-level": "info", - "output": undefined, "emoji": envSupportsEmoji(), "show-timestamps": false, "yes": false, "force-refresh": false, - "var": undefined, "version": false, "help": false, "floop": "floop-opt",