Refactor the part of .addOption() responsible for storing the default value#1961
Refactor the part of .addOption() responsible for storing the default value#1961aweebit wants to merge 1 commit intotj:developfrom
.addOption() responsible for storing the default value#1961Conversation
Eliminate excessive ._findOption() calls and undefined checks. Has the tiny side effect of overwriting the default value of a negatable option when its negating component prefixed with "--no-" is added if that component has an explicitly specified default value.
I wonder whether you discovered there was a reason for the For historical interest: the PR to make combination of positive and negative options actually sensible was #795. This is where the bonus |
The comment says it all: // --no-foo is special and defaults foo to true, unless a --foo option is already definedSo I have eliminated the call in the case when a default value is explicitly specified for the negating component, but if it's not, the call is still necessary so that |
That is not the behaviour I decided on and documented. The README says:
I deliberately treated the implicit default of true and an explicit user-supplied default the same way, and followed the existing behaviour for the implicit default. The default is applied if negated option specified alone, and it is ignored if the positive option comes first. |
|
The current behaviour is By Design, so there would need to be a compelling case for a change in behaviour. |
Disclaimer: The change introduced here is of low importance.
The change has been manually cherry-picked from the now-closed #1915 so that it can be discussed independently.
Problem
When a default value is explicitly specified for the negating component of a negatable option (the one whose long flag is prefixed with
--no-), it should be set as the default value for the option just like in case of the non-negating component (or a regular, non-negatable option).That is exactly what happens in the current
.addOption()code except for one case, namely when the non-negating component had already been added earlier. To check if that is the case,._findOption()has to be called, and that seems excessive.Demo
Solution
Eliminate excessive
._findOption()calls andundefinedchecks.The change can be considered breaking because
noneis logged when running the demo program from above after it is applied, so might want to include it in the next major release. However, code similar to the demo is so unlikely to have ever been written that I don't think caring about it is worth it.