Skip to content

Commit

Permalink
feat: Support boolean which do not consume next argument.
Browse files Browse the repository at this point in the history
This causes boolean arguments to honor an narg setting of zero.

Fixes #170
  • Loading branch information
coreyfarrell committed Apr 4, 2019
1 parent 1404f79 commit 9dff9e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function parse (args, opts) {
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
i = eatArray(i, key, args)
} else {
next = args[i + 1]
next = flags.nargs[key] === 0 ? undefined : args[i + 1]

if (next !== undefined && (!next.match(/^-/) ||
next.match(negative)) &&
Expand Down
11 changes: 11 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +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
}
})
parser.should.have.property('all', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['false'])
})

it('should allow defining options as boolean in groups', function () {
var parse = parser([ '-x', '-z', 'one', 'two', 'three' ], {
boolean: ['x', 'y', 'z']
Expand Down

0 comments on commit 9dff9e0

Please sign in to comment.