-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
If feature switch flag is a boolean, default value isn't set correctly #2351
Comments
(Not a click developer here, just lurking around.) This behavior seems to be in click since forever. For some unknown reason click doesn't expect that you would to this with boolean flags: def get_default(
self, ctx: Context, call: bool = True
) -> t.Optional[t.Union[t.Any, t.Callable[[], t.Any]]]:
# If we're a non boolean flag our default is more complex because
# we need to look at all flags in the same group to figure out
# if we're the default one in which case we return the flag
# value as default.
if self.is_flag and not self.is_bool_flag:
for param in ctx.command.params:
if param.name == self.name and param.default:
return param.flag_value # type: ignore
return None
return super().get_default(ctx, call=call) BTW, I think the comment above is misleading. It should be: -# if we're the default one in which case we return the flag
+# which is the default one in which case we return the flag So, for booleans we get default value of the last parameter wins behavior; if you reverse the order of Perhaps it's expected that one would just use the usual: @click.option('--one/--two', 'flag', default=True) The only downside is that you get one option instead of two (or more), so YMMV. |
Can confirm the same behavior from my side (on 8.1.3). This was a little confusing when I discovered it the first time, since I had a different impression from reading the documentation. Would the project be open to doing either of these two alternatives?
|
I think this is a docs problem. Feature switches docs are unclear. I will investigate. |
Let's have two short code snippets that differ only in whether the flag values being set are bool vs int:
Running both scripts without any parameters, I would expect to get
True
and1
, but that's not what happens.This seems wrong.
Environment:
The text was updated successfully, but these errors were encountered: