Skip to content

Commit

Permalink
fix(parsing): handle calling short option with an empty string as the…
Browse files Browse the repository at this point in the history
… next value.
  • Loading branch information
elas7 authored and bcoe committed Apr 9, 2016
1 parent 2dbe86b commit a867165
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ function parse (args, opts) {
} else {
next = args[i + 1]

if (next && !/^(-|--)[^-]/.test(next) &&
if (next !== undefined && !/^(-|--)[^-]/.test(next) &&
!checkAllAliases(key, flags.bools) &&
!checkAllAliases(key, flags.counts)) {
setArg(key, next)
i++
} else if (next && /true|false/.test(next)) {
} else if (/true|false/.test(next)) {
setArg(key, next)
i++
} else {
Expand Down
12 changes: 12 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ describe('yargs-parser', function () {
parse.should.have.property('_').with.length(0)
})

it('should set the value of a single long option to the next supplied value, even if the value is empty', function () {
var parse = parser(['--pow', ''])
parse.should.have.property('pow', '')
parse.should.have.property('_').with.length(0)
})

it('should set the value of a single long option if an = was used', function () {
var parse = parser(['--pow=xixxle'])
parse.should.have.property('pow', 'xixxle')
Expand Down Expand Up @@ -121,6 +127,12 @@ describe('yargs-parser', function () {
argv._[0].should.be.a('number')
})

it('should set the value of a single short option to the next supplied value, even if the value is empty', function () {
var parse = parser(['-p', ''])
parse.should.have.property('p', '')
parse.should.have.property('_').with.length(0)
})

it('should not set the next value as the value of a short option if that option is explicitly defined as a boolean', function () {
var parse = parser([ '-t', 'moo' ], {
boolean: 't'
Expand Down

0 comments on commit a867165

Please sign in to comment.