Skip to content

Commit d23b473

Browse files
committed
feat: allow disabling duplicate-arguments-array
Lets you disable "duplicates" behaviour ``` yargsParser('-x 1 -x 2') // => {x: [1, 2]} ``` ``` yargsParser('-x 1 -x 2', {configuration:{'duplicate-arguments-array': false}}) // => {x: 2} ```
1 parent a491feb commit d23b473

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function parse (args, opts) {
1515
'camel-case-expansion': true,
1616
'dot-notation': true,
1717
'parse-numbers': true,
18-
'boolean-negation': true
18+
'boolean-negation': true,
19+
'duplicate-arguments-array': true
1920
}, opts.configuration)
2021
var defaults = opts.default || {}
2122
var configObjects = opts.configObjects || []
@@ -544,8 +545,10 @@ function parse (args, opts) {
544545
o[key] = value
545546
} else if (Array.isArray(o[key])) {
546547
o[key].push(value)
547-
} else {
548+
} else if (configuration['duplicate-arguments-array']) {
548549
o[key] = [ o[key], value ]
550+
} else {
551+
o[key] = value
549552
}
550553
}
551554

test/yargs-parser.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,27 @@ describe('yargs-parser', function () {
19331933
expect(parsed.dice).to.equal(undefined)
19341934
})
19351935
})
1936+
1937+
describe('duplicate arguments array', function () {
1938+
it('adds duplicate argument to array', function () {
1939+
var parsed = parser('-x a -x b', {
1940+
configuration: {
1941+
'duplicate-arguments-array': true
1942+
}
1943+
})
1944+
1945+
parsed['x'].should.deep.equal(['a', 'b'])
1946+
})
1947+
it('keeps only last argument', function () {
1948+
var parsed = parser('-x a -x b', {
1949+
configuration: {
1950+
'duplicate-arguments-array': false
1951+
}
1952+
})
1953+
1954+
parsed['x'].should.equal('b')
1955+
})
1956+
})
19361957
})
19371958

19381959
// addresses: https://github.com/yargs/yargs-parser/issues/41

0 commit comments

Comments
 (0)