From 14aca322e7f38e189c95a93d6776b92285e81f40 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 21 Jul 2021 15:06:02 -0600 Subject: [PATCH 1/4] fix: positionals should not overwrite options --- lib/command.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/command.ts b/lib/command.ts index 866eb5324..003be7709 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -598,7 +598,13 @@ export class CommandInstance { // any new aliases need to be placed in positionalMap, which // is used for validation. if (!positionalMap[key]) positionalMap[key] = parsed.argv[key]; - argv[key] = parsed.argv[key]; + // addresses: https://github.com/yargs/yargs/issues/1637 + // don't overwrite if key already exists in argv + if (argv[key]) { + argv[key] = ([] as string[]).concat(argv[key], positionalMap[key]); + } else { + argv[key] = parsed.argv[key]; + } } }); } From d646458e1d37d4627da6f1bb7c4664751fa623a7 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Wed, 21 Jul 2021 21:47:18 -0600 Subject: [PATCH 2/4] fixes --- lib/command.ts | 8 +------- test/command.cjs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/command.ts b/lib/command.ts index 003be7709..866eb5324 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -598,13 +598,7 @@ export class CommandInstance { // any new aliases need to be placed in positionalMap, which // is used for validation. if (!positionalMap[key]) positionalMap[key] = parsed.argv[key]; - // addresses: https://github.com/yargs/yargs/issues/1637 - // don't overwrite if key already exists in argv - if (argv[key]) { - argv[key] = ([] as string[]).concat(argv[key], positionalMap[key]); - } else { - argv[key] = parsed.argv[key]; - } + argv[key] = parsed.argv[key]; } }); } diff --git a/test/command.cjs b/test/command.cjs index fd0eefef8..1540d9d06 100644 --- a/test/command.cjs +++ b/test/command.cjs @@ -209,6 +209,41 @@ describe('Command', () => { argv['--'].should.eql(['apple', 'banana']); called.should.equal(true); }); + + // Addresses: https://github.com/yargs/yargs/issues/1637 + it('does not overwrite options in argv', () => { + yargs + .command({ + command: 'cmd [foods..]', + desc: 'cmd desc', + builder: yargs => + yargs.positional('foods', { + desc: 'foods desc', + type: 'string', + }), + handler: argv => { + argv.foods.should.deep.equal(['apples', 'cherries', 'grapes']); + }, + }) + .parse('cmd --foods apples cherries grapes'); + }); + + it('does not overwrite options in argv when using default command', () => { + yargs + .command({ + command: '$0 [foods..]', + desc: 'default desc', + builder: yargs => + yargs.positional('foods', { + desc: 'foods desc', + type: 'string', + }), + handler: argv => { + argv.foods.should.deep.equal(['apples', 'cherries', 'grapes']); + }, + }) + .parse('--foods apples cherries grapes'); + }); }); describe('variadic', () => { From fe1b3333c8e6c4e8d6a37cf062a2203ccb492076 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Mon, 26 Jul 2021 20:17:25 -0600 Subject: [PATCH 3/4] fixes --- lib/command.ts | 13 ++++++++++++- lib/yargs-factory.ts | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/command.ts b/lib/command.ts index 866eb5324..f1af3b84b 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -598,7 +598,18 @@ export class CommandInstance { // any new aliases need to be placed in positionalMap, which // is used for validation. if (!positionalMap[key]) positionalMap[key] = parsed.argv[key]; - argv[key] = parsed.argv[key]; + // Addresses: https://github.com/yargs/yargs/issues/1637 + // If both positionals/options provided, + // and if at least one is an array: don't overwrite, combine. + if ( + argv[key] && + parsed.argv[key] && + (Array.isArray(argv[key]) || Array.isArray(parsed.argv[key])) + ) { + argv[key] = ([] as string[]).concat(argv[key], parsed.argv[key]); + } else { + argv[key] = parsed.argv[key]; + } } }); } diff --git a/lib/yargs-factory.ts b/lib/yargs-factory.ts index 4f602d7e8..23d6e0f27 100644 --- a/lib/yargs-factory.ts +++ b/lib/yargs-factory.ts @@ -809,7 +809,7 @@ export class YargsInstance { ); } else { globals.forEach(g => { - if (this.#options.local.indexOf(g) === -1) this.#options.local.push(g); + if (!this.#options.local.includes(g)) this.#options.local.push(g); }); } return this; From 0541d36a8715882d5104bebae906e97dac5003f5 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 27 Jul 2021 06:26:57 -0600 Subject: [PATCH 4/4] fixes --- lib/command.ts | 4 ++-- test/command.cjs | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/command.ts b/lib/command.ts index f1af3b84b..99f0724bb 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -602,8 +602,8 @@ export class CommandInstance { // If both positionals/options provided, // and if at least one is an array: don't overwrite, combine. if ( - argv[key] && - parsed.argv[key] && + Object.prototype.hasOwnProperty.call(argv, key) && + Object.prototype.hasOwnProperty.call(parsed.argv, key) && (Array.isArray(argv[key]) || Array.isArray(parsed.argv[key])) ) { argv[key] = ([] as string[]).concat(argv[key], parsed.argv[key]); diff --git a/test/command.cjs b/test/command.cjs index 1540d9d06..984481cf4 100644 --- a/test/command.cjs +++ b/test/command.cjs @@ -211,7 +211,7 @@ describe('Command', () => { }); // Addresses: https://github.com/yargs/yargs/issues/1637 - it('does not overwrite options in argv', () => { + it('does not overwrite options in argv if variadic', () => { yargs .command({ command: 'cmd [foods..]', @@ -228,7 +228,7 @@ describe('Command', () => { .parse('cmd --foods apples cherries grapes'); }); - it('does not overwrite options in argv when using default command', () => { + it('does not overwrite options in argv if variadic and when using default command', () => { yargs .command({ command: '$0 [foods..]', @@ -244,6 +244,23 @@ describe('Command', () => { }) .parse('--foods apples cherries grapes'); }); + + it('does not overwrite options in argv if variadic and preserves falsey values', () => { + yargs + .command({ + command: '$0 [numbers..]', + desc: 'default desc', + builder: yargs => + yargs.positional('numbers', { + desc: 'numbers desc', + type: 'number', + }), + handler: argv => { + argv.numbers.should.deep.equal([0, 1, 2]); + }, + }) + .parse('--numbers 0 1 2'); + }); }); describe('variadic', () => {