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 218aa5051..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: { 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()},