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
+25-4Lines changed: 25 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -314,7 +314,7 @@ If you want to define an alias for the second option only, then you will need to
314
314
315
315
## Flag Value
316
316
317
-
To have an flag pass a value to the underlying function set `flag_value`. This automatically sets `is_flag=True`. To set a default flag, set `default=True`. Setting flag values can be used to create patterns like this:
317
+
To have an flag pass a value to the underlying function set `flag_value`. This automatically sets `is_flag=True`. To mark the flag as default, set `default=True`. Setting flag values can be used to create patterns like this:
318
318
319
319
```{eval-rst}
320
320
.. click:example::
@@ -335,6 +335,26 @@ To have an flag pass a value to the underlying function set `flag_value`. This a
335
335
invoke(info)
336
336
```
337
337
338
+
````{note}
339
+
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.
340
+
341
+
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`.
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.
356
+
````
357
+
338
358
## Values from Environment Variables
339
359
340
360
To pass in a value in from a specific environment variable use `envvar`.
@@ -386,10 +406,11 @@ Here are the rules used to parse environment variable values for flag options:
386
406
- If the flag option has a `flag_value` argument, passing that value in the environment variable will activate the flag, in addition to all the cases described above
387
407
- Any other value is interpreted as deactivating the flag
388
408
389
-
.. caution::
390
-
For boolean flags with a pair of values, the only recognized environment variable is the one provided to the `envvar` argument.
409
+
```{caution}
410
+
For boolean flags with a pair of values, the only recognized environment variable is the one provided to the `envvar` argument.
391
411
392
-
So an option defined as `--flag\--no-flag`, with a `envvar="FLAG"` parameter, there is no magical `NO_FLAG=<anything>` variable that is recognized. Only the `FLAG=<anything>` environment variable is recognized.
412
+
So an option defined as `--flag\--no-flag`, with a `envvar="FLAG"` parameter, there is no magical `NO_FLAG=<anything>` variable that is recognized. Only the `FLAG=<anything>` environment variable is recognized.
413
+
```
393
414
394
415
Once the status of the flag has been determine to be activated or not, the `flag_value` is used as the value of the flag if it is activated. If the flag is not activated, the value of the flag is set to `None` by default.
0 commit comments