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 |
|
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 oftruewith 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)