Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not set boolean flags if not defined in argv #119

Merged
merged 1 commit into from
Apr 4, 2018

Conversation

pvdlg
Copy link
Contributor

@pvdlg pvdlg commented Mar 31, 2018

Fix #116

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:

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:

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

@pvdlg pvdlg changed the title fix: set boolean to undefined if not set in argv ix: do not set boolean flags if not defined in argv Mar 31, 2018
@pvdlg pvdlg changed the title ix: do not set boolean flags if not defined in argv fix: do not set boolean flags if not defined in argv Mar 31, 2018
index.js Outdated
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use Object.prototype.hasOwnProperty.call, in case the "key" is "toString" or "hasOwnProperty"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept key in defaults because if was there before, but I agree we should use Object.prototype.hasOwnProperty.call. I made the change.

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
```
Copy link
Member

@bcoe bcoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvdlg thanks for digging into this; I'm currently triaging issues with @evocateur and we both agree that this is a good improvement to the API.

@bcoe bcoe merged commit f6e6599 into yargs:master Apr 4, 2018
@pvdlg
Copy link
Contributor Author

pvdlg commented Apr 4, 2018

Awesome! Thanks!

@pvdlg pvdlg deleted the default-boolean-undef branch April 4, 2018 02:06
@pvdlg
Copy link
Contributor Author

pvdlg commented Apr 4, 2018

@bcoe could you by any chance also check out yargs/yargs#1071 which is related? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants