🐛 Update type annotations for `show_default` parameter and update docs for setting a "Custom default string" (#501)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Patrick Lannigan · committed by GitHub · parent cf61871 · Browse files · 5 files·+73 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
- Co-authored-by: svlandeg <svlandeg@github.com>
Diff stats
5 files+73 added−4 removed
- other:+73 added−4 removed
Provenance
- Commit
caac29f- Tree
07c3ba3- Author
Patrick Lannigan <p.lannigan@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
cf61871
5 files changed+73 added−4 removed
modified:docs/tutorial/options/help.md+42 added
166 unmodified linesExpand all167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212166 unmodified linesExpand all!!! note "Technical Details" 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>added:docs_src/options/help/tutorial004_an.py+14 added
1+import typer2+from typing_extensions import Annotated3+4+5+def main(6+ fullname: Annotated[7+ str, typer.Option(show_default="Deadpoolio the amazing's name")8+ ] = "Wade Wilson"9+):10+ print(f"Hello {fullname}")11+12+13+if __name__ == "__main__":14+ typer.run(main)added:docs_src/options/help/tutorial004.py+13 added
1+import typer2+3+4+def main(5+ fullname: str = typer.Option(6+ "Wade Wilson", show_default="Deadpoolio the amazing's name"7+ )8+):9+ print(f"Hello {fullname}")10+11+12+if __name__ == "__main__":13+ typer.run(main)modified:typer/models.py+1 added-1 removed
295 unmodified lines296 # Custom type297 parser: Optional[Callable[[str], Any]] = None,298 click_type: Optional[click.ParamType] = None,299 # Option300- show_default: bool = True,300+ show_default: Union[bool, str] = True,301 prompt: Union[bool, str] = False,302 confirmation_prompt: bool = False,303 prompt_required: bool = True,304 hide_input: bool = False,219 unmodified linesmodified:typer/params.py+3 added-3 removed
28 unmodified lines29 default_factory: Optional[Callable[[], Any]] = None,30 # Custom type31 parser: Optional[Callable[[str], Any]] = None,32 # Option33- show_default: bool = True,33+ show_default: Union[bool, str] = True,34 prompt: Union[bool, str] = False,35 confirmation_prompt: bool = False,36 prompt_required: bool = True,37 hide_input: bool = False,55 unmodified lines93 default_factory: Optional[Callable[[], Any]] = None,94 # Custom type95 click_type: Optional[click.ParamType] = None,96 # Option97- show_default: bool = True,97+ show_default: Union[bool, str] = True,98 prompt: Union[bool, str] = False,99 confirmation_prompt: bool = False,100 prompt_required: bool = True,101 hide_input: bool = False,54 unmodified lines156 # Custom type157 parser: Optional[Callable[[str], Any]] = None,158 click_type: Optional[click.ParamType] = None,159 # Option160- show_default: bool = True,160+ show_default: Union[bool, str] = True,161 prompt: Union[bool, str] = False,162 confirmation_prompt: bool = False,163 prompt_required: bool = True,164 hide_input: bool = False,304 unmodified lines