Skip to content

Commit 399d93c

Browse files
committed
feat: allow not to flatten duplicate array types
1 parent d23b473 commit 399d93c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function parse (args, opts) {
1616
'dot-notation': true,
1717
'parse-numbers': true,
1818
'boolean-negation': true,
19-
'duplicate-arguments-array': true
19+
'duplicate-arguments-array': true,
20+
'flatten-duplicate-arrays': true
2021
}, opts.configuration)
2122
var defaults = opts.default || {}
2223
var configObjects = opts.configObjects || []
@@ -311,15 +312,24 @@ function parse (args, opts) {
311312
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
312313
function eatArray (i, key, args) {
313314
var start = i + 1
315+
var argsToSet = [], multipleArrayFlag = i>0
314316
for (var ii = i + 1; ii < args.length; ii++) {
315317
if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
316318
if (ii === start) {
317319
setArg(key, defaultForType('array'))
318320
}
321+
multipleArrayFlag = true
319322
break
320323
}
321324
i = ii
322-
setArg(key, args[ii])
325+
argsToSet.push(args[ii])
326+
}
327+
if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) {
328+
setArg(key, argsToSet)
329+
} else {
330+
argsToSet.forEach(function(arg){
331+
setArg(key, arg)
332+
})
323333
}
324334

325335
return i
@@ -540,7 +550,7 @@ function parse (args, opts) {
540550
if (value === increment) {
541551
o[key] = increment(o[key])
542552
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
543-
o[key] = Array.isArray(value) ? value : [value]
553+
o[key] = Array.isArray(value) && configuration['flatten-duplicate-arrays'] ? value : [value]
544554
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
545555
o[key] = value
546556
} else if (Array.isArray(o[key])) {

test/yargs-parser.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,29 @@ describe('yargs-parser', function () {
19541954
parsed['x'].should.equal('b')
19551955
})
19561956
})
1957+
1958+
describe('flatten duplicate arrays', function () {
1959+
it('flattens duplicate array type', function () {
1960+
var parsed = parser('-x a b -x c d', {
1961+
array: ['x'],
1962+
configuration: {
1963+
'flatten-duplicate-arrays': true
1964+
}
1965+
})
1966+
1967+
parsed['x'].should.deep.equal(['a', 'b', 'c', 'd'])
1968+
})
1969+
it('nests duplicate array types', function () {
1970+
var parsed = parser('-x a b -x c d', {
1971+
array: ['x'],
1972+
configuration: {
1973+
'flatten-duplicate-arrays': false
1974+
}
1975+
})
1976+
1977+
parsed['x'].should.deep.equal([['a', 'b'], ['c', 'd']])
1978+
})
1979+
})
19571980
})
19581981

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

0 commit comments

Comments
 (0)