Skip to content

Commit 4e739cc

Browse files
authored
fix: default '--' to undefined when not provided; this is closer to the array API (#90)
1 parent 4a383cf commit 4e739cc

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ function parse (args, opts) {
101101

102102
var argv = { _: [] }
103103

104-
if (notFlagsOption) {
105-
argv[notFlagsArgv] = []
106-
}
107-
108104
Object.keys(flags.bools).forEach(function (key) {
109105
setArg(key, !(key in defaults) ? false : defaults[key])
110106
setDefaulted(key)
@@ -296,6 +292,8 @@ function parse (args, opts) {
296292
if (!hasKey(argv, key.split('.'))) setArg(key, 0)
297293
})
298294

295+
// '--' defaults to undefined.
296+
if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []
299297
notFlags.forEach(function (key) {
300298
argv[notFlagsArgv].push(key)
301299
})

test/yargs-parser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ var path = require('path')
1010
describe('yargs-parser', function () {
1111
it('should parse a "short boolean"', function () {
1212
var parse = parser([ '-b' ])
13+
parse.should.not.have.property('--')
1314
parse.should.have.property('b').to.be.ok.and.be.a('boolean')
1415
parse.should.have.property('_').with.length(0)
1516
})
1617

1718
it('should parse a "long boolean"', function () {
1819
var parse = parser('--bool')
20+
parse.should.not.have.property('--')
1921
parse.should.have.property('bool', true)
2022
parse.should.have.property('_').with.length(0)
2123
})
@@ -113,6 +115,16 @@ describe('yargs-parser', function () {
113115
parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
114116
})
115117

118+
it('should not populate "--" if parsing was not stopped', function () {
119+
var parse = parser([ '-b' ])
120+
parse.should.not.have.property('--')
121+
})
122+
123+
it('should populate "--" if parsing is stopped', function () {
124+
var parse = parser([ '-b', '--', 'foo bar' ])
125+
parse.should.have.property('--')
126+
})
127+
116128
it('should parse numbers appropriately', function () {
117129
var argv = parser([
118130
'-x', '1234',

0 commit comments

Comments
 (0)