Skip to content

Commit

Permalink
fix(parser): don't throw if defaultHelp func throws (#732)
Browse files Browse the repository at this point in the history
* test(parser): add flag with invalid default help test case

* fix(parser): don't throw if defaultHelp func throws

* refactor: param name (no shadow)

---------

Co-authored-by: mshanemc <shane.mclaughlin@salesforce.com>
  • Loading branch information
cristiand391 and mshanemc authored Jul 13, 2023
1 parent 3f4b0f8 commit 7003b40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,25 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
return fws
}

const addDefaultHelp = async (fws: FlagWithStrategy[]): Promise<FlagWithStrategy[]> => {
const addDefaultHelp = async (fwsArray: FlagWithStrategy[]): Promise<FlagWithStrategy[]> => {
const valueReferenceForHelp = fwsArrayToObject(flagsWithAllValues.filter(fws => !fws.metadata?.setFromDefault))
return Promise.all(fws.map(async fws => fws.helpFunction ? ({...fws, metadata: {...fws.metadata, defaultHelp: await fws.helpFunction?.(fws, valueReferenceForHelp, this.context)}}) : fws))
return Promise.all(fwsArray.map(async fws => {
try {
if (fws.helpFunction) {
return {
...fws,
metadata: {
...fws.metadata,
defaultHelp: await fws.helpFunction?.(fws, valueReferenceForHelp, this.context),
},
}
}
} catch {
// no-op
}

return fws
}))
}

const fwsArrayToObject = (fwsArray: FlagWithStrategy[]) => Object.fromEntries(
Expand Down
12 changes: 12 additions & 0 deletions test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ describe('parse', () => {
expect(Boolean(out.flags.myflag)).to.equal(true)
expect(Boolean(out.flags.myflag2)).to.equal(true)
})
it('doesn\' throw if defaultHelp func fails', async () => {
const out = await parse(['--foo', 'baz'], {
flags: {
foo: Flags.custom({
defaultHelp: async () => {
throw new Error('failed to get default help value')
},
})(),
},
})
expect(out.flags.foo).to.equal('baz')
})

it('doesn\'t throw when 2nd char in value matches a flag char', async () => {
const out = await parse(['--myflag', 'Ishikawa', '-s', 'value'], {
Expand Down

0 comments on commit 7003b40

Please sign in to comment.