Skip to content

Commit

Permalink
fix: allow empty flag default
Browse files Browse the repository at this point in the history
* fix: allow empty flag default

* chore: more falsy tests
  • Loading branch information
iowillhoit committed Jul 13, 2023
1 parent 085b2e4 commit 6338f3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,9 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
}

const flagTokenMap = this.mapAndValidateFlags()

const flagsWithValues = await Promise.all(Object.entries(this.input.flags)
// we check them if they have a token, or might have env, default, or defaultHelp. Also include booleans so they get their default value
.filter(([name, flag]) => flag.type === 'boolean' || flag.env || flag.default || 'defaultHelp' in flag || flagTokenMap.has(name))
.filter(([name, flag]) => flag.type === 'boolean' || flag.env || flag.default !== undefined || 'defaultHelp' in flag || flagTokenMap.has(name))
// match each possible flag to its token, if there is one
.map(([name, flag]): FlagWithStrategy => ({inputFlag: {name, flag}, tokens: flagTokenMap.get(name)}))
.map(fws => addValueFunction(fws))
Expand Down
19 changes: 19 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,25 @@ See more help with --help`)
expect(out.args).to.deep.include({baz: false})
})

it('accepts falsy flags', async () => {
const out = await parse([], {
flags: {
foo1: Flags.string({default: ''}),
foo2: Flags.string({default: '0'}),
foo3: Flags.string({default: 'false'}),
foo4: Flags.string({default: 'undefined'}),
bar: Flags.integer({default: 0}),
baz: Flags.boolean({default: false}),
},
})
expect(out.flags).to.deep.include({foo1: ''})
expect(out.flags).to.deep.include({foo2: '0'})
expect(out.flags).to.deep.include({foo3: 'false'})
expect(out.flags).to.deep.include({foo4: 'undefined'})
expect(out.flags).to.deep.include({bar: 0})
expect(out.flags).to.deep.include({baz: false})
})

it('default as function', async () => {
const out = await parse([], {
args: {baz: Args.string({default: async () => 'BAZ'})},
Expand Down

0 comments on commit 6338f3d

Please sign in to comment.