-
-
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
add choices to arguments #1458
Comments
No, choices for command-arguments are not supported. (But an alternative is to make |
Is it intentionally not supported or is it in the backlog? If it's in the backlog we can discuss about the api and maybe i could create a PR |
At this stage it is a new suggestion. I see Do any other CLI frameworks support any of these for command-arguments? |
I had a quick look at argparse, and it configures command-arguments and option-arguments the same way:
Looks like also support in yargs via positional, which supports choices: This is very different to how Commander currently works! |
Previous related issue: #971 |
My first intuition was that arguments should be treated the same as options in terms of validation and api, so it will be quite natural to have btw, just as a side note, you need to be aware that commander becomes (I would say even already) the top of his kind (at least among js tools), so soon you will not be able to check other solutions for understanding the standard, commander is on the point where it can define the standard:) |
I think following the option syntax may work pretty well, allowing description in a more consistent way, and defaults and coercion, and the extensible syntax to allow choices. Specifying the arguments with // arguments and description
oldProgram
.arguments('<username> [password]')
.description('test command', {
username: 'user to login',
password: 'password for user, if required'
});
newProgram
.description('test command')
.argument('<username>', 'user to login')
.argument('[password]', 'password for user, if required');
// default
cmdUsingOpt
.option('-c, --cheese <type>', 'add the specified type of cheese', 'blue');
cmdUsingArg
.argument('[cheese-type]', 'add the specified type of cheese', 'blue')
// coerce
cmdUsingOpt
.option('-f, --float <number>', 'float argument', parseFloat)
cmdUsingArg
.argument('<number>', 'float argument', parseFloat)
// choices
cmdUsingOpt
.addOption(new Option('-d, --drink <size>', 'drink size').choices(['small', 'medium', 'large']));
cmdUsingArg
.addArgument(new CommandArgument('<drink-size>', 'drink size').choices(['small', 'medium', 'large'])); |
what is/are the correct test suite for testing the arguments? I want to duplicate all the relevant tests and make sure they are passing with the new syntax |
Be warned: I am not sure yet whether there is enough value to make what I suspect will be a lot of code changes. I have not seen many requests for the features this would add, so will be watching to see if this issue gets many 👍 . You may want to open a PR with some code to show and discuss, but just warning you not to expect it will be accepted. Is there a particular area you are interested in experimenting with first? You did ask about choices, but that does need quite a lot of new syntax with what we are considering, so you may have to start elsewhere. There are base argument tests in:
But there are related tests in lots of other files, including the help related tests and the new |
sure, I know you have the right to reject the pr:) So I think I will start by introducing the new API without adding new capabilities like choices, and later I'll them. |
The support for defaults and custom parsing with |
Would you like to have first go at this @Niryo ? |
@shadowspawn sure. i'll notice here when I am starting to work on it (until then if anyone see this and want to start working on it, you are welcome to do so) |
I have made a start on this. |
Released in Commander v8. |
is it possible to have choices for arguments?
The text was updated successfully, but these errors were encountered: