From 0a7e15415f3514a4baad064478c76f0c17e9548a Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 12 Jul 2023 10:16:59 -0500 Subject: [PATCH 1/2] 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()}, From 72778488f20fe3447554e03b33d330cdc6852125 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Wed, 12 Jul 2023 10:23:08 -0500 Subject: [PATCH 2/2] chore: file cleanup --- perf.txt | 6 ------ src/parser/parse.ts | 8 +++++--- 2 files changed, 5 insertions(+), 9 deletions(-) delete mode 100644 perf.txt diff --git a/perf.txt b/perf.txt deleted file mode 100644 index 5c7d34bcc..000000000 --- a/perf.txt +++ /dev/null @@ -1,6 +0,0 @@ -yarn run v1.22.19 -$ ts-node test/perf/parser.perf.ts -simple x 131,268 ops/sec ±1.15% (79 runs sampled) -multiple async flags that take time x 9.90 ops/sec ±0.14% (50 runs sampled) -flagstravaganza x 7,425 ops/sec ±2.10% (83 runs sampled) -Done in 21.60s. diff --git a/src/parser/parse.ts b/src/parser/parse.ts index e04d8fb75..410ef7b55 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -317,7 +317,10 @@ export class Parser 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: { @@ -346,9 +349,8 @@ 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