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

Throw error if unexpected option values are present #40

Closed
rightaway opened this issue Feb 16, 2017 · 5 comments
Closed

Throw error if unexpected option values are present #40

rightaway opened this issue Feb 16, 2017 · 5 comments

Comments

@rightaway
Copy link

The docs say By default, an exception is thrown if the user sets an unknown option (one without a valid definition). But as mentioned in #39, if your definition is { name: 'file', defaultOption:true } and command line is $ example one.js two.js three.js, file will be set to one.js.

But it should throw an error by default, because it means I've specified things on the command line that weren't expected, so I should either correct the command line or the definition rather than letting the program carry on because something could be wrong.

@75lb
Copy link
Owner

75lb commented Feb 16, 2017

yes, it will throw if an unknown option is present (e.g. --pizza or -p) but two.js and three.js are not options, they are values.

So, you're saying the module should throw if unexpected values are present. This sounds reasonable - I will investigate in more depth tonight, thanks.

@rightaway
Copy link
Author

rightaway commented Feb 16, 2017

That's right, I think it would be very useful to throw anytime something isn't expected. The main reason I'm looking for an arg parser is to protect me from my own silly mistakes (like entering something wrong on the command line, or something having changed during refactoring), which could lead to some subtle bugs.

Another thing I just noticed is that if I have an option like { name: 'arg1', type: String }, but --arg1 doesn't appear on the command line, there's no error thrown. I think it should also throw an error. If someone doesn't want that option to always appear, they could specify something like optional: true for it, and then arg1 could appear or not appear, but by default it would be expected to be there.

But if you have a boolean { name: 'arg2', type: Boolean }, then of course it couldn't be required. Because if you always want --arg2 to be specified on the command line, then you might as well omit it since arg2 will always be true in the application.

@75lb
Copy link
Owner

75lb commented Feb 16, 2017

Validation (including deciding whether an option value is required) is out of scope for this module - it's too opinionated, everyone does it their own way. This module parses the command line only.

Simple validation example here - you'll need to do it yourself.

@75lb 75lb changed the title Throw error if extra command line args specified Throw error if unexpected option values are present Feb 16, 2017
@75lb 75lb self-assigned this Aug 5, 2017
@75lb 75lb modified the milestone: v4.1 Aug 5, 2017
@75lb 75lb mentioned this issue Dec 4, 2017
@75lb 75lb removed their assignment Dec 5, 2017
@75lb
Copy link
Owner

75lb commented Jan 12, 2018

just FYI, the next version will throw by default if either an unknown option or value are found.

@75lb
Copy link
Owner

75lb commented Jan 17, 2018

Implemented in v5.0.0.

@75lb 75lb closed this as completed Jan 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants