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

requiredOption with choices #1779

Closed
firmart opened this issue Aug 27, 2022 · 6 comments
Closed

requiredOption with choices #1779

firmart opened this issue Aug 27, 2022 · 6 comments
Labels
docs README (or other docs) could be improved

Comments

@firmart
Copy link

firmart commented Aug 27, 2022

I already had

.addOption(new Option("-s, --service <service>", "Service provider to use.")
        .choices(["a", "b"])
        .default("a", "A"))

I want to make this option required. There is .requiredOption but not as powerful as .addOption (i.e. cannot specify choices). AFAIK there is no way to specify an option choices and make it required at the same time.
An .addRequiredOption would seem redundant. Would it be possible to add a .required() method to Option ?

@shadowspawn
Copy link
Collaborator

Use .makeOptionMandatory().

Background: the reason for the name change is that "required" inside the Option class has always meant the option has a required option-argument, as opposed to an optional option-argument, or no option-argument for a boolean option. When we added .requiredOption() the naming made sense at the Command level, but I felt it was too confusing to use "required" in two different ways in the Option class and used "mandatory" for the new functionality.

@shadowspawn
Copy link
Collaborator

Note: the runtime test for a required option is that it has a value after parsing. When you make your option required (mandatory) you probably want to drop the default value to make the user specify a value.

@shadowspawn shadowspawn added the docs README (or other docs) could be improved label Aug 27, 2022
@shadowspawn
Copy link
Collaborator

Related issues: #1495 #1564

@firmart
Copy link
Author

firmart commented Sep 5, 2022

@shadowspawn Thanks for the quick reply and the remarks. I have checked the code source, but completely missed that method as I kept searching for the word "required" and only found, as you said, required argument. Will keep this issue opened as it is needed to be documented.

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Sep 8, 2022
@shadowspawn
Copy link
Collaborator

Added to README in #1797

@shadowspawn
Copy link
Collaborator

Added mention to README in Commander v9.4.1

@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs README (or other docs) could be improved
Projects
None yet
Development

No branches or pull requests

2 participants