Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: eatNargs() for 'opt.narg === 0' and boolean typed options #188

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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