Skip to content

Commit

Permalink
fix: nargs should allow duplicates when duplicate-arguments-array=fal…
Browse files Browse the repository at this point in the history
…se (#164)
  • Loading branch information
juergba authored and bcoe committed Jun 7, 2019
1 parent 57b7883 commit 47ccb0b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
11 changes: 10 additions & 1 deletion test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit 47ccb0b

Please sign in to comment.