Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): fix a bug in CLI argument parsing #5646

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/cli/parse-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => {

/**
* Normalize a 'negative' flag name, just to do a little pre-processing before
* we pass it to `setCLIArg`.
* we pass it to {@link setCLIArg}.
*
* @param flagName the flag name to normalize
* @returns a normalized flag name
Expand Down Expand Up @@ -221,12 +221,19 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va

// We're setting a boolean!
if (readOnlyArrayHasStringMember(BOOLEAN_CLI_FLAGS, normalizedArg)) {
flags[normalizedArg] =
const parsed =
typeof value === 'string'
? Boolean(value)
? // check if the value is `'true'`
value === 'true'
: // no value was supplied, default to true
true;

flags[normalizedArg] = parsed;
flags.knownArgs.push(rawArg);

if (typeof value === 'string' && value !== '') {
flags.knownArgs.push(value);
}
}

// We're setting a string!
Expand Down
6 changes: 6 additions & 0 deletions src/cli/test/parse-flags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe('parseFlags', () => {
expect(flags.knownArgs).toEqual([]);
expect(flags[cliArg]).toBe(undefined);
});

it.each([true, false])(`should set the value with --${cliArg}=%p`, (value) => {
const flags = parseFlags([`--${cliArg}=${value}`]);
expect(flags.knownArgs).toEqual([`--${cliArg}`, String(value)]);
expect(flags[cliArg]).toBe(value);
});
});

describe.each(STRING_CLI_FLAGS)('should parse string flag %s', (cliArg) => {
Expand Down