-
Notifications
You must be signed in to change notification settings - Fork 118
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
feat!: populate error if incompatible narg/count or array/count options are used #191
Conversation
23eeb54
to
7ec05e7
Compare
I don't like that we would silently correct strange behavior like this. I would rather that we potentially add this as a validation check in yargs itself, and raise an exception if incompatible flags are set. |
A validation check in yargs itself does not help if yargs-parser is used independently. I see two options:
IMO the validation of the user input - in this case the configuration settings - should be done before the actual parsing. Please let me know how to proceed. There will be more incompatible combinations the more configuration options we have, eg. |
@juergba we use the error object elsewhere to provide information about why a parse failed: https://github.com/yargs/yargs-parser/blob/master/index.js#L365 If a user provides configuration that's contradictory, e.g., indicating that something is an array and a counter, I think it might be worthwhile to get in the habit of populating an error object. We could do this early on in the parse process, as a way of indicating to the user that the parse may have been invalid; I think this would be better than making an effort to correct incompatible config. |
Ok, understood. I will have a look at the error object. |
@juergba the other option on my mind, if we did want to make explicit decisions like saying that tldr; I think in some cases populating an error is a good call (if the combination of config is silly), but I would love to make an effort to formally define what is "correct" behavior over time. I think this could align with some of @boneskull's interests (he'd like to define a MVP command line parser for Node.js). |
@juergba where did you land on this one, still something you'd like to see over the finish line? what do you think of populating an error? |
Populating an error is a good option. I can imagine many silly combinations of configuration settings, which can be handled that way adding case by case. |
@juergba sounds good, as long as we feel we have an approach hammered out, no rush. |
7ec05e7
to
3fee2d8
Compare
@bcoe I applied some changes, please have a look and tell me wether this is the way to go.
|
9b4abba
to
17014d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach, I think we'll gradually want to start fleshing it out into a more generic approach; one option would be that we create a configuration object in in index.js
that allows us to describe inconsistencies between different config settings, e.g.,:
const configChecks = {
counts: ['array', 'foo'],
array: ['bar']
}
I'm comfortable with landing this as a first step though.
17014d1
to
4c9af21
Compare
4c9af21
to
61958aa
Compare
Ok, I added two tests. |
@bcoe I'm not happy with this PR's title. We populate |
fixed. |
Array:
The result is incorrect, it should be: { _: [ 'foo', 'baz' ], f: 2}
Narg:
The result is incorrect, it should be: { _: [ 'foo', 'baz' ], f: 2 }
A configuration
opts.count
plusopts.array
oropts.narg
does not make sense. Nevertheless this combination can be set by the user.we deleteopts.array
/opts.narg
settings before parsingerror
with an error object (but don't throw one)