Skip to content
Myr

📝 Update markdown includes to use the new simpler format (#1054)

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 GitHub · parent f802688 · Browse files · 50 files+186 added−1855 removed
This commit

State

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

Diff stats

50 files+186 added−1855 removed

  • other:+186 added−1855 removed

50 files+186 added−1855 removed

modified:docs/tutorial/arguments/default.md+2 added-42 removed
modified:docs/tutorial/arguments/envvar.md+3 added-63 removed
8 unmodified lines
9///1011To do that, use the `envvar` parameter for `typer.Argument()`:1213//// tab | Python 3.7+13{* docs_src/arguments/envvar/tutorial001_an.py hl[5] *}1415```Python hl_lines="5"16{!> ../docs_src/arguments/envvar/tutorial001_an.py!}17```1819////2021//// tab | Python 3.7+ non-Annotated2223/// tip2425Prefer to use the `Annotated` version if possible.2627///2829```Python hl_lines="4"30{!> ../docs_src/arguments/envvar/tutorial001.py!}31```3233////3415In this case, the *CLI argument* `name` will have a default value of `"World"`, but will also read any value passed to the environment variable `AWESOME_NAME` if no value is provided in the command line:1617<div class="termy">18
35 unmodified lines
54## Multiple environment variables5556You are not restricted to a single environment variable, you can declare a list of environment variables that could be used to get a value if it was not passed in the command line:5778//// tab | Python 3.7+58{* docs_src/arguments/envvar/tutorial002_an.py hl[6] *}5980```Python hl_lines="6"81{!> ../docs_src/arguments/envvar/tutorial002_an.py!}82```8384////8586//// tab | Python 3.7+ non-Annotated8788/// tip8990Prefer to use the `Annotated` version if possible.9192///9394```Python hl_lines="4"95{!> ../docs_src/arguments/envvar/tutorial002.py!}96```9798////9960Check it:6162<div class="termy">63
25 unmodified lines
89## Hide an env var from the help text9091By default, environment variables used will be shown in the help text, but you can disable them with `show_envvar=False`:92133//// tab | Python 3.7+93{* docs_src/arguments/envvar/tutorial003_an.py hl[7] *}94135```Python hl_lines="7"136{!> ../docs_src/arguments/envvar/tutorial003_an.py!}137```138139////140141//// tab | Python 3.7+ non-Annotated142143/// tip144145Prefer to use the `Annotated` version if possible.146147///148149```Python hl_lines="4"150{!> ../docs_src/arguments/envvar/tutorial003.py!}151```152153////15495Check it:9697<div class="termy">98
28 unmodified lines
modified:docs/tutorial/arguments/help.md+9 added-171 removed
2 unmodified lines
3In the *First Steps* section you saw how to add help for a CLI app/command by adding it to a function's <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.45Here's how that last example looked like:67```Python8{!../docs_src/first_steps/tutorial006.py!}9```7{* docs_src/first_steps/tutorial006.py *}89Now that you also know how to use `typer.Argument()`, let's use it to add documentation specific for a *CLI argument*.1011## Add a `help` text for a *CLI argument*1213You can use the `help` parameter to add a help text for a *CLI argument*:1417//// tab | Python 3.7+15{* docs_src/arguments/help/tutorial001_an.py hl[5] *}1619```Python hl_lines="5"20{!> ../docs_src/arguments/help/tutorial001_an.py!}21```2223////2425//// tab | Python 3.7+ non-Annotated2627/// tip2829Prefer to use the `Annotated` version if possible.3031///3233```Python hl_lines="4"34{!> ../docs_src/arguments/help/tutorial001.py!}35```3637////3817And it will be used in the automatic `--help` option:1819<div class="termy">20
15 unmodified lines
36## Combine help text and docstrings3738And of course, you can also combine that `help` with the <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>:3962//// tab | Python 3.7+40{* docs_src/arguments/help/tutorial002_an.py hl[5:8] *}4164```Python hl_lines="5-8"65{!> ../docs_src/arguments/help/tutorial002_an.py!}66```6768////6970//// tab | Python 3.7+ non-Annotated7172/// tip7374Prefer to use the `Annotated` version if possible.7576///7778```Python hl_lines="4-7"79{!> ../docs_src/arguments/help/tutorial002.py!}80```8182////8342And the `--help` option will combine all the information:4344<div class="termy">45
17 unmodified lines
63## Help with defaults6465If you have a *CLI argument* with a default value, like `"World"`:66109//// tab | Python 3.7+67{* docs_src/arguments/help/tutorial003_an.py hl[5] *}68111```Python hl_lines="5"112{!> ../docs_src/arguments/help/tutorial003_an.py!}113```114115////116117//// tab | Python 3.7+ non-Annotated118119/// tip120121Prefer to use the `Annotated` version if possible.122123///124125```Python hl_lines="4"126{!> ../docs_src/arguments/help/tutorial003.py!}127```128129////13069It will show that default value in the help text:7071<div class="termy">72
15 unmodified lines
88</div>8990But you can disable that if you want to, with `show_default=False`:91154//// tab | Python 3.7+92{* docs_src/arguments/help/tutorial004_an.py hl[7] *}93156```Python hl_lines="7"157{!> ../docs_src/arguments/help/tutorial004_an.py!}158```159160////161162//// tab | Python 3.7+ non-Annotated163164/// tip165166Prefer to use the `Annotated` version if possible.167168///169170```Python hl_lines="4"171{!> ../docs_src/arguments/help/tutorial004.py!}172```173174////17594And then it won't show the default value:9596<div class="termy">97
25 unmodified lines
123## Custom default string124125You 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:126209//// tab | Python 3.7+127{* docs_src/arguments/help/tutorial005_an.py hl[9] *}128211```Python hl_lines="9"212{!> ../docs_src/arguments/help/tutorial005_an.py!}213```214215////216217//// tab | Python 3.7+ non-Annotated218219/// tip220221Prefer to use the `Annotated` version if possible.222223///224225```Python hl_lines="6"226{!> ../docs_src/arguments/help/tutorial005.py!}227```228229////230129And it will be used in the help text:130131<div class="termy">132
35 unmodified lines
168But you can customize it with the `metavar` parameter for `typer.Argument()`.169170For example, let's say you don't want to have the default of `NAME`, you want to have `username`, in lowercase, and you really want ✨ emojis ✨ everywhere:171274//// tab | Python 3.7+172{* docs_src/arguments/help/tutorial006_an.py hl[5] *}173276```Python hl_lines="5"277{!> ../docs_src/arguments/help/tutorial006_an.py!}278```279280////281282//// tab | Python 3.7+ non-Annotated283284/// tip285286Prefer to use the `Annotated` version if possible.287288///289290```Python hl_lines="4"291{!> ../docs_src/arguments/help/tutorial006.py!}292```293294////295174Now the generated help text will have `✨username✨` instead of `NAME`:175176<div class="termy">177
16 unmodified lines
194You might want to show the help information for *CLI arguments* in different panels when using the `--help` option.195196If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel where you want this *CLI argument* to be shown:197320//// tab | Python 3.7+198{* docs_src/arguments/help/tutorial007_an.py hl[8,12] *}199322```Python hl_lines="8 12"323{!> ../docs_src/arguments/help/tutorial007_an.py!}324```325326////327328//// tab | Python 3.7+ non-Annotated329330/// tip331332Prefer to use the `Annotated` version if possible.333334///335336```Python hl_lines="7 10"337{!> ../docs_src/arguments/help/tutorial007.py!}338```339340////341200Then, if you check the `--help` option, you will see a default panel named "`Arguments`" for the *CLI arguments* that don't have a custom `rich_help_panel`.201202And next you will see other panels for the *CLI arguments* that have a custom panel set in the `rich_help_panel` parameter:203
33 unmodified lines
237If you want, you can make a *CLI argument* **not** show up in the `Arguments` section in the help text.238239You will probably not want to do this normally, but it's possible:240383//// tab | Python 3.7+241{* docs_src/arguments/help/tutorial008_an.py hl[5] *}242385```Python hl_lines="5"386{!> ../docs_src/arguments/help/tutorial008_an.py!}387```388389////390391//// tab | Python 3.7+ non-Annotated392393/// tip394395Prefer to use the `Annotated` version if possible.396397///398399```Python hl_lines="4"400{!> ../docs_src/arguments/help/tutorial008.py!}401```402403////404243Check it:244245<div class="termy">246
47 unmodified lines
modified:docs/tutorial/arguments/optional.md+6 added-18 removed
33 unmodified lines
34## An alternative *CLI argument* declaration3536In the [First Steps](../first-steps.md#add-a-cli-argument){.internal-link target=_blank} you saw how to add a *CLI argument*:3738```Python hl_lines="4"39{!../docs_src/first_steps/tutorial002.py!}40```38{* docs_src/first_steps/tutorial002.py hl[4] *}3940Now let's see an alternative way to create the same *CLI argument*:414245```Python hl_lines="5"46{!> ../docs_src/arguments/optional/tutorial001_an.py!}47```43{* docs_src/arguments/optional/tutorial001_an.py hl[5] *}4445/// info4647Typer added support for `Annotated` (and started recommending it) in version 0.9.0.
62 unmodified lines
110Now, finally what we came for, an optional *CLI argument*.111112To make a *CLI argument* optional, use `typer.Argument()` and pass a different "default" as the first parameter to `typer.Argument()`, for example `None`:113118```Python hl_lines="7"119{!../docs_src/arguments/optional/tutorial002_an.py!}120```114{* docs_src/arguments/optional/tutorial002_an.py hl[7] *}115116Now we have:117118```Python
64 unmodified lines
183**Typer** also supports another older alternative syntax for declaring *CLI arguments* with additional metadata.184185Instead of using `Annotated`, you can use `typer.Argument()` as the default value:186193```Python hl_lines="4"194{!> ../docs_src/arguments/optional/tutorial001.py!}195```187{* docs_src/arguments/optional/tutorial001.py hl[4] *}188189/// tip190191Prefer to use the `Annotated` version if possible.
25 unmodified lines
217If you hadn't seen that `...` before: it is a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">part of Python and is called "Ellipsis"</a>.218219///220229```Python hl_lines="4"230{!> ../docs_src/arguments/optional/tutorial003.py!}231```221{* docs_src/arguments/optional/tutorial003.py hl[4] *}222223And the same way, you can make it optional by passing a different `default` value, for example `None`:224235```Python hl_lines="6"236{!> ../docs_src/arguments/optional/tutorial002.py!}237```225{* docs_src/arguments/optional/tutorial002.py hl[6] *}226227Because the first parameter passed to `typer.Argument(default=None)` (the new "default" value) is `None`, **Typer** knows that this is an **optional** *CLI argument*, if no value is provided when calling it in the command line, it will have that default value of `None`.228229The `default` argument is the first one, so it's possible that you see code that passes the value without explicitly using `default=`, like:
12 unmodified lines
modified:docs/tutorial/commands/arguments.md+1 added-3 removed
modified:docs/tutorial/commands/callback.md+4 added-12 removed
10 unmodified lines
11But we can use `@app.callback()` for that.1213It's very similar to `@app.command()`, but it declares the *CLI parameters* for the main CLI application (before the commands):1415```Python hl_lines="25 26 27 28 29 30 31 32"16{!../docs_src/commands/callback/tutorial001.py!}17```15{* docs_src/commands/callback/tutorial001.py hl[25,26,27,28,29,30,31,32] *}1617Here we create a `callback` with a `--verbose` *CLI option*.1819/// tip
58 unmodified lines
78## Adding a callback on creation7980It's also possible to add a callback when creating the `typer.Typer()` app:8184```Python hl_lines="4 5 8"85{!../docs_src/commands/callback/tutorial002.py!}86```82{* docs_src/commands/callback/tutorial002.py hl[4,5,8] *}8384That achieves the same as with `@app.callback()`.8586Check it:
12 unmodified lines
99## Overriding a callback100101If you added a callback when creating the `typer.Typer()` app, it's possible to override it with `@app.callback()`:102107```Python hl_lines="11 12 13"108{!../docs_src/commands/callback/tutorial003.py!}109```103{* docs_src/commands/callback/tutorial003.py hl[11,12,13] *}104105Now `new_callback()` will be the one used.106107Check it:
15 unmodified lines
123You can also add a callback just to add the documentation in the docstring.124125It can be convenient especially if you have several lines of text, as the indentation will be automatically handled for you:126133```Python hl_lines="8 9 10 11 12 13 14 15 16"134{!../docs_src/commands/callback/tutorial004.py!}135```127{* docs_src/commands/callback/tutorial004.py hl[8,9,10,11,12,13,14,15,16] *}128129Now the callback will be used mainly to extract the docstring for the help text.130131Check it:
54 unmodified lines
modified:docs/tutorial/commands/context.md+4 added-12 removed
12 unmodified lines
13For example, let's say that you want to execute some logic in a `Typer` callback depending on the subcommand that is being called.1415You can get the name of the subcommand from the context:1617```Python hl_lines="17 21"18{!../docs_src/commands/context/tutorial001.py!}19```17{* docs_src/commands/context/tutorial001.py hl[17,21] *}1819Check it:2021<div class="termy">
21 unmodified lines
43And if no command is provided, the help message is shown.4445But we could make it run even without a subcommand with `invoke_without_command=True`:4649```Python hl_lines="16"50{!../docs_src/commands/context/tutorial002.py!}51```47{* docs_src/commands/context/tutorial002.py hl[16] *}4849Check it:5051<div class="termy">
21 unmodified lines
73For that, we can get the `typer.Context` and check if there's an invoked command in `ctx.invoked_subcommand`.7475If it's `None`, it means that we are not calling a subcommand but the main program (the callback) directly:7681```Python hl_lines="17 21"82{!../docs_src/commands/context/tutorial003.py!}83```77{* docs_src/commands/context/tutorial003.py hl[17,21] *}7879Check it:8081<div class="termy">
22 unmodified lines
104For example, you could keep additional *CLI parameters* not declared in your CLI program with `ignore_unknown_options` and `allow_extra_args`.105106Then you can access those extra raw *CLI parameters* as a `list` of `str` in `ctx.args`:107114```Python hl_lines="7 9 10"115{!../docs_src/commands/context/tutorial004.py!}116```108{* docs_src/commands/context/tutorial004.py hl[7,9,10] *}109110<div class="termy">111112```console
15 unmodified lines
modified:docs/tutorial/commands/help.md+8 added-100 removed
2 unmodified lines
3The same as before, you can add help for the commands in the docstrings and the *CLI options*.45And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:67//// tab | Python 3.7+7{* docs_src/commands/help/tutorial001_an.py hl[4,9:11,22,26:30,43,47:51,60:62] *}89```Python hl_lines="4 9-11 22 26-30 43 47-51 60-62"10{!> ../docs_src/commands/help/tutorial001_an.py!}11```1213////1415//// tab | Python 3.7+ non-Annotated1617/// tip1819Prefer to use the `Annotated` version if possible.2021///2223```Python hl_lines="3 8-10 20 23-27 39 42-46 55-57"24{!> ../docs_src/commands/help/tutorial001.py!}25```2627////289Check it:1011<div class="termy">12
78 unmodified lines
91## Overwrite command help9293You will probably be better adding the help text as a docstring to your functions, but if for some reason you wanted to overwrite it, you can use the `help` function argument passed to `@app.command()`:94115```Python hl_lines="6 14"116{!../docs_src/commands/help/tutorial002.py!}117```95{* docs_src/commands/help/tutorial002.py hl[6,14] *}9697Check it:9899<div class="termy">
25 unmodified lines
125There could be cases where you have a command in your app that you need to deprecate, so that your users stop using it, even while it's still supported for a while.126127You can mark it with the parameter `deprecated=True`:128151```Python hl_lines="14"152{!../docs_src/commands/help/tutorial003.py!}153```129{* docs_src/commands/help/tutorial003.py hl[14] *}130131And when you show the `--help` option you will see it's marked as "`deprecated`":132133<div class="termy">
57 unmodified lines
191### Rich Markup192193If you set `rich_markup_mode="rich"` when creating the `typer.Typer()` app, you will be able to use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> in the docstring, and even in the help for the *CLI arguments* and options:194219//// tab | Python 3.7+195{* docs_src/commands/help/tutorial004_an.py hl[4,10,14:16,21,24,27] *}196221```Python hl_lines="4 10 14-16 21 24 27"222{!> ../docs_src/commands/help/tutorial004_an.py!}223```224225////226227//// tab | Python 3.7+ non-Annotated228229/// tip230231Prefer to use the `Annotated` version if possible.232233///234235```Python hl_lines="3 9 13-15 20 22 24"236{!> ../docs_src/commands/help/tutorial004.py!}237```238239////240197With that, you can use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> to format the text in the docstring for the command `create`, make the word "`create`" bold and green, and even use an <a href="https://rich.readthedocs.io/en/stable/markup.html#emoji" class="external-link" target="_blank">emoji</a>.198199You can also use markup in the help for the `username` CLI Argument.200
53 unmodified lines
254### Rich Markdown255256If you set `rich_markup_mode="markdown"` when creating the `typer.Typer()` app, you will be able to use Markdown in the docstring:257302//// tab | Python 3.7+258{* docs_src/commands/help/tutorial005_an.py hl[4,9,12:20,25,27:28] *}259304```Python hl_lines="4 9 12-20 25 27-28"305{!> ../docs_src/commands/help/tutorial005_an.py!}306```307308////309310//// tab | Python 3.7+ non-Annotated311312/// tip313314Prefer to use the `Annotated` version if possible.315316///317318```Python hl_lines="3 7 9-17 22 24-25"319{!> ../docs_src/commands/help/tutorial005.py!}320```321322////323260With that, you can use Markdown to format the text in the docstring for the command `create`, make the word "`create`" bold, show a list of items, and even use an <a href="https://rich.readthedocs.io/en/stable/markup.html#emoji" class="external-link" target="_blank">emoji</a>.261262And the same as before, the help text overwritten for the command `delete` can also use Markdown.263
66 unmodified lines
330### Help Panels for Commands331332To set the panel for a command you can pass the argument `rich_help_panel` with the name of the panel you want to use:333398//// tab | Python 3.7+334{* docs_src/commands/help/tutorial006.py hl[22,30,38,46] *}335400```Python hl_lines="22 30 38 46"401{!> ../docs_src/commands/help/tutorial006.py!}402```403404////405336Commands without a panel will be shown in the default panel `Commands`, and the rest will be shown in the next panels:337338<div class="termy">339
31 unmodified lines
371The same way, you can configure the panels for *CLI arguments* and *CLI options* with `rich_help_panel`.372373And of course, in the same application you can also set the `rich_help_panel` for commands.374445//// tab | Python 3.7+375{* docs_src/commands/help/tutorial007_an.py hl[15,21,27,37] *}376447```Python hl_lines="15 21 27 37"448{!> ../docs_src/commands/help/tutorial007_an.py!}449```450451////452453//// tab | Python 3.7+ non-Annotated454455/// tip456457Prefer to use the `Annotated` version if possible.458459///460461```Python hl_lines="12 16 21 30"462{!> ../docs_src/commands/help/tutorial007.py!}463```464465////466377Then if you run the application you will see all the *CLI parameters* in their respective panels.378379* First the ***CLI arguments*** that don't have a panel name set in a **default** one named "`Arguments`".380* Next the ***CLI arguments*** with a **custom panel**. In this example named "`Secondary Arguments`".
68 unmodified lines
449## Epilog450451If you need, you can also add an epilog section to the help of your commands:452543```Python hl_lines="6"544{!../docs_src/commands/help/tutorial008.py!}545```453{* docs_src/commands/help/tutorial008.py hl[6] *}454455And when you check the `--help` option it will look like:456457<div class="termy">
24 unmodified lines
modified:docs/tutorial/commands/index.md+5 added-15 removed
52 unmodified lines
53In the *CLI options* and *CLI argument* tutorials you have seen how to create a single function and then pass that function to `typer.run()`.5455For example:5657```Python hl_lines="9"58{!../docs_src/first_steps/tutorial002.py!}59```57{* docs_src/first_steps/tutorial002.py hl[9] *}5859But that is actually a shortcut. Under the hood, **Typer** converts that to a CLI application with `typer.Typer()` and executes it. All that inside of `typer.run()`.6061There's also a more explicit way to achieve the same:6265```Python hl_lines="3 6 12"66{!../docs_src/commands/index/tutorial001.py!}67```63{* docs_src/commands/index/tutorial001.py hl[3,6,12] *}6465When you use `typer.run()`, **Typer** is doing more or less the same as above, it will:6667* Create a new `typer.Typer()` "application".
107 unmodified lines
175We'll have a command to `create` users and another command to `delete` them.176177To begin, let's say it can only create and delete one single predefined user:178183```Python hl_lines="6 11"184{!../docs_src/commands/index/tutorial002.py!}185```179{* docs_src/commands/index/tutorial002.py hl[6,11] *}180181Now we have a CLI application with 2 commands, `create` and `delete`:182183<div class="termy">
40 unmodified lines
224By default, we need to specify `--help` to get the command's help page.225226However, by setting `no_args_is_help=True` when defining the `typer.Typer()` application, the help function will be shown whenever no argument is given:227234```Python hl_lines="3"235{!../docs_src/commands/index/tutorial003.py!}236```228{* docs_src/commands/index/tutorial003.py hl[3] *}229230Now we can run this:231232<div class="termy">
22 unmodified lines
255Note that by design, **Typer** shows the commands in the order they've been declared.256257So, if we take our original example, with `create` and `delete` commands, and reverse the order in the Python file:258267```Python hl_lines="7 12"268{!../docs_src/commands/index/tutorial004.py!}269```259{* docs_src/commands/index/tutorial004.py hl[7,12] *}260261Then we will see the `delete` command first in the help output:262263<div class="termy">
46 unmodified lines
modified:docs/tutorial/commands/name.md+1 added-3 removed
15 unmodified lines
16And what if you wanted the command to still be named `create`?1718For this, you can set the name of the command in the first parameter for the `@app.command()` decorator:1920```Python hl_lines="6 11"21{!../docs_src/commands/name/tutorial001.py!}22```20{* docs_src/commands/name/tutorial001.py hl[6,11] *}2122Now, even though the functions are named `cli_create_user()` and `cli_delete_user()`, the commands will still be named `create` and `delete`:2324<div class="termy">
32 unmodified lines
modified:docs/tutorial/commands/one-or-multiple.md+4 added-12 removed
1# One or Multiple Commands23You might have noticed that if you create a single command, as in the first example:45```Python hl_lines="3 6 12"6{!../docs_src/commands/index/tutorial001.py!}7```5{* docs_src/commands/index/tutorial001.py hl[3,6,12] *}67**Typer** is smart enough to create a CLI application with that single function as the main CLI application, not as a command/subcommand:89<div class="termy">
32 unmodified lines
42///4344But if you add multiple commands, **Typer** will create one *CLI command* for each one of them:4548```Python hl_lines="6 11"49{!../docs_src/commands/index/tutorial002.py!}50```46{* docs_src/commands/index/tutorial002.py hl[6,11] *}4748Here we have 2 commands `create` and `delete`:4950<div class="termy">
28 unmodified lines
79## One command and one callback8081If you want to create a CLI app with one single command but you still want it to be a command/subcommand you can just add a callback:8287```Python hl_lines="11 12 13"88{!../docs_src/commands/one_or_multiple/tutorial001.py!}89```83{* docs_src/commands/one_or_multiple/tutorial001.py hl[11,12,13] *}8485And now your CLI program will have a single command.8687Check it:
26 unmodified lines
114## Using the callback to document115116Now that you are using a callback just to have a single command, you might as well use it to add documentation for your app:117124```Python hl_lines="11 12 13 14 15 16 17"125{!../docs_src/commands/one_or_multiple/tutorial002.py!}126```118{* docs_src/commands/one_or_multiple/tutorial002.py hl[11,12,13,14,15,16,17] *}119120And now the docstring from the callback will be used as the help text:121122<div class="termy">
26 unmodified lines
modified:docs/tutorial/commands/options.md+1 added-21 removed
2 unmodified lines
3Commands can also have their own *CLI options*.45In fact, each command can have different *CLI arguments* and *CLI options*:67//// tab | Python 3.7+7{* docs_src/commands/options/tutorial001_an.py hl[8,14:17,27:29,38] *}89```Python hl_lines="8 14-17 27-29 38"10{!> ../docs_src/commands/options/tutorial001_an.py!}11```1213////1415//// tab | Python 3.7+ non-Annotated1617/// tip1819Prefer to use the `Annotated` version if possible.2021///2223```Python hl_lines="7 13-14 24 33"24{!> ../docs_src/commands/options/tutorial001.py!}25```2627////289Here we have multiple commands, with different *CLI parameters*:1011* `create`:12 * `username`: a *CLI argument*.
87 unmodified lines
modified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+2 added-24 removed
2 unmodified lines
3*CLI arguments* can also receive multiple values.45You can define the type of a *CLI argument* using `typing.List`.67```Python hl_lines="7"8{!../docs_src/multiple_values/arguments_with_multiple_values/tutorial001.py!}9```7{* docs_src/multiple_values/arguments_with_multiple_values/tutorial001.py hl[7] *}89And then you can pass it as many *CLI arguments* of that type as you want:1011<div class="termy">
24 unmodified lines
36## *CLI arguments* with tuples3738If you want a specific number of values and types, you can use a tuple, and it can even have default values:3942//// tab | Python 3.7+40{* docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py hl[8:10] *}4144```Python hl_lines="8-10"45{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py!}46```4748////4950//// tab | Python 3.7+ non-Annotated5152/// tip5354Prefer to use the `Annotated` version if possible.5556///5758```Python hl_lines="7-8"59{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002.py!}60```6162////6342Check it:4344<div class="termy">45
33 unmodified lines
modified:docs/tutorial/multiple-values/multiple-options.md+2 added-42 removed
4 unmodified lines
5For example, let's say you want to accept several users in a single execution.67For this, use the standard Python `typing.List` to declare it as a `list` of `str`:89//// tab | Python 3.7+9{* docs_src/multiple_values/multiple_options/tutorial001_an.py hl[1,7] *}1011```Python hl_lines="1 7"12{!> ../docs_src/multiple_values/multiple_options/tutorial001_an.py!}13```1415////1617//// tab | Python 3.7+ non-Annotated1819/// tip2021Prefer to use the `Annotated` version if possible.2223///2425```Python hl_lines="1 6"26{!> ../docs_src/multiple_values/multiple_options/tutorial001.py!}27```2829////3011You will receive the values as you declared them, as a `list` of `str`.1213Check it:14
24 unmodified lines
39## Multiple `float`4041The same way, you can use other types and they will be converted by **Typer** to their declared type:4263//// tab | Python 3.7+43{* docs_src/multiple_values/multiple_options/tutorial002_an.py hl[7] *}4465```Python hl_lines="7"66{!> ../docs_src/multiple_values/multiple_options/tutorial002_an.py!}67```6869////7071//// tab | Python 3.7+ non-Annotated7273/// tip7475Prefer to use the `Annotated` version if possible.7677///7879```Python hl_lines="6"80{!> ../docs_src/multiple_values/multiple_options/tutorial002.py!}81```8283////8445Check it:4647<div class="termy">48
17 unmodified lines
modified:docs/tutorial/multiple-values/options-with-multiple-values.md+1 added-21 removed
4 unmodified lines
5You can set the number of values and types to anything you want, but it has to be a fixed number of values.67For this, use the standard Python `typing.Tuple`:89//// tab | Python 3.7+9{* docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py hl[1,7] *}1011```Python hl_lines="1 7"12{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py!}13```1415////1617//// tab | Python 3.7+ non-Annotated1819/// tip2021Prefer to use the `Annotated` version if possible.2223///2425```Python hl_lines="1 6"26{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001.py!}27```2829////3011Each of the internal types defines the type of each value in the tuple.1213So:14
79 unmodified lines
modified:docs/tutorial/options/callback-and-context.md+4 added-84 removed
6 unmodified lines
7## Validate *CLI parameters*89For example, you could do some validation before the rest of the code is executed.1011//// tab | Python 3.7+11{* docs_src/options/callback/tutorial001_an.py hl[7:10,13] *}1213```Python hl_lines="7-10 13"14{!> ../docs_src/options/callback/tutorial001_an.py!}15```1617////1819//// tab | Python 3.7+ non-Annotated2021/// tip2223Prefer to use the `Annotated` version if possible.2425///2627```Python hl_lines="6-9 12"28{!> ../docs_src/options/callback/tutorial001.py!}29```3031////3213Here you pass a function to `typer.Option()` or `typer.Argument()` with the keyword argument `callback`.1415The function receives the value from the command line. It can do anything with it, and then return the value.16
76 unmodified lines
93### Breaking completion in a callback9495Let's say that when the callback is running, we want to show a message saying that it's validating the name:96117//// tab | Python 3.7+97{* docs_src/options/callback/tutorial002_an.py hl[8] *}98119```Python hl_lines="8"120{!> ../docs_src/options/callback/tutorial002_an.py!}121```122123////124125//// tab | Python 3.7+ non-Annotated126127/// tip128129Prefer to use the `Annotated` version if possible.130131///132133```Python hl_lines="7"134{!> ../docs_src/options/callback/tutorial002.py!}135```136137////13899And because the callback will be called when the shell calls your program asking for completion, that message `"Validating name"` will be printed and it will break completion.100101It will look something like:102
25 unmodified lines
128But you can access the context by declaring a function parameter of type `typer.Context`.129130The "context" has some additional data about the current execution of your program:131172//// tab | Python 3.7+132{* docs_src/options/callback/tutorial003_an.py hl[7:9] *}133174```Python hl_lines="7-9"175{!> ../docs_src/options/callback/tutorial003_an.py!}176```177178////179180//// tab | Python 3.7+ non-Annotated181182/// tip183184Prefer to use the `Annotated` version if possible.185186///187188```Python hl_lines="6-8"189{!> ../docs_src/options/callback/tutorial003.py!}190```191192////193134The `ctx.resilient_parsing` will be `True` when handling completion, so you can just return without printing anything else.135136But it will be `False` when calling the program normally. So you can continue the execution of your previous code.137
22 unmodified lines
160## Using the `CallbackParam` object161162The same way you can access the `typer.Context` by declaring a function parameter with its value, you can declare another function parameter with type `typer.CallbackParam` to get the specific Click `Parameter` object.163224//// tab | Python 3.7+164{* docs_src/options/callback/tutorial004_an.py hl[7,10] *}165226```Python hl_lines="7 10"227{!> ../docs_src/options/callback/tutorial004_an.py!}228```229230////231232//// tab | Python 3.7+ non-Annotated233234/// tip235236Prefer to use the `Annotated` version if possible.237238///239240```Python hl_lines="6 9"241{!> ../docs_src/options/callback/tutorial004.py!}242```243244////245166It's probably not very common, but you could do it if you need it.167168For example if you had a callback that could be used by several *CLI parameters*, that way the callback could know which parameter is each time.169
42 unmodified lines
modified:docs/tutorial/options/help.md+4 added-84 removed
2 unmodified lines
3You already saw how to add a help text for *CLI arguments* with the `help` parameter.45Let's now do the same for *CLI options*:67//// tab | Python 3.7+7{* docs_src/options/help/tutorial001_an.py hl[7:8] *}89```Python hl_lines="7-8"10{!> ../docs_src/options/help/tutorial001_an.py!}11```1213////1415//// tab | Python 3.7+ non-Annotated1617/// tip1819Prefer to use the `Annotated` version if possible.2021///2223```Python hl_lines="6-7"24{!> ../docs_src/options/help/tutorial001.py!}25```2627////289The same way as with `typer.Argument()`, we can put `typer.Option()` inside of `Annotated`.1011We can then pass the `help` keyword parameter:12
42 unmodified lines
55The same as with *CLI arguments*, you can put the help for some *CLI options* in different panels to be shown with the `--help` option.5657If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel you want for each *CLI option*:5879//// tab | Python 3.7+59{* docs_src/options/help/tutorial002_an.py hl[11,17] *}6081```Python hl_lines="11 17"82{!> ../docs_src/options/help/tutorial002_an.py!}83```8485////8687//// tab | Python 3.7+ non-Annotated8889/// tip9091Prefer to use the `Annotated` version if possible.9293///9495```Python hl_lines="8 11"96{!> ../docs_src/options/help/tutorial002.py!}97```9899////10061Now, when you check the `--help` option, you will see a default panel named "`Options`" for the *CLI options* that don't have a custom `rich_help_panel`.6263And below you will see other panels for the *CLI options* that have a custom panel set in the `rich_help_panel` parameter:64
36 unmodified lines
101## Hide default from help102103You can tell Typer to not show the default value in the help text with `show_default=False`:104145//// tab | Python 3.7+105{* docs_src/options/help/tutorial003_an.py hl[5] *}106147```Python hl_lines="5"148{!> ../docs_src/options/help/tutorial003_an.py!}149```150151////152153//// tab | Python 3.7+ non-Annotated154155/// tip156157Prefer to use the `Annotated` version if possible.158159///160161```Python hl_lines="4"162{!> ../docs_src/options/help/tutorial003.py!}163```164165////166107And it will no longer show the default value in the help text:108109<div class="termy">110
27 unmodified lines
138## Custom default string139140You 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:141202//// tab | Python 3.7+142{* docs_src/options/help/tutorial004_an.py hl[7] *}143204```Python hl_lines="7"205{!> ../docs_src/options/help/tutorial004_an.py!}206```207208////209210//// tab | Python 3.7+ non-Annotated211212/// tip213214Prefer to use the `Annotated` version if possible.215216///217218```Python hl_lines="6"219{!> ../docs_src/options/help/tutorial004.py!}220```221222////223144And it will be used in the help text:145146<div class="termy">147
18 unmodified lines
modified:docs/tutorial/options/name.md+5 added-109 removed
26 unmodified lines
27Let's say the function parameter name is `user_name` as above, but you want the *CLI option* to be just `--name`.2829You can pass the *CLI option* name that you want to have in the following positional argument passed to `typer.Option()`:3031//// tab | Python 3.7+31{* docs_src/options/name/tutorial001_an.py hl[5] *}3233```Python hl_lines="5"34{!> ../docs_src/options/name/tutorial001_an.py!}35```3637Here you are passing the string `"--name"` as the first positional argument to `typer.Option()`.3839////4041//// tab | Python 3.7+ non-Annotated4243/// tip4445Prefer to use the `Annotated` version if possible.4647///4849```Python hl_lines="4"50{!> ../docs_src/options/name/tutorial001.py!}51```5253Here you are passing the string `"--name"` as the second positional argument to `typer.Option()`, as the first argument is `...` to mark it as required.5455////5633/// info3435"<a href="https://docs.python.org/3.8/glossary.html#term-argument" class="external-link" target="_blank">Positional</a>" means that it's not a function argument with a keyword name.36
147 unmodified lines
184You can overwrite the *CLI option* name to use as in the previous example, but you can also declare extra alternatives, including short names.185186For example, extending the previous example, let's add a *CLI option* short name `-n`:187212//// tab | Python 3.7+188{* docs_src/options/name/tutorial002_an.py hl[5] *}189214```Python hl_lines="5"215{!> ../docs_src/options/name/tutorial002_an.py!}216```217218////219220//// tab | Python 3.7+ non-Annotated221222/// tip223224Prefer to use the `Annotated` version if possible.225226///227228```Python hl_lines="4"229{!> ../docs_src/options/name/tutorial002.py!}230```231232////233190Here we are overwriting the *CLI option* name that by default would be `--user-name`, and we are defining it to be `--name`. And we are also declaring a *CLI option* short name of `-n`.191192Check it:193
21 unmodified lines
215### *CLI option* only short name216217If you only declare a short name like `-n` then that will be the only *CLI option* name. And neither `--name` nor `--user-name` will be available.218263//// tab | Python 3.7+219{* docs_src/options/name/tutorial003_an.py hl[5] *}220265```Python hl_lines="5"266{!> ../docs_src/options/name/tutorial003_an.py!}267```268269////270271//// tab | Python 3.7+ non-Annotated272273/// tip274275Prefer to use the `Annotated` version if possible.276277///278279```Python hl_lines="4"280{!> ../docs_src/options/name/tutorial003.py!}281```282283////284221Check it:222223<div class="termy">224
18 unmodified lines
243### *CLI option* short name and default244245Continuing with the example above, as **Typer** allows you to declare a *CLI option* as having only a short name, if you want to have the default long name plus a short name, you have to declare both explicitly:246311//// tab | Python 3.7+247{* docs_src/options/name/tutorial004_an.py hl[5] *}248313```Python hl_lines="5"314{!> ../docs_src/options/name/tutorial004_an.py!}315```316317////318319//// tab | Python 3.7+ non-Annotated320321/// tip322323Prefer to use the `Annotated` version if possible.324325///326327```Python hl_lines="4"328{!> ../docs_src/options/name/tutorial004.py!}329```330331////332249Check it:250251<div class="termy">252
24 unmodified lines
277You can create multiple short names and use them together.278279You don't have to do anything special for it to work (apart from declaring those short versions):280365//// tab | Python 3.7+281{* docs_src/options/name/tutorial005_an.py hl[6:7] *}282367```Python hl_lines="6-7"368{!> ../docs_src/options/name/tutorial005_an.py!}369```370371////372373//// tab | Python 3.7+ non-Annotated374283/// tip284377Prefer to use the `Annotated` version if possible.378379///380381```Python hl_lines="5-6"382{!> ../docs_src/options/name/tutorial005.py!}383```384385////386387/// tip388285Notice that, again, we are declaring the long and short version of the *CLI option* names.286287///288
29 unmodified lines
modified:docs/tutorial/options/password.md+2 added-42 removed
1# Password CLI Option and Confirmation Prompt23Apart from having a prompt, you can make a *CLI option* have a `confirmation_prompt=True`:45//// tab | Python 3.7+5{* docs_src/options/password/tutorial001_an.py hl[7] *}67```Python hl_lines="7"8{!> ../docs_src/options/password/tutorial001_an.py!}9```1011////1213//// tab | Python 3.7+ non-Annotated1415/// tip1617Prefer to use the `Annotated` version if possible.1819///2021```Python hl_lines="5"22{!> ../docs_src/options/password/tutorial001.py!}23```2425////267And the CLI program will ask for confirmation:89<div class="termy">10
18 unmodified lines
29You can achieve the same using `hide_input=True`.3031And if you combine it with `confirmation_prompt=True` you can easily receive a password with double confirmation:3253//// tab | Python 3.7+33{* docs_src/options/password/tutorial002_an.py hl[8] *}3455```Python hl_lines="8"56{!> ../docs_src/options/password/tutorial002_an.py!}57```5859////6061//// tab | Python 3.7+ non-Annotated6263/// tip6465Prefer to use the `Annotated` version if possible.6667///6869```Python hl_lines="6-8"70{!> ../docs_src/options/password/tutorial002.py!}71```7273////7435Check it:3637<div class="termy">38
13 unmodified lines
modified:docs/tutorial/options/prompt.md+3 added-63 removed
1# CLI Option Prompt23It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:45//// tab | Python 3.7+5{* docs_src/options/prompt/tutorial001_an.py hl[5] *}67```Python hl_lines="5"8{!> ../docs_src/options/prompt/tutorial001_an.py!}9```1011////1213//// tab | Python 3.7+ non-Annotated1415/// tip1617Prefer to use the `Annotated` version if possible.1819///2021```Python hl_lines="4"22{!> ../docs_src/options/prompt/tutorial001.py!}23```2425////267And then your program will ask the user for it in the terminal:89<div class="termy">10
12 unmodified lines
23## Customize the prompt2425You can also set a custom prompt, passing the string that you want to use instead of just `True`:2647//// tab | Python 3.7+27{* docs_src/options/prompt/tutorial002_an.py hl[7] *}2849```Python hl_lines="7"50{!> ../docs_src/options/prompt/tutorial002_an.py!}51```5253////5455//// tab | Python 3.7+ non-Annotated5657/// tip5859Prefer to use the `Annotated` version if possible.6061///6263```Python hl_lines="5"64{!> ../docs_src/options/prompt/tutorial002.py!}65```6667////6829And then your program will ask for it using with your custom prompt:3031<div class="termy">32
16 unmodified lines
49You can do it passing the parameter `confirmation_prompt=True`.5051Let's say it's a CLI app to delete a project:5293//// tab | Python 3.7+53{* docs_src/options/prompt/tutorial003_an.py hl[6] *}5495```Python hl_lines="6"96{!> ../docs_src/options/prompt/tutorial003_an.py!}97```9899////100101//// tab | Python 3.7+ non-Annotated102103/// tip104105Prefer to use the `Annotated` version if possible.106107///108109```Python hl_lines="4"110{!> ../docs_src/options/prompt/tutorial003.py!}111```112113////11455And it will prompt the user for a value and then for the confirmation:5657<div class="termy">58
26 unmodified lines
modified:docs/tutorial/options/required.md+3 added-21 removed
11 unmodified lines
12To make a *CLI option* required, you can put `typer.Option()` inside of `Annotated` and leave the parameter without a default value.1314Let's make `--lastname` a required *CLI option*:1516//// tab | Python 3.7+16{* docs_src/options/required/tutorial001_an.py hl[5] *}1718```Python hl_lines="5"19{!> ../docs_src/options/required/tutorial001_an.py!}20```2122////2318The same way as with `typer.Argument()`, the old style of using the function parameter default value is also supported, in that case you would just not pass anything to the `default` parameter.1926//// tab | Python 3.7+ non-Annotated20{* docs_src/options/required/tutorial001.py hl[4] *}2128```Python hl_lines="4"29{!> ../docs_src/options/required/tutorial001.py!}30```3132////3322Or you can explictily pass `...` to `typer.Option(default=...)`:2336//// tab | Python 3.7+ non-Annotated24{* docs_src/options/required/tutorial002.py hl[4] *}2538```Python hl_lines="4"39{!> ../docs_src/options/required/tutorial002.py!}40```4142////4326/// info2728If you hadn't seen that `...` before: it is a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">part of Python and is called "Ellipsis"</a>.29
42 unmodified lines
modified:docs/tutorial/options/version.md+3 added-63 removed
6 unmodified lines
7## First version of `--version`89Let's see a first version of how it could look like:1011//// tab | Python 3.7+11{* docs_src/options/version/tutorial001_an.py hl[9:12,17:19] *}1213```Python hl_lines="9-12 17-19"14{!> ../docs_src/options/version/tutorial001_an.py!}15```1617////1819//// tab | Python 3.7+ non-Annotated2013/// tip1423Prefer to use the `Annotated` version if possible.2425///2627```Python hl_lines="8-11 16-18"28{!> ../docs_src/options/version/tutorial001.py!}29```3031////3233/// tip3415Notice that we don't have to get the `typer.Context` and check for `ctx.resilient_parsing` for completion to work, because we only print and modify the program when `--version` is passed, otherwise, nothing is printed or changed from the callback.1617///18
37 unmodified lines
56## Previous parameters and `is_eager`5758But now let's say that the `--name` *CLI option* that we declared before `--version` is required, and it has a callback that could exit the program:5980//// tab | Python 3.7+60{* docs_src/options/version/tutorial002_an.py hl[15:17,22:24] *}6182```Python hl_lines="15-17 22-24"83{!> ../docs_src/options/version/tutorial002_an.py!}84```8586////8788//// tab | Python 3.7+ non-Annotated8990/// tip9192Prefer to use the `Annotated` version if possible.9394///9596```Python hl_lines="14-16 21-23"97{!> ../docs_src/options/version/tutorial002.py!}98```99100////10162Then our CLI program could not work as expected in some cases as it is *right now*, because if we use `--version` after `--name` then the callback for `--name` will be processed before and we can get its error:6364<div class="termy">65
29 unmodified lines
95For those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*) with `is_eager=True`.9697That will tell **Typer** (actually Click) that it should process this *CLI parameter* before the others:98139//// tab | Python 3.7+99{* docs_src/options/version/tutorial003_an.py hl[23:26] *}100141```Python hl_lines="23-26"142{!> ../docs_src/options/version/tutorial003_an.py!}143```144145////146147//// tab | Python 3.7+ non-Annotated148149/// tip150151Prefer to use the `Annotated` version if possible.152153///154155```Python hl_lines="22-24"156{!> ../docs_src/options/version/tutorial003.py!}157```158159////160101Check it:102103<div class="termy">104
8 unmodified lines
modified:docs/tutorial/parameter-types/bool.md+4 added-84 removed
8 unmodified lines
9Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.1011We can do that by specifying the exact name we want:1213//// tab | Python 3.7+13{* docs_src/parameter_types/bool/tutorial001_an.py hl[5] *}1415```Python hl_lines="5"16{!> ../docs_src/parameter_types/bool/tutorial001_an.py!}17```1819////2021//// tab | Python 3.7+ non-Annotated2223/// tip2425Prefer to use the `Annotated` version if possible.2627///2829```Python hl_lines="4"30{!> ../docs_src/parameter_types/bool/tutorial001.py!}31```3233////3415Now there's only a `--force` *CLI option*:1617<div class="termy">18
38 unmodified lines
57We might want to instead have `--accept` and `--reject`.5859We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:6081//// tab | Python 3.7+61{* docs_src/parameter_types/bool/tutorial002_an.py hl[7] *}6283```Python hl_lines="7"84{!> ../docs_src/parameter_types/bool/tutorial002_an.py!}85```8687////8889//// tab | Python 3.7+ non-Annotated9091/// tip9293Prefer to use the `Annotated` version if possible.9495///9697```Python hl_lines="6"98{!> ../docs_src/parameter_types/bool/tutorial002.py!}99```100101////10263Check it:6465<div class="termy">66
31 unmodified lines
98The same way, you can declare short versions of the names for these *CLI options*.99100For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:101142//// tab | Python 3.7+102{* docs_src/parameter_types/bool/tutorial003_an.py hl[5] *}103144```Python hl_lines="5"145{!> ../docs_src/parameter_types/bool/tutorial003_an.py!}146```147148////149150//// tab | Python 3.7+ non-Annotated151152/// tip153154Prefer to use the `Annotated` version if possible.155156///157158```Python hl_lines="4"159{!> ../docs_src/parameter_types/bool/tutorial003.py!}160```161162////163104Check it:105106<div class="termy">107
26 unmodified lines
134If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.135136To do that, use a space and a single `/` and pass the negative name after:137198//// tab | Python 3.7+138{* docs_src/parameter_types/bool/tutorial004_an.py hl[5] *}139200```Python hl_lines="5"201{!> ../docs_src/parameter_types/bool/tutorial004_an.py!}202```203204////205206//// tab | Python 3.7+ non-Annotated207140/// tip141210Prefer to use the `Annotated` version if possible.211212///213214```Python hl_lines="4"215{!> ../docs_src/parameter_types/bool/tutorial004.py!}216```217218////219220/// tip221142Have in mind that it's a string with a preceding space and then a `/`.143144So, it's `" /-S"` not `"/-S"`.145
34 unmodified lines
modified:docs/tutorial/parameter-types/custom-types.md+2 added-28 removed
11 unmodified lines
12## Type Parser1314`typer.Argument` and `typer.Option` can create custom parameter types with a `parser` <abbr title="something that can be called like a function">callable</abbr>.1516//// tab | Python 3.7+16{* docs_src/parameter_types/custom_types/tutorial001_an.py hl[13:14,18:19] *}1718```Python hl_lines="13-14 18-19"19{!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}20```2122////2324//// tab | Python 3.7+ non-Annotated2526/// tip2728Prefer to use the `Annotated` version if possible.2930///3132```Python hl_lines="12-13 17-18"33{!> ../docs_src/parameter_types/custom_types/tutorial001.py!}34```3536////3718The function (or callable) that you pass to the parameter `parser` will receive the input value as a string and should return the parsed value with your own custom type.1920## Click Custom Type2122If you already have a <a href="https://click.palletsprojects.com/en/8.1.x/parameters/#implementing-custom-types" class="external-link" target="_blank">Click Custom Type</a>, you can use it in `typer.Argument()` and `typer.Option()` with the `click_type` parameter.2344//// tab | Python 3.7+4546```Python hl_lines="14-18 22-25"47{!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}48```4950////24{* docs_src/parameter_types/custom_types/tutorial002_an.py hl[14:18,22:25] *}
modified:docs/tutorial/parameter-types/datetime.md+2 added-24 removed
2 unmodified lines
3You can specify a *CLI parameter* as a Python <a href="https://docs.python.org/3/library/datetime.html" class="external-link" target="_blank">`datetime`</a>.45Your function will receive a standard Python `datetime` object, and again, your editor will give you completion, etc.67```Python hl_lines="1 6 7 8"8{!../docs_src/parameter_types/datetime/tutorial001.py!}9```7{* docs_src/parameter_types/datetime/tutorial001.py hl[1,6,7,8] *}89Typer will accept any string from the following formats:1011* `%Y-%m-%d`
45 unmodified lines
57* separated with "`/`"5859...It's a crazy example, but let's say you also needed that strange format:6063//// tab | Python 3.7+61{* docs_src/parameter_types/datetime/tutorial002_an.py hl[11] *}6265```Python hl_lines="11"66{!> ../docs_src/parameter_types/datetime/tutorial002_an.py!}67```6869////7071//// tab | Python 3.7+ non-Annotated7263/// tip6475Prefer to use the `Annotated` version if possible.7677///7879```Python hl_lines="8"80{!> ../docs_src/parameter_types/datetime/tutorial002.py!}81```8283////8485/// tip8665Notice the last string in `formats`: `"%m/%d/%Y"`.6667///68
17 unmodified lines
modified:docs/tutorial/parameter-types/enum.md+3 added-45 removed
1# Enum - Choices23To define a *CLI parameter* that can take a value from a predefined set of values you can use a standard Python <a href="https://docs.python.org/3/library/enum.html" class="external-link" target="_blank">`enum.Enum`</a>:45```Python hl_lines="1 6 7 8 9 12 13"6{!../docs_src/parameter_types/enum/tutorial001.py!}7```5{* docs_src/parameter_types/enum/tutorial001.py hl[1,6,7,8,9,12,13] *}67/// tip89Notice that the function parameter `network` will be an `Enum`, not a `str`.
43 unmodified lines
53### Case insensitive Enum choices5455You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:5659//// tab | Python 3.7+57{* docs_src/parameter_types/enum/tutorial002_an.py hl[15] *}5861```Python hl_lines="15"62{!> ../docs_src/parameter_types/enum/tutorial002_an.py!}63```6465////6667//// tab | Python 3.7+ non-Annotated6869/// tip7071Prefer to use the `Annotated` version if possible.7273///7475```Python hl_lines="13"76{!> ../docs_src/parameter_types/enum/tutorial002.py!}77```7879////8059And then the values of the `Enum` will be checked no matter if lower case, upper case, or a mix:6061<div class="termy">62
14 unmodified lines
77### List of Enum values7879A *CLI parameter* can also take a list of `Enum` values:80103//// tab | Python 3.7+81{* docs_src/parameter_types/enum/tutorial003_an.py hl[14] *}82105```Python hl_lines="14"106{!> ../docs_src/parameter_types/enum/tutorial003_an.py!}107```108109////110111//// tab | Python 3.7+ non-Annotated112113/// tip114115Prefer to use the `Annotated` version if possible.116117///118119```Python hl_lines="13"120{!> ../docs_src/parameter_types/enum/tutorial003.py!}121```122123////12483This works just like any other parameter value taking a list of things:8485<div class="termy">86
27 unmodified lines
modified:docs/tutorial/parameter-types/file.md+5 added-105 removed
45 unmodified lines
46```4748You will get all the correct editor support, attributes, methods, etc for the file-like object:`4950//// tab | Python 3.7+50{* docs_src/parameter_types/file/tutorial001_an.py hl[5] *}5152```Python hl_lines="5"53{!> ../docs_src/parameter_types/file/tutorial001_an.py!}54```5556////5758//// tab | Python 3.7+ non-Annotated5960/// tip6162Prefer to use the `Annotated` version if possible.6364///6566```Python hl_lines="4"67{!> ../docs_src/parameter_types/file/tutorial001.py!}68```6970////7152Check it:5354<div class="termy">55
17 unmodified lines
73## `FileTextWrite`7475For writing text, you can use `typer.FileTextWrite`:7697//// tab | Python 3.7+77{* docs_src/parameter_types/file/tutorial002_an.py hl[5:6] *}7899```Python hl_lines="5-6"100{!> ../docs_src/parameter_types/file/tutorial002_an.py!}101```102103////104105//// tab | Python 3.7+ non-Annotated106107/// tip108109Prefer to use the `Annotated` version if possible.110111///112113```Python hl_lines="4-5"114{!> ../docs_src/parameter_types/file/tutorial002.py!}115```116117////11879This would be for writing human text, like:8081```82some settings
34 unmodified lines
117You will receive `bytes` from it.118119It's useful for reading binary files like images:120161//// tab | Python 3.7+121{* docs_src/parameter_types/file/tutorial003_an.py hl[5] *}122163```Python hl_lines="5"164{!> ../docs_src/parameter_types/file/tutorial003_an.py!}165```166167////168169//// tab | Python 3.7+ non-Annotated170171/// tip172173Prefer to use the `Annotated` version if possible.174175///176177```Python hl_lines="4"178{!> ../docs_src/parameter_types/file/tutorial003.py!}179```180181////182123Check it:124125<div class="termy">126
19 unmodified lines
146Have in mind that you have to pass `bytes` to its `.write()` method, not `str`.147148If you have a `str`, you have to encode it first to get `bytes`.149210//// tab | Python 3.7+150{* docs_src/parameter_types/file/tutorial004_an.py hl[5] *}151212```Python hl_lines="5"213{!> ../docs_src/parameter_types/file/tutorial004_an.py!}214```215216////217218//// tab | Python 3.7+ non-Annotated219220/// tip221222Prefer to use the `Annotated` version if possible.223224///225226```Python hl_lines="4"227{!> ../docs_src/parameter_types/file/tutorial004.py!}228```229230////231152<div class="termy">153154```console155$ python main.py --file binary.dat
40 unmodified lines
196You can override the `mode` from the defaults above.197198For example, you could use `mode="a"` to write "appending" to the same file:199280//// tab | Python 3.7+200{* docs_src/parameter_types/file/tutorial005_an.py hl[5] *}201282```Python hl_lines="5"283{!> ../docs_src/parameter_types/file/tutorial005_an.py!}284```285286////287288//// tab | Python 3.7+ non-Annotated289202/// tip203292Prefer to use the `Annotated` version if possible.293294///295296```Python hl_lines="4"297{!> ../docs_src/parameter_types/file/tutorial005.py!}298```299300////301302/// tip303204As you are manually setting `mode="a"`, you can use `typer.FileText` or `typer.FileTextWrite`, both will work.205206///207
45 unmodified lines
modified:docs/tutorial/parameter-types/index.md+1 added-3 removed
6 unmodified lines
7When you declare a *CLI parameter* with some type **Typer** will convert the data received in the command line to that data type.89For example:1011```Python hl_lines="4"12{!../docs_src/parameter_types/index/tutorial001.py!}13```11{* docs_src/parameter_types/index/tutorial001.py hl[4] *}1213In this example, the value received for the *CLI argument* `NAME` will be treated as `str`.1415The value for the *CLI option* `--age` will be converted to an `int` and `--height-meters` will be converted to a `float`.
54 unmodified lines
modified:docs/tutorial/parameter-types/number.md+3 added-63 removed
1# Number23You can define numeric validations with `max` and `min` values for `int` and `float` *CLI parameters*:45//// tab | Python 3.7+5{* docs_src/parameter_types/number/tutorial001_an.py hl[6:8] *}67```Python hl_lines="6-8"8{!> ../docs_src/parameter_types/number/tutorial001_an.py!}9```1011////1213//// tab | Python 3.7+ non-Annotated1415/// tip1617Prefer to use the `Annotated` version if possible.1819///2021```Python hl_lines="5-7"22{!> ../docs_src/parameter_types/number/tutorial001.py!}23```2425////267*CLI arguments* and *CLI options* can both use these validations.89You can specify `min`, `max` or both.10
61 unmodified lines
72You might want to, instead of showing an error, use the closest minimum or maximum valid values.7374You can do it with the `clamp` parameter:7596//// tab | Python 3.7+76{* docs_src/parameter_types/number/tutorial002_an.py hl[6:8] *}7798```Python hl_lines="6-8"99{!> ../docs_src/parameter_types/number/tutorial002_an.py!}100```101102////103104//// tab | Python 3.7+ non-Annotated105106/// tip107108Prefer to use the `Annotated` version if possible.109110///111112```Python hl_lines="5-7"113{!> ../docs_src/parameter_types/number/tutorial002.py!}114```115116////11778And then, when you pass data that is out of the valid range, it will be "clamped", the closest valid value will be used:7980<div class="termy">81
19 unmodified lines
101## Counter *CLI options*102103You can make a *CLI option* work as a counter with the `counter` parameter:104145//// tab | Python 3.7+105{* docs_src/parameter_types/number/tutorial003_an.py hl[5] *}106147```Python hl_lines="5"148{!> ../docs_src/parameter_types/number/tutorial003_an.py!}149```150151////152153//// tab | Python 3.7+ non-Annotated154155/// tip156157Prefer to use the `Annotated` version if possible.158159///160161```Python hl_lines="4"162{!> ../docs_src/parameter_types/number/tutorial003.py!}163```164165////166107It means that the *CLI option* will be like a boolean flag, e.g. `--verbose`.108109And the value you receive in the function will be the amount of times that `--verbose` was added:110
35 unmodified lines
modified:docs/tutorial/parameter-types/path.md+2 added-42 removed
2 unmodified lines
3You can declare a *CLI parameter* to be a standard Python <a href="https://docs.python.org/3/library/pathlib.html#basic-use" class="external-link" target="_blank">`pathlib.Path`</a>.45This is what you would do for directory paths, file paths, etc:67//// tab | Python 3.7+7{* docs_src/parameter_types/path/tutorial001_an.py hl[1,8] *}89```Python hl_lines="1 8"10{!> ../docs_src/parameter_types/path/tutorial001_an.py!}11```1213////1415//// tab | Python 3.7+ non-Annotated1617/// tip1819Prefer to use the `Annotated` version if possible.2021///2223```Python hl_lines="1 7"24{!> ../docs_src/parameter_types/path/tutorial001.py!}25```2627////289And again, as you receive a standard Python `Path` object the same as the type annotation, your editor will give you autocompletion for all its attributes and methods.1011Check it:12
51 unmodified lines
64///6566For example:6788//// tab | Python 3.7+68{* docs_src/parameter_types/path/tutorial002_an.py hl[11:16] *}6990```Python hl_lines="11-16"91{!> ../docs_src/parameter_types/path/tutorial002_an.py!}92```9394////9596//// tab | Python 3.7+ non-Annotated9798/// tip99100Prefer to use the `Annotated` version if possible.101102///103104```Python hl_lines="9-14"105{!> ../docs_src/parameter_types/path/tutorial002.py!}106```107108////10970Check it:7172<div class="termy">73
39 unmodified lines
modified:docs/tutorial/parameter-types/uuid.md+1 added-3 removed
20 unmodified lines
21///2223You can declare a *CLI parameter* as a UUID:2425```Python hl_lines="1 6 7 8"26{!../docs_src/parameter_types/uuid/tutorial001.py!}27```25{* docs_src/parameter_types/uuid/tutorial001.py hl[1,6,7,8] *}2627Your Python code will receive a standard Python <a href="https://docs.python.org/3.8/library/uuid.html" class="external-link" target="_blank">`UUID`</a> object with all its attributes and methods, and as you are annotating your function parameter with that type, you will have type checks, autocompletion in your editor, etc.2829Check it:
20 unmodified lines
modified:docs/tutorial/subcommands/add-typer.md+3 added-9 removed
8 unmodified lines
9Let's imagine that you are creating a *CLI program* to manage items in some distant land.1011It could be in an `items.py` file with this:1213```Python14{!../docs_src/subcommands/tutorial001/items.py!}15```13{* docs_src/subcommands/tutorial001/items.py *}1415And you would use it like:1617<div class="termy">
11 unmodified lines
29But then you realize that you also have to manage users from your *CLI app*.3031It could be a file `users.py` with something like:3235```Python36{!../docs_src/subcommands/tutorial001/users.py!}37```33{* docs_src/subcommands/tutorial001/users.py *}3435And you would use it like:3637<div class="termy">
15 unmodified lines
53In this case, as with `git remote`, we can put them together as subcommands in another `typer.Typer()` *CLI program*.5455Now create a `main.py` with:5661```Python hl_lines="3 4 7 8"62{!../docs_src/subcommands/tutorial001/main.py!}63```57{* docs_src/subcommands/tutorial001/main.py hl[3,4,7,8] *}5859Here's what we do in `main.py`:6061* Import the other Python modules (the files `users.py` and `items.py`).
107 unmodified lines
modified:docs/tutorial/subcommands/callback-override.md+4 added-12 removed
4 unmodified lines
5When adding a Typer app inside of another, the sub-Typer can also have its own callback.67It can handle any *CLI parameters* that go before its own commands and execute any extra code:89```Python hl_lines="9 10 11"10{!../docs_src/subcommands/callback_override/tutorial001.py!}11```9{* docs_src/subcommands/callback_override/tutorial001.py hl[9,10,11] *}1011In this case it doesn't define any *CLI parameters*, it just writes a message.1213Check it:
13 unmodified lines
27## Add a callback on creation2829It's also possible to add a callback when creating the `typer.Typer()` app that will be added to another Typer app:3033```Python hl_lines="6 7 10"34{!../docs_src/subcommands/callback_override/tutorial002.py!}35```31{* docs_src/subcommands/callback_override/tutorial002.py hl[6,7,10] *}3233This achieves exactly the same as above, it's just another place to add the callback.3435Check it:
14 unmodified lines
50If a callback was added when creating the `typer.Typer()` app, it's possible to override it with a new one using `@app.callback()`.5152This is the same information you saw on the section about [Commands - Typer Callback](../commands/callback.md){.internal-link target=_blank}, and it applies the same for sub-Typer apps:5358```Python hl_lines="6 7 10 14 15 16"59{!../docs_src/subcommands/callback_override/tutorial003.py!}60```54{* docs_src/subcommands/callback_override/tutorial003.py hl[6,7,10,14,15,16] *}5556Here we had defined a callback when creating the `typer.Typer()` sub-app, but then we override it with a new callback with the function `user_callback()`.5758As `@app.callback()` takes precedence over `typer.Typer(callback=some_function)`, now our CLI app will use this new callback.
17 unmodified lines
76Lastly, you can override the callback defined anywhere else when adding a sub-Typer with `app.add_typer()` using the `callback` parameter.7778This has the highest priority:7986```Python hl_lines="13 14 17"87{!../docs_src/subcommands/callback_override/tutorial004.py!}88```80{* docs_src/subcommands/callback_override/tutorial004.py hl[13,14,17] *}8182Notice that the precedence goes to `app.add_typer()` and is not affected by the order of execution. There's another callback defined below, but the one from `app.add_typer()` wins.8384Now when you use the CLI program it will use the new callback function `callback_for_add_typer()`.
14 unmodified lines
modified:docs/tutorial/subcommands/name-and-help.md+8 added-24 removed
10 unmodified lines
11## Add a help text1213We can also set the `help` while adding a Typer:1415```Python hl_lines="6"16{!../docs_src/subcommands/name_help/tutorial001.py!}17```15{* docs_src/subcommands/name_help/tutorial001.py hl[6] *}1617And then we get that help text for that command in the *CLI program*:1819<div class="termy">
68 unmodified lines
88And if that Typer app is added to another Typer app, the default name of the command is generated from the name of the callback function.8990Here's an example:9194```Python hl_lines="6 9 10 11 12 13"95{!../docs_src/subcommands/name_help/tutorial002.py!}96```92{* docs_src/subcommands/name_help/tutorial002.py hl[6,9,10,11,12,13] *}9394Notice that now we added the sub-Typer without specifying a `name` nor a `help`.9596They are now inferred from the callback function.
45 unmodified lines
142This has the lowest priority, we'll see later what has a higher priority and can override it.143144Check the code:145150```Python hl_lines="6 7 8 9 12"151{!../docs_src/subcommands/name_help/tutorial003.py!}152```146{* docs_src/subcommands/name_help/tutorial003.py hl[6,7,8,9,12] *}147148This achieves exactly the same as the previous example.149150Check it:
35 unmodified lines
186### Override a callback set in `typer.Typer()` with `@app.callback()`187188The same as with normal **Typer** apps, if you pass a `callback` to `typer.Typer(callback=some_function)` and then override it with `@app.callback()`, the name and help text will be inferred from the new callback:189196```Python hl_lines="16 17 18 19 20"197{!../docs_src/subcommands/name_help/tutorial004.py!}198```190{* docs_src/subcommands/name_help/tutorial004.py hl[16,17,18,19,20] *}191192Now the name of the command will be `users` instead of `old-callback`, and the help text will be `Manage users in the app.` instead of `Old callback help.`.193194Check it:
39 unmodified lines
234This takes precedence over inferring the name and help from a callback set in `@sub_app.callback()` and `typer.Typer(callback=sub_app_callback)`.235236Check the code:237246```Python hl_lines="15 16 17 18 21"247{!../docs_src/subcommands/name_help/tutorial005.py!}248```238{* docs_src/subcommands/name_help/tutorial005.py hl[15,16,17,18,21] *}239240Now the command will be `new-users` instead of `users`. And the help text will be `I have the highland! Create some users.` instead of the previous ones.241242Check it:
61 unmodified lines
304If you set it explicitly, that takes precedence over inferring.305306You can set it when creating a new `typer.Typer()`:307318```Python hl_lines="12"319{!../docs_src/subcommands/name_help/tutorial006.py!}320```308{* docs_src/subcommands/name_help/tutorial006.py hl[12] *}309310/// info311312The rest of the callbacks and overrides are there only to show you that they don't affect the name and help text when you set it explicitly.
45 unmodified lines
358Any parameter that you use when creating a `typer.Typer()` app can be overridden in the parameters of `@app.callback()`.359360Continuing with the previous example, we now override the values in `@user_app.callback()`:361374```Python hl_lines="24"375{!../docs_src/subcommands/name_help/tutorial007.py!}376```362{* docs_src/subcommands/name_help/tutorial007.py hl[24] *}363364And now the command name will be `call-users` and the help text will be `Help from callback for users.`.365366Check it:
35 unmodified lines
402### Name and help in `app.add_typer()`403404And finally, with the highest priority, you can override all that by explicitly setting the `name` and `help` in `app.add_typer()`, just like we did on the first example above:405420```Python hl_lines="21"421{!../docs_src/subcommands/name_help/tutorial008.py!}422```406{* docs_src/subcommands/name_help/tutorial008.py hl[21] *}407408And now, with the highest priorities of them all, the command name will now be `cake-sith-users` and the help text will be `Unlimited powder! Eh, users.`.409410Check it:
47 unmodified lines
modified:docs/tutorial/subcommands/nested-subcommands.md+10 added-30 removed
10 unmodified lines
11## A CLI app for reigns1213Let's start with a file `reigns.py`:1415```Python16{!../docs_src/subcommands/tutorial003/reigns.py!}17```15{* docs_src/subcommands/tutorial003/reigns.py *}1617This is already a simple *CLI program* to manage reigns:1819<div class="termy">
28 unmodified lines
48## A CLI app for towns4950And now the equivalent for managing towns in `towns.py`:5154```Python55{!../docs_src/subcommands/tutorial003/towns.py!}56```52{* docs_src/subcommands/tutorial003/towns.py *}5354With it, you can manage towns:5556<div class="termy">
28 unmodified lines
85## Manage the land in a CLI app8687Now let's put the `reigns` and `towns` together in the same *CLI program* in `lands.py`:8893```Python94{!../docs_src/subcommands/tutorial003/lands.py!}95```89{* docs_src/subcommands/tutorial003/lands.py *}9091And now we have a single *CLI program* with a command (or command group) `reigns` that has its own commands. And another command `towns` with its own subcommands.9293Check it:
92 unmodified lines
186This already is a quite deeply nested "tree" of commands/command groups.187188But to achieve that, we just have to add the `lands` **Typer** app to the same `main.py` file we already had:189196```Python hl_lines="4 10"197{!../docs_src/subcommands/tutorial003/main.py!}198```190{* docs_src/subcommands/tutorial003/main.py hl[4,10] *}191192And now we have everything in a single *CLI program*:193194<div class="termy">
42 unmodified lines
237Here are all the files if you want to review/copy them:238239`reigns.py`:240249```Python250{!../docs_src/subcommands/tutorial003/reigns.py!}251```241{* docs_src/subcommands/tutorial003/reigns.py *}242243`towns.py`:244255```Python256{!../docs_src/subcommands/tutorial003/towns.py!}257```245{* docs_src/subcommands/tutorial003/towns.py *}246247`lands.py`:248261```Python262{!../docs_src/subcommands/tutorial003/lands.py!}263```249{* docs_src/subcommands/tutorial003/lands.py *}250251`users.py`:252267```Python268{!../docs_src/subcommands/tutorial003/users.py!}269```253{* docs_src/subcommands/tutorial003/users.py *}254255`items.py`:256273```Python274{!../docs_src/subcommands/tutorial003/items.py!}275```257{* docs_src/subcommands/tutorial003/items.py *}258259`main.py`:260279```Python280{!../docs_src/subcommands/tutorial003/main.py!}281```261{* docs_src/subcommands/tutorial003/main.py *}262263/// tip264265All these files have an `if __name__ == "__main__"` block just to demonstrate how each of them can also be an independent *CLI app*.
18 unmodified lines
modified:docs/tutorial/subcommands/single-file.md+4 added-12 removed
2 unmodified lines
3In some cases, it's possible that your application code needs to live on a single file.45You can still use the same ideas:67```Python8{!../docs_src/subcommands/tutorial002/main.py!}9```7{* docs_src/subcommands/tutorial002/main.py *}89There are several things to notice here...1011## Apps at the top
1 unmodified line
13First, you can create `typer.Typer()` objects and add them to another one at the top.1415It doesn't have to be done after creating the subcommands:1619```Python hl_lines="4 5 6 7"20{!../docs_src/subcommands/tutorial002/main.py!}21```17{* docs_src/subcommands/tutorial002/main.py hl[4,5,6,7] *}1819You can add the commands (subcommands) to each `typer.Typer()` app later and it will still work.2021## Function names
1 unmodified line
23As you now have subcommands like `create` for `users` and for `items`, you can no longer call the functions with just the name, like `def create()`, because they would overwrite each other.2425So we use longer names:2631```Python hl_lines="11 16 21 26 31"32{!../docs_src/subcommands/tutorial002/main.py!}33```27{* docs_src/subcommands/tutorial002/main.py hl[11,16,21,26,31] *}2829## Command name3031We are naming the functions with longer names so that they don't overwrite each other.
23 unmodified lines
55</div>5657So we pass the name we want to use for each subcommand as the function argument to the decorator:5865```Python hl_lines="10 15 20 25 30"66{!../docs_src/subcommands/tutorial002/main.py!}67```59{* docs_src/subcommands/tutorial002/main.py hl[10,15,20,25,30] *}6061## Check it6263It still works the same:
79 unmodified lines
modified:docs/tutorial/app-dir.md+1 added-3 removed
1# CLI Application Directory23You can get the application directory where you can, for example, save configuration files with `typer.get_app_dir()`:45```Python hl_lines="9"6{!../docs_src/app_dir/tutorial001.py!}7```5{* docs_src/app_dir/tutorial001.py hl[9] *}67It will give you a directory for storing configurations appropriate for your CLI program for the current user in each operating system.89Check it:
34 unmodified lines
modified:docs/tutorial/exceptions.md+4 added-12 removed
6 unmodified lines
7## Example Broken App89Let's take this example broken app:1011```Python hl_lines="5"12{!../docs_src/exceptions/tutorial001.py!}13```11{* docs_src/exceptions/tutorial001.py hl[5] *}1213This code is broken because you can't sum a string and a number (`name + 3`).1415## Exceptions with Rich
68 unmodified lines
84But if it was something like a password, you would have liked to hide it.8586In that case, you can create the `typer.Typer()` application explicitly and set the parameter `pretty_exceptions_show_locals=False`:8790```Python hl_lines="3"91{!../docs_src/exceptions/tutorial002.py!}92```88{* docs_src/exceptions/tutorial002.py hl[3] *}8990And now when you run it, you will see the error without the local variables:9192<div class="termy">
24 unmodified lines
117## Disable Short Output118119If you want to show the full exception, including the parts in Typer and Click, you can use the parameter `pretty_exceptions_short=False`:120125```Python hl_lines="3"126{!../docs_src/exceptions/tutorial003.py!}127```121{* docs_src/exceptions/tutorial003.py hl[3] *}122123Now when you run it, you will see the whole output:124125<div class="termy">
73 unmodified lines
199## Disable Pretty Exceptions200201You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`:202209```Python hl_lines="3"210{!../docs_src/exceptions/tutorial004.py!}211```203{* docs_src/exceptions/tutorial004.py hl[3] *}204205And now you will see the full standard exception as with any other Python program:206207<div class="termy">
66 unmodified lines
modified:docs/tutorial/first-steps.md+6 added-18 removed
2 unmodified lines
3## The simplest example45The simplest **Typer** file could look like this:67```Python8{!../docs_src/first_steps/tutorial001.py!}9```7{* docs_src/first_steps/tutorial001.py *}89Copy that to a file `main.py`.1011Test it:
48 unmodified lines
60## Add a CLI argument6162Update the previous example with an argument `name`:6366```Python hl_lines="4 5"67{!../docs_src/first_steps/tutorial002.py!}68```64{* docs_src/first_steps/tutorial002.py hl[4,5] *}6566<div class="termy">6768```console
33 unmodified lines
102Now let's say we want to have the name and last name separated.103104So, extend that to have 2 arguments, `name` and `lastname`:105110```Python hl_lines="4 5"111{!../docs_src/first_steps/tutorial003.py!}112```106{* docs_src/first_steps/tutorial003.py hl[4,5] *}107108<div class="termy">109110```console
116 unmodified lines
227## Add one *CLI option*228229Now add a `--formal` *CLI option*:230237```Python hl_lines="4 5"238{!../docs_src/first_steps/tutorial004.py!}239```231{* docs_src/first_steps/tutorial004.py hl[4,5] *}232233Here `formal` is a `bool` that is `False` by default.234235<div class="termy">
52 unmodified lines
288## A *CLI option* with a value289290To convert the `lastname` from a *CLI argument* to a *CLI option*, give it a default value of `""`:291300```Python hl_lines="4"301{!../docs_src/first_steps/tutorial005.py!}302```292{* docs_src/first_steps/tutorial005.py hl[4] *}293294As `lastname` now has a default value of `""` (an empty string) it is no longer required in the function, and **Typer** will now by default make it an optional *CLI option*.295296<div class="termy">
62 unmodified lines
359## Document your CLI app360361If you add a <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr> to your function it will be used in the help text:362373```Python hl_lines="5 6 7 8 9"374{!../docs_src/first_steps/tutorial006.py!}375```363{* docs_src/first_steps/tutorial006.py hl[5,6,7,8,9] *}364365Now see it with the `--help` option:366367<div class="termy">
142 unmodified lines
modified:docs/tutorial/launch.md+2 added-6 removed
2 unmodified lines
3You can launch applications from your CLI program with `typer.launch()`.45It will launch the appropriate application depending on the URL or file type you pass it:67```Python hl_lines="6"8{!../docs_src/launch/tutorial001.py!}9```7{* docs_src/launch/tutorial001.py hl[6] *}89Check it:1011<div class="termy">
11 unmodified lines
23## Locating a file2425You can also make the operating system open the file browser indicating where a file is located with `locate=True`:2629```Python hl_lines="17"30{!../docs_src/launch/tutorial002.py!}31```27{* docs_src/launch/tutorial002.py hl[17] *}2829/// tip3031The rest of the code in this example is just making sure the app directory exists and creating the config file.
18 unmodified lines
modified:docs/tutorial/options-autocompletion.md+9 added-189 removed
14 unmodified lines
15To check it quickly without creating a new Python package, use the `typer` command.1617Then let's create small example program:1819//// tab | Python 3.7+19{* docs_src/options_autocompletion/tutorial001_an.py *}2021```Python22{!> ../docs_src/options_autocompletion/tutorial001_an.py!}23```2425////2627//// tab | Python 3.7+ non-Annotated2829/// tip3031Prefer to use the `Annotated` version if possible.3233///3435```Python36{!> ../docs_src/options_autocompletion/tutorial001.py!}37```3839////4021And let's try it with the `typer` command to get completion:2223<div class="termy">24
24 unmodified lines
49Right now we get completion for the *CLI option* names, but not for the values.5051We can provide completion for the values creating an `autocompletion` function, similar to the `callback` functions from [CLI Option Callback and Context](./options/callback-and-context.md){.internal-link target=_blank}:5273//// tab | Python 3.7+53{* docs_src/options_autocompletion/tutorial002_an.py hl[5:6,15] *}5475```Python hl_lines="5-6 15"76{!> ../docs_src/options_autocompletion/tutorial002_an.py!}77```7879////8081//// tab | Python 3.7+ non-Annotated8283/// tip8485Prefer to use the `Annotated` version if possible.8687///8889```Python hl_lines="4-5 14"90{!> ../docs_src/options_autocompletion/tutorial002.py!}91```9293////9455We return a `list` of strings from the `complete_name()` function.5657And then we get those values when using completion:58
19 unmodified lines
78Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.7980Then we can check and return only the values that start with the incomplete value from the command line:81122//// tab | Python 3.7+82{* docs_src/options_autocompletion/tutorial003_an.py hl[7:12] *}83124```Python hl_lines="7-12"125{!> ../docs_src/options_autocompletion/tutorial003_an.py!}126```127128////129130//// tab | Python 3.7+ non-Annotated131132/// tip133134Prefer to use the `Annotated` version if possible.135136///137138```Python hl_lines="6-11"139{!> ../docs_src/options_autocompletion/tutorial003.py!}140```141142////14384Now let's try it:8586<div class="termy">87
29 unmodified lines
117In the `complete_name()` function, instead of providing one `str` per completion element, we provide a `tuple` with 2 items. The first item is the actual completion string, and the second item is the help text.118119So, in the end, we return a `list` of `tuples` of `str`:120181//// tab | Python 3.7+121{* docs_src/options_autocompletion/tutorial004_an.py hl[4:8,11:17] *}122183```Python hl_lines="4-8 11-17"184{!> ../docs_src/options_autocompletion/tutorial004_an.py!}185```186187////188189//// tab | Python 3.7+ non-Annotated190123/// tip124193Prefer to use the `Annotated` version if possible.194195///196197```Python hl_lines="3-7 10-16"198{!> ../docs_src/options_autocompletion/tutorial004.py!}199```200201////202203/// tip204125If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.126127Click checks specifically for a `tuple` when extracting the help text.128
29 unmodified lines
158Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.159160That way our function will be a <a href="https://docs.python.org/3.8/glossary.html#index-19" class="external-link" target="_blank">generator</a> that **Typer** (actually Click) can iterate:161242//// tab | Python 3.7+162{* docs_src/options_autocompletion/tutorial005_an.py hl[11:14] *}163244```Python hl_lines="11-14"245{!> ../docs_src/options_autocompletion/tutorial005_an.py!}246```247248////249250//// tab | Python 3.7+ non-Annotated251252/// tip253254Prefer to use the `Annotated` version if possible.255256///257258```Python hl_lines="10-13"259{!> ../docs_src/options_autocompletion/tutorial005.py!}260```261262////263164That simplifies our code a bit and works the same.165166/// tip167
26 unmodified lines
194///195196For this we use a `List` of `str`:197298//// tab | Python 3.7+198{* docs_src/options_autocompletion/tutorial006_an.py hl[9:14] *}199300```Python hl_lines="9-14"301{!> ../docs_src/options_autocompletion/tutorial006_an.py!}302```303304////305306//// tab | Python 3.7+ non-Annotated307308/// tip309310Prefer to use the `Annotated` version if possible.311312///313314```Python hl_lines="8-11"315{!> ../docs_src/options_autocompletion/tutorial006.py!}316```317318////319200And then we can use it like:201202<div class="termy">203
15 unmodified lines
219But you can access the context by declaring a function parameter of type `typer.Context`.220221And from that context you can get the current values for each parameter.222343//// tab | Python 3.7+223{* docs_src/options_autocompletion/tutorial007_an.py hl[13:14,16] *}224345```Python hl_lines="13-14 16"346{!> ../docs_src/options_autocompletion/tutorial007_an.py!}347```348349////350351//// tab | Python 3.7+ non-Annotated352353/// tip354355Prefer to use the `Annotated` version if possible.356357///358359```Python hl_lines="12-13 15"360{!> ../docs_src/options_autocompletion/tutorial007.py!}361```362363////364225We are getting the `names` already provided with `--name` in the command line before this completion was triggered.226227If there's no `--name` in the command line, it will be `None`, so we use `or []` to make sure we have a `list` (even if empty) to check its contents later.228
66 unmodified lines
295You can print to "standard error" with a **Rich** `Console(stderr=True)`.296297Using `stderr=True` tells **Rich** that the output should be shown in "standard error".298439//// tab | Python 3.7+299{* docs_src/options_autocompletion/tutorial008_an.py hl[13,16:17] *}300441```Python hl_lines="13 16-17"442{!> ../docs_src/options_autocompletion/tutorial008_an.py!}443```444445////446447//// tab | Python 3.7+ non-Annotated448449/// tip450451Prefer to use the `Annotated` version if possible.452453///454455```Python hl_lines="12 15-16"456{!> ../docs_src/options_autocompletion/tutorial008.py!}457```458459////460301/// info302303If you can't install and use Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.304
38 unmodified lines
343## Getting the Context and the raw *CLI parameters*344345Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:346507//// tab | Python 3.7+347{* docs_src/options_autocompletion/tutorial009_an.py hl[16] *}348509```Python hl_lines="16"510{!> ../docs_src/options_autocompletion/tutorial009_an.py!}511```512513////514515//// tab | Python 3.7+ non-Annotated516517/// tip518519Prefer to use the `Annotated` version if possible.520521///522523```Python hl_lines="15"524{!> ../docs_src/options_autocompletion/tutorial009.py!}525```526527////528349Check it:350351<div class="termy">352
34 unmodified lines
modified:docs/tutorial/printing.md+7 added-21 removed
1# Printing and Colors23You can use the normal `print()` to show information on the screen:45```Python hl_lines="5"6{!../docs_src/first_steps/tutorial001.py!}7```5{* docs_src/first_steps/tutorial001.py hl[5] *}67It will show the output normally:89<div class="termy">
13 unmodified lines
23### Use Rich `print`2425For the simplest cases, you can just import `print` from `rich` and use it instead of the standard `print`:2629```Python hl_lines="2 15"30{!../docs_src/printing/tutorial001.py!}31```27{* docs_src/printing/tutorial001.py hl[2,15] *}2829Just with that, **Rich** will be able to print your data with nice colors and structure:3031<div class="termy">
19 unmodified lines
51### Rich Markup5253Rich also supports a <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">custom markup syntax</a> to set colors and styles, for example:5459```Python hl_lines="6"60{!../docs_src/printing/tutorial002.py!}61```55{* docs_src/printing/tutorial002.py hl[6] *}5657<div class="termy">5859```console
15 unmodified lines
75When you call Rich's `print`, it automatically creates this object and uses it.7677But for advanced use cases, you could create a `Console` yourself.7885```Python hl_lines="2-3 5 9-12"86{!../docs_src/printing/tutorial003.py!}87```79{* docs_src/printing/tutorial003.py hl[2:3,5,9:12] *}8081In this example, we create a `Console`, and a `Table`. And then we can add some rows to the table, and print it.8283If you run it, you will see a nicely formatted table:
67 unmodified lines
151///152153Using `stderr=True` tells **Rich** that the output should be shown in "standard error".154163```Python hl_lines="4 8"164{!../docs_src/printing/tutorial004.py!}165```155{* docs_src/printing/tutorial004.py hl[4,8] *}156157When you try it in the terminal, it will probably just look the same:158159<div class="termy">
67 unmodified lines
227///228229You can create colored strings to output to the terminal with `typer.style()`, that gives you `str`s that you can then pass to `typer.echo()`:230241```Python hl_lines="7 9"242{!../docs_src/printing/tutorial005.py!}243```231{* docs_src/printing/tutorial005.py hl[7,9] *}232233/// tip234235The parameters `fg` and `bg` receive strings with the color names for the "**f**ore**g**round" and "**b**ack**g**round" colors. You could simply pass `fg="green"` and `bg="red"`.
37 unmodified lines
273///274275There's a shorter form to style and print at the same time with `typer.secho()` it's like `typer.echo()` but also adds style like `typer.style()`:276289```Python hl_lines="5"290{!../docs_src/printing/tutorial006.py!}291```277{* docs_src/printing/tutorial006.py hl[5] *}278279Check it:280281<div class="use-termynal" data-termynal>
3 unmodified lines
modified:docs/tutorial/progressbar.md+6 added-18 removed
4 unmodified lines
5## Progress Bar67You can use <a href="https://rich.readthedocs.io/en/stable/progress.html" class="external-link" target="_blank">Rich's Progress Display</a> to show a progress bar, for example:89```Python hl_lines="4 9"10{!../docs_src/progressbar/tutorial001.py!}11```9{* docs_src/progressbar/tutorial001.py hl[4,9] *}1011You put the thing that you want to iterate over inside of Rich's `track()`, and then iterate over that.1213Check it:
31 unmodified lines
45Rich allows you to display many things in complex and advanced ways.4647For example, this will show two spinners:4851```Python hl_lines="4 8-15"52{!../docs_src/progressbar/tutorial002.py!}53```49{* docs_src/progressbar/tutorial002.py hl[4,8:15] *}5051I can't show you the beautiful animation here in the docs. 😅5253But at some point in time it will look like this (imagine it's spinning). 🤓
45 unmodified lines
99```100101And you pass as function argument to `typer.progressbar()` the thing that you would normally iterate over.102107```Python hl_lines="8"108{!../docs_src/progressbar/tutorial003.py!}109```103{* docs_src/progressbar/tutorial003.py hl[8] *}104105So, if you have a list of users, this could be:106107```Python
54 unmodified lines
162The progress bar is generated from the length of the iterable (e.g. the list of users).163164But if the length is not available (for example, with something that fetches a new user from a web API each time) you can pass an explicit `length` to `typer.progressbar()`.165172```Python hl_lines="14"173{!../docs_src/progressbar/tutorial004.py!}174```166{* docs_src/progressbar/tutorial004.py hl[14] *}167168Check it:169170<div class="termy">
39 unmodified lines
210///211212You can also set a `label`:213222```Python hl_lines="8"223{!../docs_src/progressbar/tutorial005.py!}224```214{* docs_src/progressbar/tutorial005.py hl[8] *}215216Check it:217218<div class="use-termynal">
7 unmodified lines
226If you need to manually iterate over something and update the progress bar irregularly, you can do it by not passing an iterable but just a `length` to `typer.progressbar()`.227228And then calling the `.update()` method in the object from the `with` statement:229240```Python hl_lines="8 12"241{!../docs_src/progressbar/tutorial006.py!}242```230{* docs_src/progressbar/tutorial006.py hl[8,12] *}231232Check it:233234<div class="use-termynal">
4 unmodified lines
modified:docs/tutorial/prompt.md+4 added-12 removed
2 unmodified lines
3When you need to ask the user for info interactively you should normally use [*CLI Option*s with Prompt](options/prompt.md){.internal-link target=_blank}, because they allow using the CLI program in a non-interactive way (for example, a Bash script could use it).45But if you absolutely need to ask for interactive information without using a *CLI option*, you can use `typer.prompt()`:67```Python hl_lines="5"8{!../docs_src/prompt/tutorial001.py!}9```7{* docs_src/prompt/tutorial001.py hl[5] *}89Check it:1011<div class="termy">
11 unmodified lines
23## Confirm2425There's also an alternative to ask for confirmation. Again, if possible, you should use a [*CLI Option* with a confirmation prompt](options/prompt.md){.internal-link target=_blank}:2629```Python hl_lines="5"30{!../docs_src/prompt/tutorial002.py!}31```27{* docs_src/prompt/tutorial002.py hl[5] *}2829Check it:3031<div class="termy">
19 unmodified lines
51## Confirm or abort5253As it's very common to abort if the user doesn't confirm, there's an integrated parameter `abort` that does it automatically:5459```Python hl_lines="5"60{!../docs_src/prompt/tutorial003.py!}61```55{* docs_src/prompt/tutorial003.py hl[5] *}5657<div class="termy">5859```console
16 unmodified lines
76## Prompt with Rich7778If you installed Rich as described in [Printing and Colors](printing.md){.internal-link target=_blank}, you can use Rich to prompt the user for input:7986```Python hl_lines="2 6"87{!../docs_src/prompt/tutorial004.py!}88```80{* docs_src/prompt/tutorial004.py hl[2,6] *}8182And when you run it, it will look like:8384<div class="termy">
10 unmodified lines
modified:docs/tutorial/terminating.md+3 added-9 removed
10 unmodified lines
11This doesn't have to mean that there's an error, just that nothing else needs to be executed.1213In that case, you can raise a `typer.Exit()` exception:1415```Python hl_lines="9"16{!../docs_src/terminating/tutorial001.py!}17```15{* docs_src/terminating/tutorial001.py hl[9] *}1617There are several things to see in this example.1819* The CLI program is the function `main()`, not the others. This is the one that takes a *CLI argument*.
35 unmodified lines
55`typer.Exit()` takes an optional `code` parameter. By default, `code` is `0`, meaning there was no error.5657You can pass a `code` with a number other than `0` to tell the terminal that there was an error in the execution of the program:5861```Python hl_lines="7"62{!../docs_src/terminating/tutorial002.py!}63```59{* docs_src/terminating/tutorial002.py hl[7] *}6061Check it:6263<div class="termy">
34 unmodified lines
98There's a special exception that you can use to "abort" a program.99100It works more or less the same as `typer.Exit()` but will print `"Aborted!"` to the screen and can be useful in certain cases later to make it explicit that the execution was aborted:101106```Python hl_lines="7"107{!../docs_src/terminating/tutorial003.py!}108```102{* docs_src/terminating/tutorial003.py hl[7] *}103104Check it:105106<div class="termy">
14 unmodified lines
modified:docs/tutorial/testing.md+8 added-42 removed
2 unmodified lines
3Testing **Typer** applications is very easy with <a href="https://docs.pytest.org/en/latest/" class="external-link" target="_blank">pytest</a>.45Let's say you have an application `app/main.py` with:67```Python8{!../docs_src/testing/app01/main.py!}9```7{* docs_src/testing/app01/main.py *}89So, you would use it like:1011<div class="termy">
20 unmodified lines
32Import `CliRunner` and create a `runner` object.3334This runner is what will "invoke" or "call" your command line application.3538```Python hl_lines="1 5"39{!../docs_src/testing/app01/test_main.py!}40```36{* docs_src/testing/app01/test_main.py hl[1,5] *}3738/// tip3940It's important that the name of the file starts with `test_`, that way pytest will be able to detect it and use it automatically.
9 unmodified lines
50The first parameter to `runner.invoke()` is a `Typer` app.5152The second parameter is a `list` of `str`, with all the text you would pass in the command line, right as you would pass it:5358```Python hl_lines="8 9"59{!../docs_src/testing/app01/test_main.py!}60```54{* docs_src/testing/app01/test_main.py hl[8,9] *}5556/// tip5758The name of the function has to start with `test_`, that way pytest can detect it and use it automatically.
3 unmodified lines
62### Check the result6364Then, inside of the test function, add `assert` statements to ensure that everything in the result of the call is as it should be.6572```Python hl_lines="10 11 12"73{!../docs_src/testing/app01/test_main.py!}74```66{* docs_src/testing/app01/test_main.py hl[10,11,12] *}6768Here we are checking that the exit code is 0, as it is for programs that exit without errors.6970Then we check that the text printed to "standard output" contains the text that our CLI program prints.
37 unmodified lines
108## Testing input109110If you have a CLI with prompts, like:111120//// tab | Python 3.7+112{* docs_src/testing/app02_an/main.py hl[8] *}113122```Python hl_lines="8"123{!> ../docs_src/testing/app02_an/main.py!}124```125126////127128//// tab | Python 3.7+ non-Annotated129130/// tip131132Prefer to use the `Annotated` version if possible.133134///135136```Python hl_lines="7"137{!> ../docs_src/testing/app02/main.py!}138```139140////141114That you would use like:115116<div class="termy">117
20 unmodified lines
138When you hit the <kbd>ENTER</kbd> key after typing the email, that is just a "new line character". And in Python that is represented with `"\n"`.139140So, if you use `input="camila@example.com\n"` it means: "type `camila@example.com` in the terminal, then hit the <kbd>ENTER</kbd> key":141170```Python hl_lines="9"171{!../docs_src/testing/app02/test_main.py!}172```142{* docs_src/testing/app02/test_main.py hl[9] *}143144## Test a function145146If you have a script and you never created an explicit `typer.Typer` app, like:147178```Python hl_lines="9"179{!../docs_src/testing/app03/main.py!}180```148{* docs_src/testing/app03/main.py hl[9] *}149150...you can still test it, by creating an app during testing:151184```Python hl_lines="6 7 13"185{!../docs_src/testing/app03/test_main.py!}186```152{* docs_src/testing/app03/test_main.py hl[6,7,13] *}153154Of course, if you are testing that script, it's probably easier/cleaner to just create the explicit `typer.Typer` app in `main.py` instead of creating it just during the test.155156But if you want to keep it that way, e.g. because it's a simple example in documentation, then you can use that trick.
58 unmodified lines
modified:docs/tutorial/typer-command.md+1 added-3 removed
239 unmodified lines
240### Sample script with docs241242For example, you could have a script like:243244```Python245{!../docs_src/commands/help/tutorial001.py!}246```244{* docs_src/commands/help/tutorial001.py *}245246### Generate docs with the `typer` command247248Then you could generate docs for it with the `typer` command.
131 unmodified lines
modified:docs/tutorial/using-click.md+4 added-12 removed
26 unmodified lines
27A `Command` can have its own *CLI arguments* and *CLI options*, and it has a function that it calls.2829For example, in this Click app:3031```Python hl_lines="7 14"32{!../docs_src/using_click/tutorial001.py!}33```31{* docs_src/using_click/tutorial001.py hl[7,14] *}3233The original `hello` variable is converted by Click from a function to a `Command` object. And the original `hello` function is used by that `Command` internally, but it is no longer named `hello` (as `hello` is now a Click `Command`).3435#### Click `Group`
7 unmodified lines
43And a `Group` can also have a function that it calls, right before calling the function for any specific subcommand.4445For example:4649```Python hl_lines="5 19 20"50{!../docs_src/using_click/tutorial002.py!}51```47{* docs_src/using_click/tutorial002.py hl[5,19,20] *}4849The `cli` variable is converted by Click from a function to a `Group` object. And the original `cli` function is used by that `Group` internally.5051/// tip
33 unmodified lines
85### Including a Click app in a **Typer** app8687For example, you could have a **Typer** app, generate a Click `Group` from it, and then include other Click apps in it:8893```Python hl_lines="15 16 29 31 34"94{!../docs_src/using_click/tutorial003.py!}95```89{* docs_src/using_click/tutorial003.py hl[15,16,29,31,34] *}9091Notice that we add a callback that does nothing (only document the CLI program), to make sure **Typer** creates a Click `Group`. That way we can add sub-commands to that Click `Group`.9293Then we generate a Click object from our `typer.Typer` app (`typer_click_object`), and then we can include another Click object (`hello`) in this Click `Group`.
35 unmodified lines
129### Including a **Typer** app in a Click app130131The same way, you can do the contrary and include a **Typer** sub app in a bigger Click app:132139```Python hl_lines="31 33 36"140{!../docs_src/using_click/tutorial004.py!}141```133{* docs_src/using_click/tutorial004.py hl[31,33,36] *}134135Notice that we don't have to add a callback or more commands, we can just create a **Typer** app that generates a single Click `Command`, as we don't need to include anything under the Typer app.136137Then we generate a Click object from our `typer.Typer` app (`typer_click_object`), and then we use **the Click `cli` to include** our Click object from our Typer app.
56 unmodified lines
modified:mkdocs.yml+1 added-1 removed
190 unmodified lines
191 alternate_style: True192193 # Other extensions194 mdx_include:195 base_path: docs195 markdown_include_variants:196197extra:198 analytics:199 provider: google
38 unmodified lines
modified:requirements-docs.txt+1 added
14 unmodified lines
15# griffe-typingdoc==0.2.516# For griffe, it formats with black17# black==24.3.018mkdocs-macros-plugin==1.3.719markdown-include-variants==0.0.3