From 8344d6e69c0d2d9c69d7d5e43179e03aa6b74dc5 Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Sat, 12 Nov 2022 08:40:14 -0500 Subject: [PATCH] fix(command): accept `ignoreArgv` (#19) --- src/command.ts | 6 ++++++ tests/specs/command.ts | 46 ++++++++++++++++++++++++++++++++++++++++++ tests/specs/flags.ts | 33 ++++++++++++++++++++++++++++++ tests/tsconfig.json | 4 ---- tsconfig.json | 3 --- 5 files changed, 85 insertions(+), 7 deletions(-) delete mode 100644 tests/tsconfig.json diff --git a/src/command.ts b/src/command.ts index 82bc457..b0a4379 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,3 +1,4 @@ +import type { TypeFlagOptions } from 'type-flag'; import type { CallbackFunction, Flags, @@ -37,6 +38,11 @@ export type CommandOptions = { Options to configure the help documentation. Pass in `false` to disable handling `--help, -h`. */ help?: false | HelpOptions; + + /** + * Which argv elements to ignore from parsing + */ + ignoreArgv?: TypeFlagOptions['ignore']; }; export function command< diff --git a/tests/specs/command.ts b/tests/specs/command.ts index 91fc6a9..c897d54 100644 --- a/tests/specs/command.ts +++ b/tests/specs/command.ts @@ -269,5 +269,51 @@ export default testSuite(({ describe }) => { expect(callback.called).toBe(true); }); }); + + describe('ignoreArgv', ({ test }) => { + test('ignore after arguments', () => { + const callback = spy(); + const argv = ['commandA', '--unknown', 'arg', '--help']; + + let receivedArgument = false; + const commandA = command({ + name: 'commandA', + flags: { + flagA: String, + }, + ignoreArgv(type) { + if (receivedArgument) { + return true; + } + if (type === 'argument') { + receivedArgument = true; + return true; + } + }, + }, (parsed) => { + expect<(string | boolean)[]>(parsed.unknownFlags.unknown).toStrictEqual([true]); + callback(); + }); + + const parsed = cli( + { + commands: [ + commandA, + ], + }, + undefined, + argv, + ); + + expect(parsed.command).toBe('commandA'); + + // Narrow type + if (parsed.command === 'commandA') { + expect<(string | boolean)[]>(parsed.unknownFlags.unknown).toStrictEqual([true]); + } + + expect(callback.called).toBe(true); + }); + }); }); }); diff --git a/tests/specs/flags.ts b/tests/specs/flags.ts index 3a9573e..e8979d4 100644 --- a/tests/specs/flags.ts +++ b/tests/specs/flags.ts @@ -120,5 +120,38 @@ export default testSuite(({ describe }) => { expect(mocked.processExit.calls).toStrictEqual([[0]]); }); }); + + describe('ignoreArgv', ({ test }) => { + test('ignore after arguments', () => { + const argv = ['--unknown', 'arg', '--help']; + + let receivedArgument = false; + const parsed = cli( + { + ignoreArgv(type) { + if (receivedArgument) { + return true; + } + if (type === 'argument') { + receivedArgument = true; + return true; + } + }, + }, + (p) => { + expect(argv).toStrictEqual(['arg', '--help']); + expect(p.unknownFlags).toStrictEqual({ + unknown: [true], + }); + }, + argv, + ); + + expect(argv).toStrictEqual(['arg', '--help']); + expect(parsed.unknownFlags).toStrictEqual({ + unknown: [true], + }); + }); + }); }); }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json deleted file mode 100644 index 784e219..0000000 --- a/tests/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../tsconfig.json", - "include": ["."] -} diff --git a/tsconfig.json b/tsconfig.json index 2796d6f..a28a889 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,4 @@ "esModuleInterop": true, "module": "Node16" }, - "include": [ - "src", - ], }