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

Negated boolean option behaves unexpectedly #531

Closed
kumar303 opened this issue Jun 21, 2016 · 2 comments
Closed

Negated boolean option behaves unexpectedly #531

kumar303 opened this issue Jun 21, 2016 · 2 comments

Comments

@kumar303
Copy link

It looks like a flag prefixed by no- has some special handling. I find this behavior unexpected. I made a script.js like this:

var argv = require('yargs')
  .option('no-reload', {
    type: 'boolean',
  })
  .argv;
console.log(argv);

When I run:

node script.js --no-reload

I expect {noReload: true} but I get {noReload: false, reload: false}

If I run it without args, like

node script.js

I expect {noReload: false} and I do get {noReload: false} but note how I do not see {reload: true}.

Is there a way to disable the special handling? At the very least, I think the last case should produce {reload: true} so I can get the proper flag value. Otherwise, I have no way to act on the user input.

This seems to be a change introduced by a recent upgrade to yargs.

@bcoe
Copy link
Member

bcoe commented Jun 21, 2016

@kumar303 most of these clever parsing tricks can be turned off in configuration; I added this flexibility to the parsing engine, because different folks have different expectations regarding these edge-cases:

https://github.com/yargs/yargs-parser#configuration
https://github.com/yargs/yargs#configuration

var parsed = parser(['--no-dice'], {
  configuration: {
    'boolean-negation': false
  }
})

@bcoe bcoe closed this as completed Jun 21, 2016
@kumar303
Copy link
Author

kumar303 commented Jun 21, 2016

ok, thanks, the configuration is what I needed.

I think it would be great to mention the boolean-negation config value here: http://yargs.js.org/docs/#methods-booleankey

I think what I described above is still a bug. In the case where --no-reload is not given on the command line, argv should contain reload: true, otherwise you have to do an awkard undefined vs false check to figure it out. I also don't understand why you get noReload: false for boolean negation when --no-reload is on the command line. It seems like that should still be set to true.

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

No branches or pull requests

2 participants