Skip to content

Commit

Permalink
fix(command): accept ignoreArgv (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 12, 2022
1 parent 460a935 commit 8344d6e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TypeFlagOptions } from 'type-flag';
import type {
CallbackFunction,
Flags,
Expand Down Expand Up @@ -37,6 +38,11 @@ export type CommandOptions<Parameters = string[]> = {
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<
Expand Down
46 changes: 46 additions & 0 deletions tests/specs/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
33 changes: 33 additions & 0 deletions tests/specs/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
});
});
});
});
4 changes: 0 additions & 4 deletions tests/tsconfig.json

This file was deleted.

3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@
"esModuleInterop": true,
"module": "Node16"
},
"include": [
"src",
],
}

0 comments on commit 8344d6e

Please sign in to comment.