You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reconcile default value passing and default activation
Fix#3111
Reintroduce a commit pushed by mistake in 6c4a77b and reverted in bb7be1f
Refine tests introduced in 06847da for #3030
Document the interaction between `default` and `flag_value`
Copy file name to clipboardExpand all lines: docs/options.md
+70-5Lines changed: 70 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -341,12 +341,13 @@ To have a flag pass a value to the decorated function set `flag_value`. This aut
341
341
invoke(info)
342
342
```
343
343
344
-
````{note}
345
-
The `default` value is given to the decorated function as-is. So if you set `default=None`, the value passed to the function is the `None` Python value. Same for any other type.
344
+
### How `default` and `flag_value` interact
346
345
347
-
But there is a special case for flags. If a flag has a `flag_value`, then setting `default=True` is interpreted as *the flag should be activated by default*. So instead of the decorated function receiving the `True` Python value, it will receive the `flag_value`.
346
+
The `default` value is given to the underlying function as-is. So if you set `default=None`, the function receives `None`. Same for any other type.
348
347
349
-
Which means, in example above, this option:
348
+
But there is a special case for **non-boolean** flags: if a flag has a non-boolean `flag_value` (like a string or a class), then `default=True` is interpreted as *the flag should be activated by default*. The function receives the `flag_value`, not the Python `True`.
Because the two are equivalent, it is recommended to always set `default` to the actual value you want to pass and avoid using the special `True` case. This makes the code more explicit and predictable.
362
+
Because the two are equivalent, it is recommended to always use the second form and set `default` to the actual value you want. This makes code more explicit and predictable.
363
+
364
+
This special case does **not** apply to boolean flags (where `flag_value` is `True` or `False`). For boolean flags, `default=True` is the literal Python value `True`.
365
+
366
+
The tables below show the value received by the function for each combination of `default`, `flag_value`, and whether the flag was passed on the command line.
For a negative flag that defaults to off, prefer the explicit pair form `--with-xyz/--without-xyz` over the single-flag `flag_value=False, default=True`:
#### Non-boolean feature switches (`flag_value` is a string, class, etc.)
405
+
406
+
For these flags, `default=True` is a **special case**: it means "activate this flag by default" and resolves to the `flag_value`. All other `default` values are passed through literally.
407
+
408
+
|`default`|`flag_value`| Not passed |`--flag` passed |
0 commit comments