From 0a7e15415f3514a4baad064478c76f0c17e9548a Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 12 Jul 2023 10:16:59 -0500 Subject: [PATCH] fix: flags omit undefined for boolean flags --- src/parser/parse.ts | 3 ++- test/parser/parse.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index 218aa5051..e04d8fb75 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -346,8 +346,9 @@ export class Parser !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 diff --git a/test/parser/parse.test.ts b/test/parser/parse.test.ts index 62c8eaacf..06e3852a4 100644 --- a/test/parser/parse.test.ts +++ b/test/parser/parse.test.ts @@ -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()},