Skip to content

Commit

Permalink
Merge pull request #730 from oclif/sm/omit-undefined-flags-from-parse
Browse files Browse the repository at this point in the history
fix: flags omit undefined for boolean flags
  • Loading branch information
WillieRuemmele authored Jul 12, 2023
2 parents 9f232c2 + 7277848 commit 1711ecd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 0 additions & 6 deletions perf.txt

This file was deleted.

5 changes: 4 additions & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
return Promise.all(fws.map(async fws => fws.helpFunction ? ({...fws, metadata: {...fws.metadata, defaultHelp: await fws.helpFunction?.(fws, valueReferenceForHelp, this.context)}}) : fws))
}

const fwsArrayToObject = (fwsArray: FlagWithStrategy[]) => Object.fromEntries(fwsArray.map(fws => [fws.inputFlag.name, fws.value]))
const fwsArrayToObject = (fwsArray: FlagWithStrategy[]) => Object.fromEntries(
fwsArray.filter(fws => fws.value !== undefined)
.map(fws => [fws.inputFlag.name, fws.value]),
)

type FlagWithStrategy = {
inputFlag: {
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 1711ecd

Please sign in to comment.