-
-
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
Default value for an option is always there even if flag isn't used #1394
Comments
Related: #1135 |
You didn't miss something. If you need to be able to tell if the option was on the command-line, don't specify a default value for the option. You can then handle the value for "missing" and "optional" option in code. I am not sure what you have in mind with "pre-process values"? If you can't see how to achieve what you want, would you please explain a little more about your desired workflow? |
Thank you for the quick reply! I ended up doing the following as suggested: program
[...]
.option('-p, --prettify [number|tab]', 'Prettify files (default: 4 spaces)', parseIndentation)
.option('--duplicates [strict|loose]', 'Find duplicates (default: loose)', parseMode('--duplicates'))
.option('--diff [strict|loose]', 'Find differences and conflicts (default: strict)', parseMode('--diff'))
.action((args, cmd) => {
// Inject fallback values, true will be an option without a value specified
cmd.prettify = cmd.prettify === true ? 4 : cmd.prettify;
cmd.duplicates = cmd.duplicates === true ? 'loose' : cmd.duplicates;
cmd.diff = cmd.diff === true ? 'strict' : cmd.diff;
...
})
.parse();
Now, let's take
What I could do is adding a Sorry if my first command did not properly explain my use-case, hope this cleared things up a little bit. The code snipped is taken from here. |
That is the two approaches I had in mind: handling the boolean option usage in code, or splitting the boolean and value uses of the option into two options. (Thanks for expanding!) |
I have wondered about changing the default value behaviour for boolean|value option type, but it has not come up often so far to justify what would be a big breaking change. This option type was used in the examples through Commander v2, even in cases where probably the boolean usage was not even intended, and the missing flag cases would change behaviour. |
argparse has separate values for the "default" option value, and the "const" value used when the option is specified on the command-line without an option-value. I think making the two purposes separate like this is the non-breaking way forward. https://docs.python.org/2.7/library/argparse.html#nargs
|
A work-around was reached, and no further activity in a month. Closing this as resolved. Feel free to open a new issue if it comes up again, with new information and renewed interest. |
I discovered recently that options with optional arguments are actually broken when there is a default value, making the requested behaviour difficult to achieve. The option is ignored when supplied without a value. I have PR #1652 open to fix that, and add program
.addOption(new Option('-p, --pizza [type]'', 'Pizza name').preset('pepperoni')); |
Another related feature added after the original question, Commander v8.3 added |
First of all, great library!
Consider this code:
Usage:
Is there a way to see if the
--pizza
flag is actually used? Currently the default-value is always present and used, even if the flag itself is not even present in the arguments. This makes it impossible to pre-process values and making the flag optional as at this point the flag is neither optional or mandatory, it is always there, even if not specified.Related: #440 #1036
Do I miss something or how can I achieve that using commanderjs?
The text was updated successfully, but these errors were encountered: