-
-
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
Allow boolean default for flag option #987
Allow boolean default for flag option #987
Conversation
To clarify one breaking aspect of this change. There is a subtle and I think undocumented behaviour I only discovered working through this code that you can set a value for a flag to take when specified (instead of true). This may even be completely unintentional. This behaviour is unchanged by this Pull Request for a non-boolean value.
So there was an existing behaviour that these two calls were actually the same, and specifying a boolean default of true would have no affect previously, and now it will have an affect:
I think the new behaviour is reasonable, and the old behaviour is undocumented and perhaps even accidental, but the change did break one of the existing tests. |
@usmonster FYI, the future change I mentioned in #795. |
Thanks for the ping, @shadowspawn. FYI, I'd like to review this, but I probably can't before the end of the week. Regarding the old behavior you mention, I'm pretty sure it was intentional to allow any option (even flags) to have a default value specified, especially given that there's a test for it. I imagine some folks may rely on this behavior even if undocumented, but your change does make sense as well. |
Yes indeed. I actually looked up the following quote when I joined as a maintainer and have been expecting an opportunity to use it and/or suffer from it. :-)
|
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.
👍
@shadowspawn, my only concern is that I'm not sure how many users would prefer the new feature over the existing behavior, or vice-versa. If you have more data on this beyond the initial request, feel free to share. I just don't want more issues to pop up over this breaking change. |
I do not have any metrics on preferred behaviour. I do like making it convenient to have an externally supplied default that can be overridden, with From code inspection and experimentation I think the old behaviour was that a boolean default value (third parameter) was ignored, which I do see a use case for?
Old behaviour:
New behaviour. (Reminder, the intended use case is for
|
Thanks for review @abetomo. |
Thanks for the further clarification, @shadowspawn. I misunderstood the current behavior. I'm on board with this change, but will leave an inline comment or two. |
Expanded tests to have more complete coverage of intended boolean flag permutations. |
Added written description of possibility for default value for --foo/--no-foo to README. |
@@ -101,8 +101,9 @@ cheese: stilton | |||
|
|||
You can specify a boolean option long name with a leading `no-` to set the option value to false when used. | |||
Defined alone this also makes the option true by default. |
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.
In rereading this and the edits, I'm not sure we're using consistent vocabulary on this specific line; i.e. it can be unclear whether "define" (or "specify", "add", "set", etc.) means "when defining the program" or "when invoking the program via the CLI."
It could just be that the sentence is slightly awkwardly worded (and sorry to comment on something that isn't a change in this commit).
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.
I agree with the awkward and potential for misinterpretation. I have struggled with the wording a couple of times, but at least we do have examples to consult. I'm not planning to have another go in this PR.
Available now as a prerelease. See #1001 |
(Have not added anything to README yet, but code and tests as intended.)
This allows supplying default value for boolean flags, in particular for a default value when both
--foo
and--no-foo
options are defined, such as a default value from environment. Only possible after #795.Resolves #928.