Skip to content

Commit

Permalink
explain in docs how to use the str version of show_default for typer.…
Browse files Browse the repository at this point in the history
…Option
  • Loading branch information
svlandeg committed Mar 18, 2024
1 parent 4eb4d7f commit 2197f6d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/tutorial/options/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,45 @@ Options:
In Click applications the default values are hidden by default. 🙈

In **Typer** these default values are shown by default. 👀

## Custom default string

You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:

=== "Python 3.6+"

```Python hl_lines="7"
{!> ../docs_src/options/help/tutorial004_an.py!}
```

=== "Python 3.6+ non-Annotated"

!!! tip
Prefer to use the `Annotated` version if possible.

```Python hl_lines="6"
{!> ../docs_src/options/help/tutorial004.py!}
```

And it will be used in the help text:

<div class="termy">

```console
$ python main.py

Hello Wade Wilson

// Show the help
$ python main.py --help

Usage: main.py [OPTIONS]

Options:
--fullname TEXT [default: (Deadpoolio the amazing's name)]
--help Show this message and exit.

// Notice how it shows "(Deadpoolio the amazing's name)" instead of the actual default of "Wade Wilson"
```

</div>
13 changes: 13 additions & 0 deletions docs_src/options/help/tutorial004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typer


def main(
fullname: str = typer.Option(
"Wade Wilson", show_default="Deadpoolio the amazing's name"
)
):
print(f"Hello {fullname}")


if __name__ == "__main__":
typer.run(main)
14 changes: 14 additions & 0 deletions docs_src/options/help/tutorial004_an.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import typer
from typing_extensions import Annotated


def main(
fullname: Annotated[
str, typer.Option(show_default="Deadpoolio the amazing's name")
] = "Wade Wilson"
):
print(f"Hello {fullname}")


if __name__ == "__main__":
typer.run(main)

0 comments on commit 2197f6d

Please sign in to comment.