Skip to content
Myr

:memo: Add/extend docs and examples for param types. bool, int.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent 439fb06 · Browse files · 8 files+297 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+297 added−0 removed

  • other:+297 added−0 removed

8 files+297 added−0 removed

added:docs/src/parameter_types/bool/__init__.py
added:docs/src/parameter_types/bool/tutorial001.py+12 added
1import typer234def main(force: bool = typer.Option(False, "--force")):5 if force:6 typer.echo("Forcing operation")7 else:8 typer.echo("Not forcing")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/bool/tutorial002.py+14 added
1import typer234def main(accept: bool = typer.Option(None, "--accept/--reject")):5 if accept is None:6 typer.echo("I don't know what you want yet")7 elif accept:8 typer.echo("Accepting!")9 else:10 typer.echo("Rejecting!")111213if __name__ == "__main__":14 typer.run(main)
added:docs/src/parameter_types/bool/tutorial003.py+12 added
1import typer234def main(force: bool = typer.Option(False, "--force/--no-force", "-f/-F")):5 if force:6 typer.echo("Forcing operation")7 else:8 typer.echo("Not forcing")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/bool/tutorial004.py+12 added
1import typer234def main(in_prod: bool = typer.Option(True, " /--demo", " /-d")):5 if in_prod:6 typer.echo("Running in production")7 else:8 typer.echo("Running demo")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/number/tutorial003.py+9 added
1import typer234def main(verbose: int = typer.Option(0, "--verbose", "-v", count=True)):5 typer.echo(f"Verbose level is {verbose}")678if __name__ == "__main__":9 typer.run(main)
added:docs/tutorial/parameter-types/bool.md+190 added
1We have seen some examples of *CLI options* with `bool`, and how **Typer** creates `--something` and `--no-something` automatically.23But we can customize those names.45## Only `--force`67Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.89We can do that by specifying the exact name we want:1011```Python hl_lines="4"12{!./src/parameter_types/bool/tutorial001.py!}13```1415Now there's only a `--force` *CLI option*:1617<div class="termy">1819```console20// Check the help21$ python main.py --help2223// Notice there's only --force, we no longer have --no-force24Usage: main.py [OPTIONS]2526Options:27 --force28 --install-completion Install completion for the current shell.29 --show-completion Show completion for the current shell, to copy it or customize the installation.30 --help Show this message and exit.3132// Try it:33$ python main.py3435Not forcing3637// Now add --force38$ python main.py --force3940Forcing operation4142// And --no-force no longer exists ⛔️43$ python main.py --no-force4445Usage: main.py [OPTIONS]46Try "main.py --help" for help.4748Error: no such option: --no-force49```5051</div>5253## Alternative names5455Now let's imagine we have a *CLI option* `--accept`.5657And we want to allow setting `--accept` or the contrary, but `--no-accept` looks ugly.5859We might want to instead have `--accept` and `--reject`.6061We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:6263```Python hl_lines="4"64{!./src/parameter_types/bool/tutorial002.py!}65```6667Check it:6869<div class="termy">7071```console72// Check the help73$ python main.py --help7475// Notice the --accept / --reject76Usage: main.py [OPTIONS]7778Options:79 --accept / --reject80 --install-completion Install completion for the current shell.81 --show-completion Show completion for the current shell, to copy it or customize the installation.82 --help Show this message and exit.8384// Try it85$ python main.py8687I don't know what you want yet8889// Now pass --accept90$ python main.py --accept9192Accepting!9394// And --reject95$ python main.py --reject9697Rejecting!98```99100</div>101102## Short names103104The same way, you can declare short versions of the names for these *CLI options*.105106For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:107108```Python hl_lines="4"109{!./src/parameter_types/bool/tutorial003.py!}110```111112Check it:113114<div class="termy">115116```console117// Check the help118$ python main.py --help119120// Notice the -f, --force / -F, --no-force121Usage: main.py [OPTIONS]122123Options:124 -f, --force / -F, --no-force125 --install-completion Install completion for the current shell.126 --show-completion Show completion for the current shell, to copy it or customize the installation.127 --help Show this message and exit.128129// Try with the short name -f130$ python main.py -f131132Forcing operation133134// Try with the short name -F135$ python main.py -F136137Not forcing138```139140</div>141142## Only names for `False`143144If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.145146To do that, use a space and a single `/` and pass the negative name after:147148```Python hl_lines="4"149{!./src/parameter_types/bool/tutorial004.py!}150```151152!!! tip153 Have in mind that it's a string with a preceding space and then a `/`.154 155 So, it's `" /-S"` not `"/-S"`.156157Check it:158159<div class="termy">160161```console162// Check the help163$ python main.py --help164165// Notice the / -d, --demo166Usage: main.py [OPTIONS]167168Options:169 / -d, --demo170 --install-completion Install completion for the current shell.171 --show-completion Show completion for the current shell, to copy it or customize the installation.172 --help Show this message and exit.173174// Try it175$ python main.py176177Running in production178179// Now pass --demo180$ python main.py --demo181182Running demo183184// And the short version185$ python main.py -d186187Running demo188```189190</div>
modified:docs/tutorial/parameter-types/number.md+48 added