From 7003b403a3cb9f6fe54a87de843dbc058f96be6c Mon Sep 17 00:00:00 2001 From: Cristian Dominguez <6853656+cristiand391@users.noreply.github.com> Date: Wed, 12 Jul 2023 22:45:18 -0300 Subject: [PATCH] fix(parser): don't throw if defaultHelp func throws (#732) * 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 --- src/parser/parse.ts | 20 ++++++++++++++++++-- test/parser/parse.test.ts | 12 ++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index 410ef7b55..2d83b37f9 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -312,9 +312,25 @@ export class Parser => { + const addDefaultHelp = async (fwsArray: FlagWithStrategy[]): Promise => { 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( diff --git a/test/parser/parse.test.ts b/test/parser/parse.test.ts index 06e3852a4..e65f80aa5 100644 --- a/test/parser/parse.test.ts +++ b/test/parser/parse.test.ts @@ -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'], {