From 826218703c82954737b6d4645328a91ff82e0feb Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 22 Mar 2019 11:15:25 +0100 Subject: [PATCH 1/3] maybeCoerceNumber(): don't loose array type, adapt existing tests, add new tests --- index.js | 4 +++- test/yargs-parser.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index a6c95ba3..aaf69f25 100644 --- a/index.js +++ b/index.js @@ -501,7 +501,9 @@ function parse (args, opts) { const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && ( Number.isSafeInteger(Math.floor(value)) ) - if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value) + if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) { + value = Array.isArray(value) ? [Number(value)] : Number(value) + } } return value } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index a8628261..83b3d4d0 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2191,7 +2191,7 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal(['a', 'b']) }) - it('keeps only last argument', function () { + it('string: keeps only last argument', function () { var parsed = parser('-x a -x b', { configuration: { 'duplicate-arguments-array': false @@ -2200,6 +2200,26 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) + it('array[string]: keeps only last argument', function () { + var parsed = parser('-x a -x b', { + array: [{ key: 'x', string: true }], + configuration: { + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.equal('b') + }) + it('array[number]: keeps only last argument', function () { + var parsed = parser('-x 1 -x 2', { + array: [{ key: 'x', number: true }], + configuration: { + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.equal(2) + }) it('does not interfere with nargs', function () { var parsed = parser('-x a b c -x o p q', { narg: { x: 3 }, @@ -2405,25 +2425,25 @@ describe('yargs-parser', function () { }) describe('duplicate=true, flatten=false,', function () { describe('type=array', function () { - it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () { + it('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () { var parsed = parser('-x 1 -x 2 -x 3', { - array: ['x'], + array: [{ key: 'x', number: true }], configuration: { 'duplicate-arguments-array': true, 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([1, 2, 3]) + parsed['x'].should.deep.equal([[1], [2], [3]]) }) - it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [ 2, 3, 4]]', function () { - var parsed = parser('-x 1 2 3 -x 2 3 4', { - array: ['x'], + it("string: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () { + var parsed = parser('-x 1 -x 2 -x 3', { + array: [{ key: 'x', string: true }], configuration: { 'duplicate-arguments-array': true, 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]]) + parsed['x'].should.deep.equal([['1'], ['2'], ['3']]) }) }) describe('type=number', function () { From 61faf08fd1c33a4ec19e041d92b6c01abdfacd16 Mon Sep 17 00:00:00 2001 From: juergba Date: Mon, 10 Jun 2019 15:53:44 +0200 Subject: [PATCH 2/3] type "array" precedes "duplicate-arguments-array" --- README.md | 2 +- index.js | 5 +++++ test/yargs-parser.js | 44 +++++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index dde9f84d..99e19304 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ a configuration file. * default: `true` * key: `duplicate-arguments-array` -Should arguments be coerced into an array when duplicated: +Should arguments be coerced into an array when duplicated? This option has no effect on `opts.array`. ```sh node example.js -x 1 -x 2 diff --git a/index.js b/index.js index aaf69f25..83e1a0e3 100644 --- a/index.js +++ b/index.js @@ -686,6 +686,11 @@ function parse (args, opts) { var isValueArray = Array.isArray(value) var duplicate = configuration['duplicate-arguments-array'] + // array has higher priority than duplicate + if (!duplicate && isTypeArray) { + duplicate = true + } + // nargs has higher priority than duplicate if (!duplicate && checkAllAliases(key, flags.nargs)) { duplicate = true diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 83b3d4d0..6d20eda3 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2200,25 +2200,33 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) - it('array[string]: keeps only last argument', function () { - var parsed = parser('-x a -x b', { - array: [{ key: 'x', string: true }], + it('array[string]: keeps all arguments - ignores configuration', function () { + var parsed = parser('-x a -x b -y c', { + array: [ + { key: 'x', string: true }, + { key: 'y', string: true } + ], configuration: { 'duplicate-arguments-array': false } }) - parsed['x'].should.equal('b') - }) - it('array[number]: keeps only last argument', function () { - var parsed = parser('-x 1 -x 2', { - array: [{ key: 'x', number: true }], + parsed['x'].should.deep.equal(['a', 'b']) + parsed['y'].should.deep.equal(['c']) + }) + it('array[number]: keeps all arguments - ignores configuration', function () { + var parsed = parser('-x 1 -x 2 -y 3', { + array: [ + { key: 'x', number: true }, + { key: 'y', number: true } + ], configuration: { 'duplicate-arguments-array': false } }) - parsed['x'].should.equal(2) + parsed['x'].should.deep.equal([1, 2]) + parsed['y'].should.deep.equal([3]) }) it('does not interfere with nargs', function () { var parsed = parser('-x a b c -x o p q', { @@ -2327,15 +2335,16 @@ describe('yargs-parser', function () { }) parsed['x'].should.deep.equal([1, 2, 3]) }) - it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () { - var parsed = parser('-x 1 2 3 -x 2 3 4', { - array: ['x'], + it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]', function () { + var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', { + array: ['x', 'y'], configuration: { 'duplicate-arguments-array': false, 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([2, 3, 4]) + parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]]) + parsed['y'].should.deep.equal([7]) }) }) describe('type=number', function () { @@ -2363,15 +2372,16 @@ describe('yargs-parser', function () { }) parsed['x'].should.deep.equal([1, 2, 3]) }) - it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () { - var parsed = parser('-x 1 2 3 -x 2 3 4', { - array: ['x'], + it('[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]', function () { + var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', { + array: ['x', 'y'], configuration: { 'duplicate-arguments-array': false, 'flatten-duplicate-arrays': true } }) - parsed['x'].should.deep.equal([2, 3, 4]) + parsed['x'].should.deep.equal([1, 2, 3, 2, 3, 4]) + parsed['y'].should.deep.equal([7]) }) }) describe('type=number', function () { From c50873ac64dfaaade3c308e327871d18eb7ad449 Mon Sep 17 00:00:00 2001 From: juergba Date: Mon, 10 Jun 2019 19:13:41 +0200 Subject: [PATCH 3/3] fix maybeCoerceNumber() --- index.js | 6 +++--- test/yargs-parser.js | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 83e1a0e3..c29f0c46 100644 --- a/index.js +++ b/index.js @@ -497,13 +497,13 @@ function parse (args, opts) { } function maybeCoerceNumber (key, value) { + if (Array.isArray(value)) return value + if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) { const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && ( Number.isSafeInteger(Math.floor(value)) ) - if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) { - value = Array.isArray(value) ? [Number(value)] : Number(value) - } + if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value) } return value } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 6d20eda3..b8cadf02 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2200,11 +2200,12 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) - it('array[string]: keeps all arguments - ignores configuration', function () { - var parsed = parser('-x a -x b -y c', { + it('string[]: keeps all arguments - ignores configuration', function () { + var parsed = parser('-x a -x b -y c -z d e -z f g', { array: [ { key: 'x', string: true }, - { key: 'y', string: true } + { key: 'y', string: true }, + { key: 'z', string: true } ], configuration: { 'duplicate-arguments-array': false @@ -2213,12 +2214,14 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal(['a', 'b']) parsed['y'].should.deep.equal(['c']) + parsed['z'].should.deep.equal(['d', 'e', 'f', 'g']) }) - it('array[number]: keeps all arguments - ignores configuration', function () { - var parsed = parser('-x 1 -x 2 -y 3', { + it('number[]]: keeps all arguments - ignores configuration', function () { + var parsed = parser('-x 1 -x 2 -y 3 -z 4 5 -z 6 7', { array: [ { key: 'x', number: true }, - { key: 'y', number: true } + { key: 'y', number: true }, + { key: 'z', number: true } ], configuration: { 'duplicate-arguments-array': false @@ -2227,6 +2230,7 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal([1, 2]) parsed['y'].should.deep.equal([3]) + parsed['z'].should.deep.equal([4, 5, 6, 7]) }) it('does not interfere with nargs', function () { var parsed = parser('-x a b c -x o p q', { @@ -2298,14 +2302,14 @@ describe('yargs-parser', function () { duplicate=false, flatten=false type=array [-x 1 2 3] => [1, 2, 3] - [-x 1 2 3 -x 2 3 4] => [2, 3, 4] + [-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]] type=string/number/etc [-x 1 -x 2 -x 3] => 3 duplicate=false, flatten=true type=array [-x 1 2 3] => [1, 2, 3] - [-x 1 2 3 -x 2 3 4] => [2, 3, 4] + [-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4] type=string/number/etc [-x 1 -x 2 -x 3] => 3 @@ -2435,25 +2439,33 @@ describe('yargs-parser', function () { }) describe('duplicate=true, flatten=false,', function () { describe('type=array', function () { - it('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () { - var parsed = parser('-x 1 -x 2 -x 3', { - array: [{ key: 'x', number: true }], + it('number[]: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () { + var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', { + array: [ + { key: 'x', number: true }, + { key: 'y', number: true } + ], configuration: { 'duplicate-arguments-array': true, 'flatten-duplicate-arrays': false } }) parsed['x'].should.deep.equal([[1], [2], [3]]) + parsed['y'].should.deep.equal([[4, 5], [6, 7]]) }) - it("string: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () { - var parsed = parser('-x 1 -x 2 -x 3', { - array: [{ key: 'x', string: true }], + it("string[]: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () { + var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', { + array: [ + { key: 'x', string: true }, + { key: 'y', string: true } + ], configuration: { 'duplicate-arguments-array': true, 'flatten-duplicate-arrays': false } }) parsed['x'].should.deep.equal([['1'], ['2'], ['3']]) + parsed['y'].should.deep.equal([['4', '5'], ['6', '7']]) }) }) describe('type=number', function () {