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
+92-5Lines changed: 92 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -341,12 +341,19 @@ 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
347
+
as-is. So if you set `default=None`, the function receives
348
+
`None`. Same for any other type.
348
349
349
-
Which means, in example above, this option:
350
+
But there is a special case for **non-boolean** flags: if a
351
+
flag has a non-boolean `flag_value` (like a string or a
352
+
class), then `default=True` is interpreted as *the flag
353
+
should be activated by default*. The function receives the
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.
368
+
Because the two are equivalent, it is recommended to always
369
+
use the second form and set `default` to the actual value
370
+
you want. This makes code more explicit and predictable.
371
+
372
+
This special case does **not** apply to boolean flags (where
373
+
`flag_value` is `True` or `False`). For boolean flags,
374
+
`default=True` is the literal Python value `True`.
375
+
376
+
The tables below show the value received by the function for
377
+
each combination of `default`, `flag_value`, and whether
0 commit comments