Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix option type definition to match usage. #1119

Merged
merged 12 commits into from Jan 9, 2020
20 changes: 20 additions & 0 deletions tests/options.custom-processing.test.js
Expand Up @@ -13,6 +13,10 @@ function collect(value, previous) {
return previous.concat([value]);
}

function collectVoid(value, previous) {
UrielCh marked this conversation as resolved.
Show resolved Hide resolved
previous.concat([value]);
}

function commaSeparatedList(value, dummyPrevious) {
return value.split(',');
}
Expand Down Expand Up @@ -131,6 +135,22 @@ test('when collect -c a -c b -c c then value is [a, b, c]', () => {
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);
expect(program.collect).toEqual(['a', 'b', 'c']);
});
// freeze void returning collector behaviour
test('when collect returning Void -c a -c b -c c then value is void', () => {
const program = new commander.Command();
program
.option('-c, --collect <value>', 'repeatable value', collectVoid, []);
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c']);
expect(program.collect).toBeUndefined();
});
// freeze void returning collector behaviour
test('when collect returning Void -c a -c b -c c -c d then value is []', () => {
const program = new commander.Command();
program
.option('-c, --collect <value>', 'repeatable value', collectVoid, []);
program.parse(['node', 'test', '-c', 'a', '-c', 'b', '-c', 'c', '-c', 'd']);
expect(program.collect).toEqual([]);
});

test('when commaSeparatedList x,y,z then value is [x, y, z]', () => {
const program = new commander.Command();
Expand Down
7 changes: 7 additions & 0 deletions typings/commander-tests.ts
Expand Up @@ -37,6 +37,12 @@ function collect(val: string, memo: string[]) {
memo.push(val);
return memo;
}
/*
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
* Collector without return all so work
UrielCh marked this conversation as resolved.
Show resolved Hide resolved
*/
//function collect2(val: string, memo: string[]) {
// memo.push(val);
//}

function increaseVerbosity(v: any, total: number) {
return total + 1;
Expand All @@ -51,6 +57,7 @@ program
.option('-l, --list <items>', 'A list', list)
.option('-o, --optional [value]', 'An optional value')
.option('-c, --collect [value]', 'A repeatable value', collect, [])
// .option('--collect2 [value]', 'A repeatable value void return', collect2, [])
.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
.parse(process.argv);

Expand Down
18 changes: 8 additions & 10 deletions typings/index.d.ts
Expand Up @@ -162,24 +162,22 @@ declare namespace commander {
*
* // optional argument
* program.option('-c, --cheese [type]', 'add cheese [marble]');
*
* @param {string} flags
* @param {string} [description]
* @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default
* @param {*} [defaultValue]
* @returns {Command} for chaining
*/
option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
option(flags: string, description?: string, defaultValue?: any): Command;
option(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command;
UrielCh marked this conversation as resolved.
Show resolved Hide resolved
// option<T>(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command;
option<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command;
option(flags: string, description?: string, defaultValue?: string): Command;

/**
* Define a required option, which must have a value after parsing. This usually means
* the option must be specified on the command line. (Otherwise the same as .option().)
*
* The `flags` string should contain both the short and long flags, separated by comma, a pipe or space.
*/
requiredOption(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
requiredOption(flags: string, description?: string, defaultValue?: any): Command;
requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command;
// requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command;
requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command;
requiredOption(flags: string, description?: string, defaultValue?: string): Command;

/**
* Allow unknown options on the command line.
Expand Down