Skip to content

Commit f6e6599

Browse files
pvdlgbcoe
authored andcommitted
fix: do not set boolean flags if not defined in argv (#119)
BREAKING CHANGE: `boolean` flags defined without a `default` value will now behave like other option type and won't be set in the parsed results when the user doesn't set the corresponding CLI arg. Previous behavior: ```js var parse = require('yargs-parser'); parse('--flag', {boolean: ['flag']}); // => { _: [], flag: true } parse('--no-flag', {boolean: ['flag']}); // => { _: [], flag: false } parse('', {boolean: ['flag']}); // => { _: [], flag: false } ``` New behavior: ```js var parse = require('yargs-parser'); parse('--flag', {boolean: ['flag']}); // => { _: [], flag: true } parse('--no-flag', {boolean: ['flag']}); // => { _: [], flag: false } parse('', {boolean: ['flag']}); // => { _: [] } => flag not set similarly to other option type ```
1 parent 523aecd commit f6e6599

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ function parse (args, opts) {
105105
var argv = { _: [] }
106106

107107
Object.keys(flags.bools).forEach(function (key) {
108-
setArg(key, !(key in defaults) ? false : defaults[key])
109-
setDefaulted(key)
108+
if (Object.prototype.hasOwnProperty.call(defaults, key)) {
109+
setArg(key, defaults[key])
110+
setDefaulted(key)
111+
}
110112
})
111113

112114
var notFlags = []

test/yargs-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('yargs-parser', function () {
199199
boolean: ['x', 'y', 'z']
200200
})
201201
parse.should.have.property('x', true).and.be.a('boolean')
202-
parse.should.have.property('y', false).and.be.a('boolean')
202+
parse.should.not.have.property('y')
203203
parse.should.have.property('z', true).and.be.a('boolean')
204204
parse.should.have.property('_').and.deep.equal(['one', 'two', 'three'])
205205
})
@@ -1101,7 +1101,7 @@ describe('yargs-parser', function () {
11011101
})
11021102

11031103
it('should set false if no flag in arg', function () {
1104-
parser([], opts).flag.should.be.false // eslint-disable-line
1104+
expect(parser([], opts).flag).to.be.undefined // eslint-disable-line
11051105
})
11061106
})
11071107

0 commit comments

Comments
 (0)