From 67cc79cd6971eff9d59722cd6a93fee6011b86a1 Mon Sep 17 00:00:00 2001 From: urielCh Date: Thu, 12 Dec 2019 17:44:13 +0200 Subject: [PATCH 01/11] fix option type def --- typings/index.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index a2fc6a989..2c3cd4474 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -165,11 +165,12 @@ declare namespace commander { * * @param {string} flags * @param {string} [description] - * @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default + * @param {((value: string, previous: 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, regexp?: RegExp, defaultValue?: any): Command; + option(flags: string, description?: string, fn?: ((value: string, previous: T) => T), defaultValue?: T): Command; option(flags: string, description?: string, defaultValue?: any): Command; /** From de995da7d48d71fe15c7376dca27c277068db105 Mon Sep 17 00:00:00 2001 From: urielCh Date: Thu, 12 Dec 2019 18:01:45 +0200 Subject: [PATCH 02/11] add test improve types --- typings/commander-tests.ts | 7 +++++++ typings/index.d.ts | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/typings/commander-tests.ts b/typings/commander-tests.ts index 25c00e9e5..7d69d15bb 100644 --- a/typings/commander-tests.ts +++ b/typings/commander-tests.ts @@ -37,6 +37,12 @@ function collect(val: string, memo: string[]) { memo.push(val); return memo; } +/* + * Collector without return all so work + */ +function collect2(val: string, memo: string[]) { + memo.push(val); +} function increaseVerbosity(v: any, total: number) { return total + 1; @@ -51,6 +57,7 @@ program .option('-l, --list ', '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); diff --git a/typings/index.d.ts b/typings/index.d.ts index 2c3cd4474..ada292593 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -169,9 +169,10 @@ declare namespace commander { * @param {*} [defaultValue] * @returns {Command} for chaining */ - option(flags: string, description?: string, regexp?: RegExp, defaultValue?: any): Command; - option(flags: string, description?: string, fn?: ((value: string, previous: T) => T), defaultValue?: T): Command; option(flags: string, description?: string, defaultValue?: any): Command; + option(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; + option(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; + option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; /** * Define a required option, which must have a value after parsing. This usually means From 78fa81d335ad7fce47c04d9f6060f1a36fd96e4a Mon Sep 17 00:00:00 2001 From: urielCh Date: Wed, 18 Dec 2019 08:41:52 +0200 Subject: [PATCH 03/11] change definition order --- typings/index.d.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index ada292593..195d433bb 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -162,17 +162,11 @@ declare namespace commander { * * // optional argument * program.option('-c, --cheese [type]', 'add cheese [marble]'); - * - * @param {string} flags - * @param {string} [description] - * @param {((value: string, previous: any) => void) | RegExp} [fn] function or default - * @param {*} [defaultValue] - * @returns {Command} for chaining */ - option(flags: string, description?: string, defaultValue?: any): Command; option(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; option(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; + option(flags: string, description?: string, defaultValue?: any): Command; /** * Define a required option, which must have a value after parsing. This usually means @@ -180,7 +174,9 @@ declare namespace commander { * * 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, regexp: RegExp, defaultValue?: any): Command; + requiredOption(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; + requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; requiredOption(flags: string, description?: string, defaultValue?: any): Command; /** From 5129c973651e5b4e3123d5796ef8ffafcedbcf3f Mon Sep 17 00:00:00 2001 From: urielCh Date: Wed, 18 Dec 2019 08:50:37 +0200 Subject: [PATCH 04/11] change def order --- typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 195d433bb..7b153b684 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -164,8 +164,8 @@ declare namespace commander { * program.option('-c, --cheese [type]', 'add cheese [marble]'); */ option(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; - option(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; + option(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; option(flags: string, description?: string, defaultValue?: any): Command; /** @@ -175,8 +175,8 @@ declare namespace commander { * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space. */ requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; - requiredOption(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; + requiredOption(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; requiredOption(flags: string, description?: string, defaultValue?: any): Command; /** From cd8fd034b421f36c811b0cd5466a904b4a68546b Mon Sep 17 00:00:00 2001 From: urielCh Date: Wed, 18 Dec 2019 08:53:54 +0200 Subject: [PATCH 05/11] fix typing --- typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 7b153b684..1a7ee899d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -165,7 +165,7 @@ declare namespace commander { */ option(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; - option(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; + option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; option(flags: string, description?: string, defaultValue?: any): Command; /** @@ -176,7 +176,7 @@ declare namespace commander { */ requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; - requiredOption(flags: string, description: string, fn: (value: string, previous: T) => (T | void), defaultValue?: T): Command; + requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; requiredOption(flags: string, description?: string, defaultValue?: any): Command; /** From 5823a8e2aa048cb89d8bc6dbb8342388e8c5a378 Mon Sep 17 00:00:00 2001 From: urielCh Date: Tue, 24 Dec 2019 09:16:51 +0200 Subject: [PATCH 06/11] improve typing enforce current implementation --- tests/options.custom-processing.test.js | 20 ++++++++++++++++++++ typings/commander-tests.ts | 8 ++++---- typings/index.d.ts | 12 ++++++------ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/options.custom-processing.test.js b/tests/options.custom-processing.test.js index d5a9b98b8..febf825a1 100644 --- a/tests/options.custom-processing.test.js +++ b/tests/options.custom-processing.test.js @@ -13,6 +13,10 @@ function collect(value, previous) { return previous.concat([value]); } +function collectVoid(value, previous) { + previous.concat([value]); +} + function commaSeparatedList(value, dummyPrevious) { return value.split(','); } @@ -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 ', '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 ', '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(); diff --git a/typings/commander-tests.ts b/typings/commander-tests.ts index 7d69d15bb..ac729407f 100644 --- a/typings/commander-tests.ts +++ b/typings/commander-tests.ts @@ -40,9 +40,9 @@ function collect(val: string, memo: string[]) { /* * Collector without return all so work */ -function collect2(val: string, memo: string[]) { - memo.push(val); -} +//function collect2(val: string, memo: string[]) { +// memo.push(val); +//} function increaseVerbosity(v: any, total: number) { return total + 1; @@ -57,7 +57,7 @@ program .option('-l, --list ', '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('--collect2 [value]', 'A repeatable value void return', collect2, []) .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0) .parse(process.argv); diff --git a/typings/index.d.ts b/typings/index.d.ts index 1a7ee899d..661739bbe 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -163,10 +163,10 @@ declare namespace commander { * // optional argument * program.option('-c, --cheese [type]', 'add cheese [marble]'); */ - option(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; - option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; + option(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command; + // option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; - option(flags: string, description?: string, defaultValue?: any): Command; + option(flags: string, description?: string, defaultValue?: string): Command; /** * Define a required option, which must have a value after parsing. This usually means @@ -174,10 +174,10 @@ declare namespace commander { * * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space. */ - requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: any): Command; - requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; + requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command; + // requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; - requiredOption(flags: string, description?: string, defaultValue?: any): Command; + requiredOption(flags: string, description?: string, defaultValue?: string): Command; /** * Allow unknown options on the command line. From c9b5deb68a711025cc9b8cabb467b0e10f2c0fb9 Mon Sep 17 00:00:00 2001 From: urielCh Date: Sat, 28 Dec 2019 18:32:35 +0200 Subject: [PATCH 07/11] final patch --- typings/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 661739bbe..0a7f75546 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -163,10 +163,10 @@ declare namespace commander { * // optional argument * program.option('-c, --cheese [type]', 'add cheese [marble]'); */ - option(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command; - // option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; - option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; option(flags: string, description?: string, defaultValue?: string): Command; + option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; + option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; + // option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; /** * Define a required option, which must have a value after parsing. This usually means @@ -174,10 +174,10 @@ declare namespace commander { * * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space. */ - requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string): Command; - // requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; - requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; requiredOption(flags: string, description?: string, defaultValue?: string): Command; + requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; + requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; + // requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; /** * Allow unknown options on the command line. From 79335117140806521b95bbe2f7dd3a5331e83021 Mon Sep 17 00:00:00 2001 From: urielCh Date: Sat, 28 Dec 2019 18:37:51 +0200 Subject: [PATCH 08/11] drop all void returning option --- tests/options.custom-processing.test.js | 20 -------------------- typings/index.d.ts | 2 -- 2 files changed, 22 deletions(-) diff --git a/tests/options.custom-processing.test.js b/tests/options.custom-processing.test.js index febf825a1..d5a9b98b8 100644 --- a/tests/options.custom-processing.test.js +++ b/tests/options.custom-processing.test.js @@ -13,10 +13,6 @@ function collect(value, previous) { return previous.concat([value]); } -function collectVoid(value, previous) { - previous.concat([value]); -} - function commaSeparatedList(value, dummyPrevious) { return value.split(','); } @@ -135,22 +131,6 @@ 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 ', '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 ', '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(); diff --git a/typings/index.d.ts b/typings/index.d.ts index 0a7f75546..c73378fee 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -166,7 +166,6 @@ declare namespace commander { option(flags: string, description?: string, defaultValue?: string): Command; option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; - // option(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; /** * Define a required option, which must have a value after parsing. This usually means @@ -177,7 +176,6 @@ declare namespace commander { requiredOption(flags: string, description?: string, defaultValue?: string): Command; requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; - // requiredOption(flags: string, description: string, fn: (value: string, previous: T) => void, defaultValue: T): Command; /** * Allow unknown options on the command line. From 9de970bc3fd3298af11aa28d0421e250559d220a Mon Sep 17 00:00:00 2001 From: urielCh Date: Tue, 31 Dec 2019 18:19:23 +0200 Subject: [PATCH 09/11] fix missing boolean type for default value --- typings/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index c73378fee..e2d31778d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -163,7 +163,7 @@ declare namespace commander { * // optional argument * program.option('-c, --cheese [type]', 'add cheese [marble]'); */ - option(flags: string, description?: string, defaultValue?: string): Command; + option(flags: string, description?: string, defaultValue?: string | boolean): Command; option(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; option(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; @@ -173,7 +173,7 @@ declare namespace commander { * * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space. */ - requiredOption(flags: string, description?: string, defaultValue?: string): Command; + requiredOption(flags: string, description?: string, defaultValue?: string | boolean): Command; requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): Command; requiredOption(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): Command; From 383afb64a765351b8780e8a31829a4673ff64aea Mon Sep 17 00:00:00 2001 From: urielCh Date: Tue, 31 Dec 2019 18:21:47 +0200 Subject: [PATCH 10/11] clean code --- typings/commander-tests.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/typings/commander-tests.ts b/typings/commander-tests.ts index ac729407f..d25fd0a76 100644 --- a/typings/commander-tests.ts +++ b/typings/commander-tests.ts @@ -37,12 +37,6 @@ function collect(val: string, memo: string[]) { memo.push(val); return memo; } -/* - * Collector without return all so work - */ -//function collect2(val: string, memo: string[]) { -// memo.push(val); -//} function increaseVerbosity(v: any, total: number) { return total + 1; From 742621097e95d250db99f73010be51b7a5e533ee Mon Sep 17 00:00:00 2001 From: urielCh Date: Tue, 31 Dec 2019 18:22:02 +0200 Subject: [PATCH 11/11] clean code --- typings/commander-tests.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/typings/commander-tests.ts b/typings/commander-tests.ts index d25fd0a76..25c00e9e5 100644 --- a/typings/commander-tests.ts +++ b/typings/commander-tests.ts @@ -51,7 +51,6 @@ program .option('-l, --list ', '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);