Skip to content

Commit 41ed4bc

Browse files
committed
Document the interaction between default and flag_value
1 parent d890914 commit 41ed4bc

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

docs/options.md

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,13 @@ To have an flag pass a value to the underlying function set `flag_value`. This a
342342
invoke(info)
343343
```
344344

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
347346

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.
349348

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`.
350+
351+
Which means, in the example above, this option:
351352

352353
```python
353354
@click.option('--upper', 'transformation', flag_value='upper', default=True)
@@ -359,8 +360,68 @@ is equivalent to:
359360
@click.option('--upper', 'transformation', flag_value='upper', default='upper')
360361
```
361362

362-
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.
368+
369+
#### Boolean flags (`is_flag=True`, boolean `flag_value`)
370+
371+
These are flags where `flag_value` is `True` or `False`. The `default` value is always passed through literally — no special substitution.
372+
373+
| `default` | `flag_value` | Not passed | `--flag` passed |
374+
|-----------|-------------|------------|-----------------|
375+
| *(unset)* | *(unset)* | `False` | `True` |
376+
| `True` | *(unset)* | `True` | `True` |
377+
| `False` | *(unset)* | `False` | `True` |
378+
| `None` | *(unset)* | `None` | `True` |
379+
| `True` | `True` | `True` | `True` |
380+
| `True` | `False` | `True` | `False` |
381+
| `False` | `True` | `False` | `True` |
382+
| `False` | `False` | `False` | `False` |
383+
| `None` | `True` | `None` | `True` |
384+
| `None` | `False` | `None` | `False` |
385+
386+
```{tip}
387+
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`:
388+
389+
@click.option('--with-xyz/--without-xyz', 'enable_xyz', default=True)
390+
```
391+
392+
#### Boolean flag pairs (`--flag/--no-flag`)
393+
394+
These use secondary option names to provide both an on and off switch. The `default` value is always literal.
395+
396+
| `default` | Not passed | `--flag` | `--no-flag` |
397+
|-----------|------------|----------|-------------|
398+
| *(unset)* | `False` | `True` | `False` |
399+
| `True` | `True` | `True` | `False` |
400+
| `False` | `False` | `True` | `False` |
401+
| `None` | `None` | `True` | `False` |
402+
403+
#### 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 |
408+
|------------|-------------|-------------|-----------------|
409+
| *(unset)* | `"upper"` | `None` | `"upper"` |
410+
| `True` | `"upper"` | `"upper"` * | `"upper"` |
411+
| `"lower"` | `"upper"` | `"lower"` | `"upper"` |
412+
| `None` | `"upper"` | `None` | `"upper"` |
413+
414+
\* `default=True` is substituted with `flag_value`.
415+
416+
#### Feature switch groups (multiple flags sharing one variable)
417+
418+
When multiple `flag_value` options target the same parameter name, `default=True` on one of them marks it as the default choice.
419+
420+
| Definition | Not passed | `--upper` | `--lower` |
421+
|------------|------------|-----------|-----------|
422+
| `--upper` with `flag_value='upper'`, `default=True` | `"upper"` | `"upper"` | `"lower"` |
423+
| `--upper` with `flag_value='upper'`, `default='upper'` | `"upper"` | `"upper"` | `"lower"` |
424+
| Both without `default` | `None` | `"upper"` | `"lower"` |
364425

365426
## Values from Environment Variables
366427

0 commit comments

Comments
 (0)