Skip to content

Commit f5f9e5a

Browse files
juergbabcoe
authored andcommitted
fix: address issue with array options with array default values (#206)
1 parent 850bbda commit f5f9e5a

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ function parse (args, opts) {
389389
// for keys without value ==> argsToSet remains an empty []
390390
// set user default value, if available
391391
if (defaults.hasOwnProperty(key)) {
392-
argsToSet.push(defaults[key])
392+
let defVal = defaults[key]
393+
argsToSet = Array.isArray(defVal) ? defVal : [defVal]
393394
}
394395
} else {
395396
for (var ii = i + 1; ii < args.length; ii++) {

test/yargs-parser.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1516,18 +1516,21 @@ describe('yargs-parser', function () {
15161516
})
15171517

15181518
it('should default argument to empty array if no value given', function () {
1519-
var result = parser(['-b'], {
1520-
array: 'b'
1519+
var result = parser(['-b', '--tag'], {
1520+
array: ['b', 'tag'],
1521+
default: { 'tag': [] }
15211522
})
1522-
result.should.have.property('b').and.deep.equal([])
1523+
result.b.should.deep.equal([])
1524+
result.tag.should.deep.equal([])
15231525
})
15241526

15251527
it('should place default of argument in array, when default provided', function () {
1526-
var result = parser(['-b'], {
1527-
array: 'b',
1528-
default: { 'b': 33 }
1528+
var result = parser(['-b', '--tag'], {
1529+
array: ['b', 'tag'],
1530+
default: { 'b': 33, 'tag': ['foo'] }
15291531
})
1530-
result.should.have.property('b').and.deep.equal([33])
1532+
result.b.should.deep.equal([33])
1533+
result.tag.should.deep.equal(['foo'])
15311534
})
15321535

15331536
it('should place value of argument in array, when one argument provided', function () {

0 commit comments

Comments
 (0)