Skip to content

Commit 0f0fb2d

Browse files
laggingreflexbcoe
authored andcommitted
feat: allow multiple arrays to be provided, rather than always combining (#71)
1 parent 8267340 commit 0f0fb2d

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

index.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ 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,
20+
'flatten-duplicate-arrays': true
1921
}, opts.configuration)
2022
var defaults = opts.default || {}
2123
var configObjects = opts.configObjects || []
@@ -310,15 +312,25 @@ function parse (args, opts) {
310312
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
311313
function eatArray (i, key, args) {
312314
var start = i + 1
315+
var argsToSet = []
316+
var multipleArrayFlag = i > 0
313317
for (var ii = i + 1; ii < args.length; ii++) {
314318
if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
315319
if (ii === start) {
316320
setArg(key, defaultForType('array'))
317321
}
322+
multipleArrayFlag = true
318323
break
319324
}
320325
i = ii
321-
setArg(key, args[ii])
326+
argsToSet.push(args[ii])
327+
}
328+
if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) {
329+
setArg(key, argsToSet)
330+
} else {
331+
argsToSet.forEach(function (arg) {
332+
setArg(key, arg)
333+
})
322334
}
323335

324336
return i
@@ -539,13 +551,15 @@ function parse (args, opts) {
539551
if (value === increment) {
540552
o[key] = increment(o[key])
541553
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
542-
o[key] = Array.isArray(value) ? value : [value]
554+
o[key] = Array.isArray(value) && configuration['flatten-duplicate-arrays'] ? value : [value]
543555
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
544556
o[key] = value
545557
} else if (Array.isArray(o[key])) {
546558
o[key].push(value)
547-
} else {
559+
} else if (configuration['duplicate-arguments-array']) {
548560
o[key] = [ o[key], value ]
561+
} else {
562+
o[key] = value
549563
}
550564
}
551565

test/yargs-parser.js

+54
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,60 @@ 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+
})
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+
it('doesn\'t nests single arrays', function () {
1980+
var parsed = parser('-x a b', {
1981+
array: ['x'],
1982+
configuration: {
1983+
'flatten-duplicate-arrays': false
1984+
}
1985+
})
1986+
1987+
parsed['x'].should.deep.equal(['a', 'b'])
1988+
})
1989+
})
19361990
})
19371991

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

0 commit comments

Comments
 (0)