Skip to content

Commit

Permalink
fix: default '--' to undefined when not provided; this is closer to t…
Browse files Browse the repository at this point in the history
…he array API (#90)
  • Loading branch information
bcoe committed May 1, 2017
1 parent 4a383cf commit 4e739cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ function parse (args, opts) {

var argv = { _: [] }

if (notFlagsOption) {
argv[notFlagsArgv] = []
}

Object.keys(flags.bools).forEach(function (key) {
setArg(key, !(key in defaults) ? false : defaults[key])
setDefaulted(key)
Expand Down Expand Up @@ -296,6 +292,8 @@ function parse (args, opts) {
if (!hasKey(argv, key.split('.'))) setArg(key, 0)
})

// '--' defaults to undefined.
if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []
notFlags.forEach(function (key) {
argv[notFlagsArgv].push(key)
})
Expand Down
12 changes: 12 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ var path = require('path')
describe('yargs-parser', function () {
it('should parse a "short boolean"', function () {
var parse = parser([ '-b' ])
parse.should.not.have.property('--')
parse.should.have.property('b').to.be.ok.and.be.a('boolean')
parse.should.have.property('_').with.length(0)
})

it('should parse a "long boolean"', function () {
var parse = parser('--bool')
parse.should.not.have.property('--')
parse.should.have.property('bool', true)
parse.should.have.property('_').with.length(0)
})
Expand Down Expand Up @@ -113,6 +115,16 @@ describe('yargs-parser', function () {
parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
})

it('should not populate "--" if parsing was not stopped', function () {
var parse = parser([ '-b' ])
parse.should.not.have.property('--')
})

it('should populate "--" if parsing is stopped', function () {
var parse = parser([ '-b', '--', 'foo bar' ])
parse.should.have.property('--')
})

it('should parse numbers appropriately', function () {
var argv = parser([
'-x', '1234',
Expand Down

0 comments on commit 4e739cc

Please sign in to comment.