Skip to content

Commit

Permalink
fix: do not set boolean flags if not defined in argv
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 committed Mar 31, 2018
1 parent 523aecd commit cf299cb
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 (key in defaults) {
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 cf299cb

Please sign in to comment.