-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add .requiredOption() for mandatory options #1071
Conversation
- option name different than property name - both of option/requiredOption for negated option - add exitOverride to allow Jest to catch unexpected errors
Pretty tidy now, but I'll leave as a draft for another day in case I think of anything more. I made some improvements today after thinking on it overnight. :-) I think this will be safe to add to 4.0.0 when (if) approved since it is a separate new feature that should not affect any existing parsing. |
ToDo: add call to Edit: added calls to |
I did a little research into the help conventions for required options. There are common conventions for the short description in the usage with optional options in square brackets, but I didn't see a common convention for the longer list of options. Plan to leave it up to the user, for now. Obviously the user can change the option description itself. We also support the user customising the "usage" description, which is a good place to show required options as part of the calling signature. e.g.
An interesting lightweight idea I saw in
|
Ready for review. No rush. Feedback welcome. |
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.
👍
Thanks @abetomo I am thinking of doing another prerelease version of 4.0.0 with this change included? |
I think it can be included in 4.0.0. |
Pull Request
Problem
Some programs have options which are mandatory. This usually means they must be specified on the command line. This is not directly supported by Commander, and is a popular request. (It is often confused with options which have a required value when the option is specified.)
Issues: #230 #1038
Solution
The most popular form for the API (#1038) is to add a method called
.requiredOption()
. For simplicity it is has the same signature as.option()
and supports all the same parameters.(This new functionality is in addition to the existing support for options with a required value when the option is specified, like
'-f,--foo <requiredValue>'
.)Commander will check that each mandatory option has a value after parsing, and display an error if the option value has not been supplied. Requiring a value rather than requiring the option on the command line allows use of environment variables to optionally supply default values.