diff --git a/index.js b/index.js index 11eac1e5..1b821031 100644 --- a/index.js +++ b/index.js @@ -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)) && @@ -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) { @@ -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 @@ -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 diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 773cfe32..d5532ace 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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 () {