-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Provide Array of permitted values for an option. #518
Comments
I second this, Would be great to be able to do this and validate options. |
(I suspect that neither of these are as "easy" as OP had in mind when said "I'd like an easy way to pass an Array to an option() and have Commander automatically check that the value passed by the user is one of the permitted values."! See next two comments for what code could look like.) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Related link from #215 (comment) Python argparser has Yargs has |
Experimenting with Option approach in #1331: program
.addOption(new Option('-s, --size <value>').choices(['big', 'little'])); |
Included in v7.0.0 pre-release #1386 |
I might be late to the game, but what if you could return or throw an error in the coercion function? Then you could do any validation you want. commander would automatically generate the error message. |
Using an exception is actually the approach I took for the choice method, see
But I had not thought of adding the option name in the catch rather than the throw. That would be a solution to #1207 |
Included in Commander 7.0.0. |
Nice work @shadowspawn! I'll try and make some time to give this a go. |
@shadowspawn this works very well. The only feature missing right now is to make the newly created option required. I would suggest something like
What do you think? |
@fernetmatt I think that’s |
@lydell is correct. Went with |
Sorry for commenting here, but couldn't be there also an validator for arguments using addArgument? I also just have an array of possible values |
Hi @SrBrahma , From the README: program
.addArgument(new commander.Argument('<drink-size>', 'drink cup size').choices(['small', 'medium', 'large']))
.addArgument(new commander.Argument('[timeout]', 'timeout in seconds').default(60, 'one minute')) |
Many thanks for the quick answer! |
I have an
.option()
that is only allowed to be one of "all", "debug" or "none".I'd like an easy way to pass an
Array
to anoption()
and have Commander automatically check that the value passed by the user is one of the permitted values.Something like this:
Also including the permitted values in the help output could be useful, provided it's not a very large
Array
.I'd gladly do the PR if I can get a response from @tj or a collaborator that this is useful.
Thanks!!
The text was updated successfully, but these errors were encountered: