Skip to content

Commit

Permalink
fix: eatNargs() for 'opt.narg === 0' and boolean typed options (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba authored and bcoe committed Jul 23, 2019
1 parent 31c204b commit c5a1db0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ function parse (args, opts) {
key = arg.match(/^--?(.+)/)[1]

// nargs format = '--foo a b c'
if (checkAllAliases(key, flags.nargs)) {
// should be truthy even if: flags.nargs[key] === 0
if (checkAllAliases(key, flags.nargs) !== false) {
i = eatNargs(i, key, args)
// array format = '--foo a b c'
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
i = eatArray(i, key, args)
} else {
next = flags.nargs[key] === 0 ? undefined : args[i + 1]
next = args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
next.match(negative)) &&
Expand Down Expand Up @@ -266,7 +267,8 @@ function parse (args, opts) {

if (!broken && key !== '-') {
// nargs format = '-f a b c'
if (checkAllAliases(key, flags.nargs)) {
// should be truthy even if: flags.nargs[key] === 0
if (checkAllAliases(key, flags.nargs) !== false) {
i = eatNargs(i, key, args)
// array format = '-f a b c'
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
Expand Down Expand Up @@ -347,6 +349,11 @@ function parse (args, opts) {
var ii
const toEat = checkAllAliases(key, flags.nargs)

if (toEat === 0) {
setArg(key, defaultValue(key))
return i
}

// nargs will not consume flag arguments, e.g., -abc, --foo,
// and terminates when one is observed.
var available = 0
Expand Down Expand Up @@ -747,7 +754,7 @@ function parse (args, opts) {
var toCheck = [].concat(flags.aliases[key] || [], key)

toCheck.forEach(function (key) {
if (flag[key]) isSet = flag[key]
if (flag.hasOwnProperty(key)) isSet = flag[key]
})

return isSet
Expand Down
20 changes: 11 additions & 9 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,17 @@ describe('yargs-parser', function () {
parse.should.have.property('_').and.deep.equal(['aaatrueaaa', 'moo', 'aaafalseaaa'])
})

it('should not use next value for boolean configured with zero narg', function () {
var parse = parser(['--all', 'false'], {
boolean: ['all'],
narg: {
all: 0
}
})
parse.should.have.property('all', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['false'])
it('should not use next value for boolean/number/string configured with zero narg', function () {
var parse = parser(['--bool', 'false', '--nr', '7', '--str', 'foo'], {
boolean: ['bool'],
number: ['nr'],
string: ['str'],
narg: { bool: 0, nr: 0, str: 0 }
})
parse.should.have.property('bool', true).and.be.a('boolean')
parse.should.have.property('nr', undefined).and.be.a('undefined')
parse.should.have.property('str', '').and.be.a('string')
parse.should.have.property('_').and.deep.equal(['false', 7, 'foo'])
})

it('should allow defining options as boolean in groups', function () {
Expand Down

0 comments on commit c5a1db0

Please sign in to comment.