From 47ccb0b7fcca1b989ef97bb084e4aa2aaf2a7666 Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Fri, 7 Jun 2019 19:30:56 +0200 Subject: [PATCH] fix: nargs should allow duplicates when duplicate-arguments-array=false (#164) --- index.js | 8 ++++++++ test/yargs-parser.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3b772185..a6c95ba3 100644 --- a/index.js +++ b/index.js @@ -684,6 +684,14 @@ function parse (args, opts) { var isValueArray = Array.isArray(value) var duplicate = configuration['duplicate-arguments-array'] + // nargs has higher priority than duplicate + if (!duplicate && checkAllAliases(key, flags.nargs)) { + duplicate = true + if ((!isUndefined(o[key]) && flags.nargs[key] === 1) || (Array.isArray(o[key]) && o[key].length === flags.nargs[key])) { + o[key] = undefined + } + } + if (value === increment) { o[key] = increment(o[key]) } else if (Array.isArray(o[key])) { diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 6106e953..a8628261 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2200,6 +2200,16 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) + it('does not interfere with nargs', function () { + var parsed = parser('-x a b c -x o p q', { + narg: { x: 3 }, + configuration: { + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.deep.equal(['o', 'p', 'q']) + }) }) describe('flatten duplicate arrays', function () { @@ -2243,7 +2253,6 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal(['a', 'b']) }) - it('flattens duplicate array type, when argument uses dot notation', function () { var parsed = parser('-x.foo a -x.foo b', { array: ['x.foo'],