Skip to content

Commit

Permalink
fix: do not set boolean flags if not defined in argv (#119)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
pvdlg authored and bcoe committed Apr 4, 2018
1 parent 523aecd commit f6e6599
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ function parse (args, opts) {
var argv = { _: [] }

Object.keys(flags.bools).forEach(function (key) {
setArg(key, !(key in defaults) ? false : defaults[key])
setDefaulted(key)
if (Object.prototype.hasOwnProperty.call(defaults, key)) {
setArg(key, defaults[key])
setDefaulted(key)
}
})

var notFlags = []
Expand Down
4 changes: 2 additions & 2 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('yargs-parser', function () {
boolean: ['x', 'y', 'z']
})
parse.should.have.property('x', true).and.be.a('boolean')
parse.should.have.property('y', false).and.be.a('boolean')
parse.should.not.have.property('y')
parse.should.have.property('z', true).and.be.a('boolean')
parse.should.have.property('_').and.deep.equal(['one', 'two', 'three'])
})
Expand Down Expand Up @@ -1101,7 +1101,7 @@ describe('yargs-parser', function () {
})

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

Expand Down

0 comments on commit f6e6599

Please sign in to comment.