Skip to content

Commit

Permalink
fix: duplicate-arguments-array should not interfere with nargs
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
juergba committed Mar 23, 2019
1 parent 1404f79 commit 6dae1ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions index.js
Expand Up @@ -667,6 +667,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 ((typeof o[key] === 'string' && 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])) {
Expand Down
11 changes: 10 additions & 1 deletion test/yargs-parser.js
Expand Up @@ -2189,6 +2189,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 () {
Expand Down Expand Up @@ -2232,7 +2242,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'],
Expand Down

0 comments on commit 6dae1ea

Please sign in to comment.