Skip to content

Commit

Permalink
fix: address issue with array options with array default values (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba authored and bcoe committed Oct 26, 2019
1 parent 850bbda commit f5f9e5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -389,7 +389,8 @@ function parse (args, opts) {
// for keys without value ==> argsToSet remains an empty []
// set user default value, if available
if (defaults.hasOwnProperty(key)) {
argsToSet.push(defaults[key])
let defVal = defaults[key]
argsToSet = Array.isArray(defVal) ? defVal : [defVal]
}
} else {
for (var ii = i + 1; ii < args.length; ii++) {
Expand Down
17 changes: 10 additions & 7 deletions test/yargs-parser.js
Expand Up @@ -1516,18 +1516,21 @@ describe('yargs-parser', function () {
})

it('should default argument to empty array if no value given', function () {
var result = parser(['-b'], {
array: 'b'
var result = parser(['-b', '--tag'], {
array: ['b', 'tag'],
default: { 'tag': [] }
})
result.should.have.property('b').and.deep.equal([])
result.b.should.deep.equal([])
result.tag.should.deep.equal([])
})

it('should place default of argument in array, when default provided', function () {
var result = parser(['-b'], {
array: 'b',
default: { 'b': 33 }
var result = parser(['-b', '--tag'], {
array: ['b', 'tag'],
default: { 'b': 33, 'tag': ['foo'] }
})
result.should.have.property('b').and.deep.equal([33])
result.b.should.deep.equal([33])
result.tag.should.deep.equal(['foo'])
})

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

0 comments on commit f5f9e5a

Please sign in to comment.