Skip to content

Commit a867165

Browse files
elas7bcoe
authored andcommitted
fix(parsing): handle calling short option with an empty string as the next value.
1 parent 2dbe86b commit a867165

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ function parse (args, opts) {
240240
} else {
241241
next = args[i + 1]
242242

243-
if (next && !/^(-|--)[^-]/.test(next) &&
243+
if (next !== undefined && !/^(-|--)[^-]/.test(next) &&
244244
!checkAllAliases(key, flags.bools) &&
245245
!checkAllAliases(key, flags.counts)) {
246246
setArg(key, next)
247247
i++
248-
} else if (next && /true|false/.test(next)) {
248+
} else if (/true|false/.test(next)) {
249249
setArg(key, next)
250250
i++
251251
} else {

test/yargs-parser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ describe('yargs-parser', function () {
4040
parse.should.have.property('_').with.length(0)
4141
})
4242

43+
it('should set the value of a single long option to the next supplied value, even if the value is empty', function () {
44+
var parse = parser(['--pow', ''])
45+
parse.should.have.property('pow', '')
46+
parse.should.have.property('_').with.length(0)
47+
})
48+
4349
it('should set the value of a single long option if an = was used', function () {
4450
var parse = parser(['--pow=xixxle'])
4551
parse.should.have.property('pow', 'xixxle')
@@ -121,6 +127,12 @@ describe('yargs-parser', function () {
121127
argv._[0].should.be.a('number')
122128
})
123129

130+
it('should set the value of a single short option to the next supplied value, even if the value is empty', function () {
131+
var parse = parser(['-p', ''])
132+
parse.should.have.property('p', '')
133+
parse.should.have.property('_').with.length(0)
134+
})
135+
124136
it('should not set the next value as the value of a short option if that option is explicitly defined as a boolean', function () {
125137
var parse = parser([ '-t', 'moo' ], {
126138
boolean: 't'

0 commit comments

Comments
 (0)