mirror of
https://github.com/jbranchaud/til
synced 2026-09-01 01:01:46 +00:00
Add Specify Parameter Name For Click Option as a Python TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1871 TILs and counting..._
|
||||
_1872 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1131,6 +1131,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Skip Specific Pytest Test Cases](python/skip-specific-pytest-test-cases.md)
|
||||
- [Sort A List Of Dataclass Instances](python/sort-a-list-of-dataclass-instances.md)
|
||||
- [Sort Normalized Version Of Data](python/sort-normalized-version-of-data.md)
|
||||
- [Specify Parameter Name For Click Option](python/specify-parameter-name-for-click-option.md)
|
||||
- [Start Jupyter Notebook With Extra Packages](python/start-jupyter-notebook-with-extra-packages.md)
|
||||
- [Start The Debugger When A Test Errors](python/start-the-debugger-when-a-test-errors.md)
|
||||
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
# Specify Parameter Name For Click Option
|
||||
|
||||
[Click](https://click.palletsprojects.com/en/stable/)'s option decorator
|
||||
provides a versatile way to define flags for a command. It has good defaults
|
||||
that minimize the aspects of a flag that I need to be explicit about.
|
||||
|
||||
For example, a boolean `--init` flag for the `config` command could be specified
|
||||
like so:
|
||||
|
||||
```python
|
||||
@cli.command()
|
||||
@click.option(
|
||||
"--init",
|
||||
help="Initialize a config file with minimal defaults",
|
||||
is_flag=True,
|
||||
)
|
||||
@pass_cli
|
||||
def config(cli_ctx: CliContext, init: bool):
|
||||
# ...
|
||||
```
|
||||
|
||||
Notice, in particular, that the flag string (`--init`) that I pass as the first
|
||||
argument to `@click.option` has to correspond to the name of the parameter
|
||||
`init`. Click passes all the defined options as keyword arguments when invoking
|
||||
`config`. If the `init` parameter was changed to `initial`, there would be a
|
||||
runtime error like this: `TypeError: config() got an unexpected keyword argument 'init'`.
|
||||
|
||||
Like I said though, Click is flexible when I need it to be. I can leave the flag
|
||||
name as it is, but specify a different name to be used for the function
|
||||
parameter. I found this useful when I realized that as I added support for a
|
||||
`--json` flag I was inadvertently superseding the `json` import.
|
||||
|
||||
The second positional argument to `@click.option` can be included to rename that
|
||||
parameter:
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
"--json",
|
||||
"use_json",
|
||||
help="Output all info details in JSON format",
|
||||
is_flag=True,
|
||||
)
|
||||
@pass_cli
|
||||
def info(cli_ctx: CliContext, use_json: bool):
|
||||
# ...
|
||||
|
||||
if use_json:
|
||||
click.echo(json.dumps(info_details, indent=2))
|
||||
```
|
||||
|
||||
Both of these code blocks are excerpts from my [`py-vmt` time tracker project](https://github.com/jbranchaud/py-vmt).
|
||||
Reference in New Issue
Block a user