Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5a1db0

Browse filesBrowse files
juergbaBenjamin E. Coe
authored andcommitted
fix: eatNargs() for 'opt.narg === 0' and boolean typed options (#188)
1 parent 31c204b commit c5a1db0
Copy full SHA for c5a1db0

File tree

Expand file treeCollapse file tree

2 files changed

+22
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+22
-13
lines changed
Open diff view settings
Collapse file

‎index.js‎

Copy file name to clipboardExpand all lines: index.js
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ function parse (args, opts) {
173173
key = arg.match(/^--?(.+)/)[1]
174174

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

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

267268
if (!broken && key !== '-') {
268269
// nargs format = '-f a b c'
269-
if (checkAllAliases(key, flags.nargs)) {
270+
// should be truthy even if: flags.nargs[key] === 0
271+
if (checkAllAliases(key, flags.nargs) !== false) {
270272
i = eatNargs(i, key, args)
271273
// array format = '-f a b c'
272274
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
@@ -347,6 +349,11 @@ function parse (args, opts) {
347349
var ii
348350
const toEat = checkAllAliases(key, flags.nargs)
349351

352+
if (toEat === 0) {
353+
setArg(key, defaultValue(key))
354+
return i
355+
}
356+
350357
// nargs will not consume flag arguments, e.g., -abc, --foo,
351358
// and terminates when one is observed.
352359
var available = 0
@@ -747,7 +754,7 @@ function parse (args, opts) {
747754
var toCheck = [].concat(flags.aliases[key] || [], key)
748755

749756
toCheck.forEach(function (key) {
750-
if (flag[key]) isSet = flag[key]
757+
if (flag.hasOwnProperty(key)) isSet = flag[key]
751758
})
752759

753760
return isSet
Collapse file

‎test/yargs-parser.js‎

Copy file name to clipboardExpand all lines: test/yargs-parser.js
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +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'])
197+
it('should not use next value for boolean/number/string configured with zero narg', function () {
198+
var parse = parser(['--bool', 'false', '--nr', '7', '--str', 'foo'], {
199+
boolean: ['bool'],
200+
number: ['nr'],
201+
string: ['str'],
202+
narg: { bool: 0, nr: 0, str: 0 }
203+
})
204+
parse.should.have.property('bool', true).and.be.a('boolean')
205+
parse.should.have.property('nr', undefined).and.be.a('undefined')
206+
parse.should.have.property('str', '').and.be.a('string')
207+
parse.should.have.property('_').and.deep.equal(['false', 7, 'foo'])
206208
})
207209

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.