Skip to content

Commit

Permalink
fix(core): fix handling of empty option values
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
thsig authored and edvald committed Sep 21, 2021
1 parent b9f9b4e commit ed8cee7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 10 additions & 2 deletions core/src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,17 @@ export function processCliArgs<A extends Parameters, O extends Parameters>({
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: <DefaultArgs & ParameterValues<A>>processedArgs,
opts: <ParameterValues<GlobalOptions> & ParameterValues<O>>processedOpts,
opts: <ParameterValues<GlobalOptions> & ParameterValues<O>>cleanedProcessedOpts,
}
}

Expand All @@ -301,7 +309,7 @@ export function optionsWithAliasValues<A extends Parameters, O extends Parameter
): DeepPrimitiveMap {
const withAliases = { ...parsedOpts } // Create a new object instead of mutating.
for (const [name, spec] of Object.entries(command.options || {})) {
if (spec.alias) {
if (spec.alias && parsedOpts[name]) {
withAliases[spec.alias] = parsedOpts[name]
}
}
Expand Down
8 changes: 0 additions & 8 deletions core/test/unit/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ed8cee7

Please sign in to comment.