Skip to content

Commit

Permalink
fix: flags omit undefined for boolean flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jul 12, 2023
1 parent 9f232c2 commit 0a7e154
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']

const valueReference = fwsArrayToObject(flagsWithValues.filter(fws => !fws.metadata?.setFromDefault))

const flagsWithAllValues = await Promise.all(flagsWithValues
const flagsWithAllValues = (await Promise.all(flagsWithValues
.map(async fws => (fws.metadata?.setFromDefault ? {...fws, value: await fws.valueFunction?.(fws, valueReference)} : fws)))
).filter(fws => fws.value !== undefined)

const finalFlags = (flagsWithAllValues.some(fws => typeof fws.helpFunction === 'function')) ? await addDefaultHelp(flagsWithAllValues) : flagsWithAllValues

Expand Down
23 changes: 23 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ describe('parse', () => {
expect(out).to.deep.include({flags: {bool: true}})
})

describe('undefined flags', () => {
it('omits undefined flags when no flags', async () => {
const out = await parse([], {
flags: {
bool: Flags.boolean(),
},
})
expect(out.flags).to.deep.equal({})
})

it('omits undefined flags when some flags exist', async () => {
const out = await parse(['--bool', '--str', 'k'], {
flags: {
bool: Flags.boolean(),
bool2: Flags.boolean(),
str: Flags.string(),
str2: Flags.string(),
},
})
expect(out.flags).to.deep.equal({bool: true, str: 'k'})
})
})

it('arg1', async () => {
const out = await parse(['arg1'], {
args: {foo: Args.string()},
Expand Down

0 comments on commit 0a7e154

Please sign in to comment.