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
Copy file name to clipboardExpand all lines: docs/options.md
+67-6Lines changed: 67 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -342,12 +342,13 @@ To have an flag pass a value to the underlying function set `flag_value`. This a
342
342
invoke(info)
343
343
```
344
344
345
-
````{note}
346
-
The `default` value is given to the underlying 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.
345
+
### How `default` and `flag_value` interact
347
346
348
-
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 underlying function receiving the `True` Python value, it will receive the `flag_value`.
347
+
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.
349
348
350
-
Which means, in example above, this option:
349
+
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 use the second form, and set `default` to the actual value you want to pass. And not use the special `True` case. This makes the code more explicit and predictable.
363
-
````
363
+
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.
364
+
365
+
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`.
366
+
367
+
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.)
404
+
405
+
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.
406
+
407
+
|`default`|`flag_value`| Not passed |`--flag` passed |
0 commit comments