Skip to content

Commit 0ae7fcb

Browse files
coreyfarrellbcoe
authored andcommitted
feat: support boolean which do not consume next argument. (#171)
1 parent f184308 commit 0ae7fcb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function parse (args, opts) {
186186
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
187187
i = eatArray(i, key, args)
188188
} else {
189-
next = args[i + 1]
189+
next = flags.nargs[key] === 0 ? undefined : args[i + 1]
190190

191191
if (next !== undefined && (!next.match(/^-/) ||
192192
next.match(negative)) &&

test/yargs-parser.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ describe('yargs-parser', function () {
194194
parse.should.have.property('_').and.deep.equal(['aaatrueaaa', 'moo', 'aaafalseaaa'])
195195
})
196196

197+
it('should not use next value for boolean configured with zero narg', function () {
198+
var parse = parser(['--all', 'false'], {
199+
boolean: ['all'],
200+
narg: {
201+
all: 0
202+
}
203+
})
204+
parse.should.have.property('all', true).and.be.a('boolean')
205+
parse.should.have.property('_').and.deep.equal(['false'])
206+
})
207+
197208
it('should allow defining options as boolean in groups', function () {
198209
var parse = parser([ '-x', '-z', 'one', 'two', 'three' ], {
199210
boolean: ['x', 'y', 'z']

0 commit comments

Comments
 (0)