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

Boolean "swallows" values #50

Open
thiagodp opened this issue Jul 24, 2020 · 1 comment
Open

Boolean "swallows" values #50

thiagodp opened this issue Jul 24, 2020 · 1 comment

Comments

@thiagodp
Copy link

thiagodp commented Jul 24, 2020

Boolean options are considering any value as true. For example, let's consider an application that runs scripts by default and the user can filter the script to run:

{
  alias: { scriptFile: '--script-file' }, // filter the script files to run
  boolean: [ 'run' ], // indented to apply `--no-run` to avoid running script files
  default: { run: true }
}

A user then make a mistake an type the following for filtering the script to run:

node example.js --run=my-script.js

That only gives { "_": [], "run": true }, that is, when I validate run for reporting the invalid syntax, its value is true and getopts lacks the incorrect value (my-script.js) - so I can't detect and report it. That makes the application run without the user knowing about the problem (and the filter will not be applied...).

Wouldn't be a better approach to assign the received value, in order to be possible to validate it? It would still pass an if test (as true).

@jorgebucaran
Copy link
Owner

jorgebucaran commented Jul 24, 2020

@thiagodp Hmm, yeah, it might be more useful to assign the value to the option and leave further parsing to the user in the case of boolean flags, that way users can throw an error or do whatever they want. Parsing rules wouldn't change, e.g., we'd still handle immediately adjacent non-flag arguments as operands (instead of values).

Related: #30

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