-
-
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
Rework option defaults and add preset #1652
Rework option defaults and add preset #1652
Conversation
The core of the processing dates all the way back to at least v1.1.0. For interest: v1.1.0 code setting default value (note, not including boolean):
v1.1.0 code setting new option value. Note: null and undefined val. Default value being used in multiple places. Choosing behaviour based on previous value. Tricky!
|
This PR changes the boolean behaviour with defaults, which is a breaking change. The use of defaults with boolean options has not been much documented so hopefully not in widespread use. Long version of behaviours. program
.option('--plain', 'option without a default')
.option('--default-false', 'option with a boolean default', false)
.option('--default-string', 'option with a non-boolean default', 'stringy');
program.parse(process.argv);
console.log(program.opts()); commander@2
commander@8
This PR.
And new behaviour available with preset.
|
I should probably add the preset to the usage in the help?
Edit: Done. |
This PR fixes the behaviour of optional used without an option-argument, so it works when option already has a value whether from default or from previous arguments on command line. program
.option('-o, --optional [value]')
.option('-p [value]', 'description', 'default');
// program.P = 'oops';
program.parse(process.argv);
console.log(program.opts()); Commander 8
Behaviour with this PR
|
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.
Great work!
Commander v9 has been released. |
Pull Request
Problem
No way to set value for optional used without argument .
Optional used without argument does not trigger custom processing, complicating some uses.
This area of code is a problem for adding features to the code with some difficult to understand behaviours and intentions.
Matching issue: #1648
Related: #116 #440 #928 #984 #1135 #1201 #1328 #1394
Solution
Make default consistently set just the default (initial) value for boolean, required, and optional options. Fix use of optional with a default value.
Add
Option.preset()
for explicitly setting the preset value to use instead oftrue
with a boolean option, or optional without an option argument.Using preset does trigger custom processing.
ChangeLog
.preset()
to match some previous behaviours)Option.preset()
allows specifying value/arg for option when used without option-argument (especially optional, but also boolean option)