📝 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
Provenance
- Commit
12459d2- Tree
958fdd3- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
f802688
50 files changed+186 added−1855 removed
modified:docs/tutorial/arguments/default.md+2 added-42 removed
6 unmodified lines78910111112131415161718192013142324252627282930313233341516171832 unmodified lines5152535475555677787980818283848586878889909192939495965758596042 unmodified lines6 unmodified lines## An optional *CLI argument* with a default
We can also use `typer.Argument()` to make a *CLI argument* have a default value other than `None`:
//// tab | Python 3.7+{* docs_src/arguments/default/tutorial001_an.py hl[5] *}
```Python hl_lines="5"{!> ../docs_src/arguments/default/tutorial001_an.py!}```
////
//// tab | Python 3.7+ non-Annotated
/// tip
Prefer to use the `Annotated` version if possible.
///
```Python hl_lines="4"{!> ../docs_src/arguments/default/tutorial001.py!}```
////
/// tip
Because now the value will be a `str` passed by the user or the default value of `"Wade Wilson"` which is also a `str`, we know the value will never be `None`, so we don't have to (and shouldn't) use `Optional[str]`.
Have in mind that the `Optional[something]` tells Python that a value "could be `None`". But the use of `Optional` doesn't affect Typer in any way, e.g. it doesn't tell Typer if a value is required or not.
32 unmodified lines## Dynamic default value
And we can even make the default value be dynamically generated by passing a function as the `default_factory` argument:
//// tab | Python 3.7+{* docs_src/arguments/default/tutorial002_an.py hl[7:8,11] *}
```Python hl_lines="7-8 11"{!> ../docs_src/arguments/default/tutorial002_an.py!}```
////
//// tab | Python 3.7+ non-Annotated
/// tip
Prefer to use the `Annotated` version if possible.
///
```Python hl_lines="6-7 10"{!> ../docs_src/arguments/default/tutorial002.py!}```
////
In this case, we created the function `get_name` that will just return a random `str` each time.
And we pass it as the first function argument to `typer.Argument()`.
42 unmodified linesmodified:docs/tutorial/arguments/envvar.md+3 added-63 removed
8 unmodified lines9 ///10 11 To do that, use the `envvar` parameter for `typer.Argument()`:12 13-//// tab | Python 3.7+13+{* docs_src/arguments/envvar/tutorial001_an.py hl[5] *}14 15-```Python hl_lines="5"16-{!> ../docs_src/arguments/envvar/tutorial001_an.py!}17-```18-19-////20-21-//// tab | Python 3.7+ non-Annotated22-23-/// tip24-25-Prefer to use the `Annotated` version if possible.26-27-///28-29-```Python hl_lines="4"30-{!> ../docs_src/arguments/envvar/tutorial001.py!}31-```32-33-////34-15 In 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:16 17 <div class="termy">18 35 unmodified lines54 ## Multiple environment variables55 56 You 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:57 78-//// tab | Python 3.7+58+{* docs_src/arguments/envvar/tutorial002_an.py hl[6] *}59 80-```Python hl_lines="6"81-{!> ../docs_src/arguments/envvar/tutorial002_an.py!}82-```83-84-////85-86-//// tab | Python 3.7+ non-Annotated87-88-/// tip89-90-Prefer to use the `Annotated` version if possible.91-92-///93-94-```Python hl_lines="4"95-{!> ../docs_src/arguments/envvar/tutorial002.py!}96-```97-98-////99-60 Check it:61 62 <div class="termy">63 25 unmodified lines89 ## Hide an env var from the help text90 91 By default, environment variables used will be shown in the help text, but you can disable them with `show_envvar=False`:92 133-//// tab | Python 3.7+93+{* docs_src/arguments/envvar/tutorial003_an.py hl[7] *}94 135-```Python hl_lines="7"136-{!> ../docs_src/arguments/envvar/tutorial003_an.py!}137-```138-139-////140-141-//// tab | Python 3.7+ non-Annotated142-143-/// tip144-145-Prefer to use the `Annotated` version if possible.146-147-///148-149-```Python hl_lines="4"150-{!> ../docs_src/arguments/envvar/tutorial003.py!}151-```152-153-////154-95 Check it:96 97 <div class="termy">98 28 unmodified linesmodified:docs/tutorial/arguments/help.md+9 added-171 removed
2 unmodified lines3 In 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>.4 5 Here's how that last example looked like:6 7-```Python8-{!../docs_src/first_steps/tutorial006.py!}9-```7+{* docs_src/first_steps/tutorial006.py *}8 9 Now that you also know how to use `typer.Argument()`, let's use it to add documentation specific for a *CLI argument*.10 11 ## Add a `help` text for a *CLI argument*12 13 You can use the `help` parameter to add a help text for a *CLI argument*:14 17-//// tab | Python 3.7+15+{* docs_src/arguments/help/tutorial001_an.py hl[5] *}16 19-```Python hl_lines="5"20-{!> ../docs_src/arguments/help/tutorial001_an.py!}21-```22-23-////24-25-//// tab | Python 3.7+ non-Annotated26-27-/// tip28-29-Prefer to use the `Annotated` version if possible.30-31-///32-33-```Python hl_lines="4"34-{!> ../docs_src/arguments/help/tutorial001.py!}35-```36-37-////38-17 And it will be used in the automatic `--help` option:18 19 <div class="termy">20 15 unmodified lines36 ## Combine help text and docstrings37 38 And 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>:39 62-//// tab | Python 3.7+40+{* docs_src/arguments/help/tutorial002_an.py hl[5:8] *}41 64-```Python hl_lines="5-8"65-{!> ../docs_src/arguments/help/tutorial002_an.py!}66-```67-68-////69-70-//// tab | Python 3.7+ non-Annotated71-72-/// tip73-74-Prefer to use the `Annotated` version if possible.75-76-///77-78-```Python hl_lines="4-7"79-{!> ../docs_src/arguments/help/tutorial002.py!}80-```81-82-////83-42 And the `--help` option will combine all the information:43 44 <div class="termy">45 17 unmodified lines63 ## Help with defaults64 65 If you have a *CLI argument* with a default value, like `"World"`:66 109-//// tab | Python 3.7+67+{* docs_src/arguments/help/tutorial003_an.py hl[5] *}68 111-```Python hl_lines="5"112-{!> ../docs_src/arguments/help/tutorial003_an.py!}113-```114-115-////116-117-//// tab | Python 3.7+ non-Annotated118-119-/// tip120-121-Prefer to use the `Annotated` version if possible.122-123-///124-125-```Python hl_lines="4"126-{!> ../docs_src/arguments/help/tutorial003.py!}127-```128-129-////130-69 It will show that default value in the help text:70 71 <div class="termy">72 15 unmodified lines88 </div>89 90 But you can disable that if you want to, with `show_default=False`:91 154-//// tab | Python 3.7+92+{* docs_src/arguments/help/tutorial004_an.py hl[7] *}93 156-```Python hl_lines="7"157-{!> ../docs_src/arguments/help/tutorial004_an.py!}158-```159-160-////161-162-//// tab | Python 3.7+ non-Annotated163-164-/// tip165-166-Prefer to use the `Annotated` version if possible.167-168-///169-170-```Python hl_lines="4"171-{!> ../docs_src/arguments/help/tutorial004.py!}172-```173-174-////175-94 And then it won't show the default value:95 96 <div class="termy">97 25 unmodified lines123 ## Custom default string124 125 You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:126 209-//// tab | Python 3.7+127+{* docs_src/arguments/help/tutorial005_an.py hl[9] *}128 211-```Python hl_lines="9"212-{!> ../docs_src/arguments/help/tutorial005_an.py!}213-```214-215-////216-217-//// tab | Python 3.7+ non-Annotated218-219-/// tip220-221-Prefer to use the `Annotated` version if possible.222-223-///224-225-```Python hl_lines="6"226-{!> ../docs_src/arguments/help/tutorial005.py!}227-```228-229-////230-129 And it will be used in the help text:130 131 <div class="termy">132 35 unmodified lines168 But you can customize it with the `metavar` parameter for `typer.Argument()`.169 170 For 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:171 274-//// tab | Python 3.7+172+{* docs_src/arguments/help/tutorial006_an.py hl[5] *}173 276-```Python hl_lines="5"277-{!> ../docs_src/arguments/help/tutorial006_an.py!}278-```279-280-////281-282-//// tab | Python 3.7+ non-Annotated283-284-/// tip285-286-Prefer to use the `Annotated` version if possible.287-288-///289-290-```Python hl_lines="4"291-{!> ../docs_src/arguments/help/tutorial006.py!}292-```293-294-////295-174 Now the generated help text will have `✨username✨` instead of `NAME`:175 176 <div class="termy">177 16 unmodified lines194 You might want to show the help information for *CLI arguments* in different panels when using the `--help` option.195 196 If 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:197 320-//// tab | Python 3.7+198+{* docs_src/arguments/help/tutorial007_an.py hl[8,12] *}199 322-```Python hl_lines="8 12"323-{!> ../docs_src/arguments/help/tutorial007_an.py!}324-```325-326-////327-328-//// tab | Python 3.7+ non-Annotated329-330-/// tip331-332-Prefer to use the `Annotated` version if possible.333-334-///335-336-```Python hl_lines="7 10"337-{!> ../docs_src/arguments/help/tutorial007.py!}338-```339-340-////341-200 Then, 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`.201 202 And 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 lines237 If you want, you can make a *CLI argument* **not** show up in the `Arguments` section in the help text.238 239 You will probably not want to do this normally, but it's possible:240 383-//// tab | Python 3.7+241+{* docs_src/arguments/help/tutorial008_an.py hl[5] *}242 385-```Python hl_lines="5"386-{!> ../docs_src/arguments/help/tutorial008_an.py!}387-```388-389-////390-391-//// tab | Python 3.7+ non-Annotated392-393-/// tip394-395-Prefer to use the `Annotated` version if possible.396-397-///398-399-```Python hl_lines="4"400-{!> ../docs_src/arguments/help/tutorial008.py!}401-```402-403-////404-243 Check it:244 245 <div class="termy">246 47 unmodified linesmodified:docs/tutorial/arguments/optional.md+6 added-18 removed
33 unmodified lines34 ## An alternative *CLI argument* declaration35 36 In the [First Steps](../first-steps.md#add-a-cli-argument){.internal-link target=_blank} you saw how to add a *CLI argument*:37 38-```Python hl_lines="4"39-{!../docs_src/first_steps/tutorial002.py!}40-```38+{* docs_src/first_steps/tutorial002.py hl[4] *}39 40 Now let's see an alternative way to create the same *CLI argument*:41 42 45-```Python hl_lines="5"46-{!> ../docs_src/arguments/optional/tutorial001_an.py!}47-```43+{* docs_src/arguments/optional/tutorial001_an.py hl[5] *}44 45 /// info46 47 Typer added support for `Annotated` (and started recommending it) in version 0.9.0.62 unmodified lines110 Now, finally what we came for, an optional *CLI argument*.111 112 To make a *CLI argument* optional, use `typer.Argument()` and pass a different "default" as the first parameter to `typer.Argument()`, for example `None`:113 118-```Python hl_lines="7"119-{!../docs_src/arguments/optional/tutorial002_an.py!}120-```114+{* docs_src/arguments/optional/tutorial002_an.py hl[7] *}115 116 Now we have:117 118 ```Python64 unmodified lines183 **Typer** also supports another older alternative syntax for declaring *CLI arguments* with additional metadata.184 185 Instead of using `Annotated`, you can use `typer.Argument()` as the default value:186 193-```Python hl_lines="4"194-{!> ../docs_src/arguments/optional/tutorial001.py!}195-```187+{* docs_src/arguments/optional/tutorial001.py hl[4] *}188 189 /// tip190 191 Prefer to use the `Annotated` version if possible.25 unmodified lines217 If 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>.218 219 ///220 229-```Python hl_lines="4"230-{!> ../docs_src/arguments/optional/tutorial003.py!}231-```221+{* docs_src/arguments/optional/tutorial003.py hl[4] *}222 223 And the same way, you can make it optional by passing a different `default` value, for example `None`:224 235-```Python hl_lines="6"236-{!> ../docs_src/arguments/optional/tutorial002.py!}237-```225+{* docs_src/arguments/optional/tutorial002.py hl[6] *}226 227 Because 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`.228 229 The `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 linesmodified:docs/tutorial/commands/arguments.md+1 added-3 removed
12345675678936 unmodified lines# Command CLI Arguments
The same way as with a CLI application with a single command, subcommands (or just "commands") can also have their own *CLI arguments*:
```Python hl_lines="7 12"{!../docs_src/commands/arguments/tutorial001.py!}```{* docs_src/commands/arguments/tutorial001.py hl[7,12] *}
<div class="termy">
```console36 unmodified linesmodified:docs/tutorial/commands/callback.md+4 added-12 removed
10 unmodified lines11 But we can use `@app.callback()` for that.12 13 It's very similar to `@app.command()`, but it declares the *CLI parameters* for the main CLI application (before the commands):14 15-```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] *}16 17 Here we create a `callback` with a `--verbose` *CLI option*.18 19 /// tip58 unmodified lines78 ## Adding a callback on creation79 80 It's also possible to add a callback when creating the `typer.Typer()` app:81 84-```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] *}83 84 That achieves the same as with `@app.callback()`.85 86 Check it:12 unmodified lines99 ## Overriding a callback100 101 If you added a callback when creating the `typer.Typer()` app, it's possible to override it with `@app.callback()`:102 107-```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] *}104 105 Now `new_callback()` will be the one used.106 107 Check it:15 unmodified lines123 You can also add a callback just to add the documentation in the docstring.124 125 It can be convenient especially if you have several lines of text, as the indentation will be automatically handled for you:126 133-```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] *}128 129 Now the callback will be used mainly to extract the docstring for the help text.130 131 Check it:54 unmodified linesmodified:docs/tutorial/commands/context.md+4 added-12 removed
12 unmodified lines13 For example, let's say that you want to execute some logic in a `Typer` callback depending on the subcommand that is being called.14 15 You can get the name of the subcommand from the context:16 17-```Python hl_lines="17 21"18-{!../docs_src/commands/context/tutorial001.py!}19-```17+{* docs_src/commands/context/tutorial001.py hl[17,21] *}18 19 Check it:20 21 <div class="termy">21 unmodified lines43 And if no command is provided, the help message is shown.44 45 But we could make it run even without a subcommand with `invoke_without_command=True`:46 49-```Python hl_lines="16"50-{!../docs_src/commands/context/tutorial002.py!}51-```47+{* docs_src/commands/context/tutorial002.py hl[16] *}48 49 Check it:50 51 <div class="termy">21 unmodified lines73 For that, we can get the `typer.Context` and check if there's an invoked command in `ctx.invoked_subcommand`.74 75 If it's `None`, it means that we are not calling a subcommand but the main program (the callback) directly:76 81-```Python hl_lines="17 21"82-{!../docs_src/commands/context/tutorial003.py!}83-```77+{* docs_src/commands/context/tutorial003.py hl[17,21] *}78 79 Check it:80 81 <div class="termy">22 unmodified lines104 For example, you could keep additional *CLI parameters* not declared in your CLI program with `ignore_unknown_options` and `allow_extra_args`.105 106 Then you can access those extra raw *CLI parameters* as a `list` of `str` in `ctx.args`:107 114-```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] *}109 110 <div class="termy">111 112 ```console15 unmodified linesmodified:docs/tutorial/commands/help.md+8 added-100 removed
2 unmodified lines3 The same as before, you can add help for the commands in the docstrings and the *CLI options*.4 5 And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:6 7-//// tab | Python 3.7+7+{* docs_src/commands/help/tutorial001_an.py hl[4,9:11,22,26:30,43,47:51,60:62] *}8 9-```Python hl_lines="4 9-11 22 26-30 43 47-51 60-62"10-{!> ../docs_src/commands/help/tutorial001_an.py!}11-```12-13-////14-15-//// tab | Python 3.7+ non-Annotated16-17-/// tip18-19-Prefer to use the `Annotated` version if possible.20-21-///22-23-```Python hl_lines="3 8-10 20 23-27 39 42-46 55-57"24-{!> ../docs_src/commands/help/tutorial001.py!}25-```26-27-////28-9 Check it:10 11 <div class="termy">12 78 unmodified lines91 ## Overwrite command help92 93 You 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()`:94 115-```Python hl_lines="6 14"116-{!../docs_src/commands/help/tutorial002.py!}117-```95+{* docs_src/commands/help/tutorial002.py hl[6,14] *}96 97 Check it:98 99 <div class="termy">25 unmodified lines125 There 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.126 127 You can mark it with the parameter `deprecated=True`:128 151-```Python hl_lines="14"152-{!../docs_src/commands/help/tutorial003.py!}153-```129+{* docs_src/commands/help/tutorial003.py hl[14] *}130 131 And when you show the `--help` option you will see it's marked as "`deprecated`":132 133 <div class="termy">57 unmodified lines191 ### Rich Markup192 193 If 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:194 219-//// tab | Python 3.7+195+{* docs_src/commands/help/tutorial004_an.py hl[4,10,14:16,21,24,27] *}196 221-```Python hl_lines="4 10 14-16 21 24 27"222-{!> ../docs_src/commands/help/tutorial004_an.py!}223-```224-225-////226-227-//// tab | Python 3.7+ non-Annotated228-229-/// tip230-231-Prefer to use the `Annotated` version if possible.232-233-///234-235-```Python hl_lines="3 9 13-15 20 22 24"236-{!> ../docs_src/commands/help/tutorial004.py!}237-```238-239-////240-197 With 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>.198 199 You can also use markup in the help for the `username` CLI Argument.200 53 unmodified lines254 ### Rich Markdown255 256 If you set `rich_markup_mode="markdown"` when creating the `typer.Typer()` app, you will be able to use Markdown in the docstring:257 302-//// tab | Python 3.7+258+{* docs_src/commands/help/tutorial005_an.py hl[4,9,12:20,25,27:28] *}259 304-```Python hl_lines="4 9 12-20 25 27-28"305-{!> ../docs_src/commands/help/tutorial005_an.py!}306-```307-308-////309-310-//// tab | Python 3.7+ non-Annotated311-312-/// tip313-314-Prefer to use the `Annotated` version if possible.315-316-///317-318-```Python hl_lines="3 7 9-17 22 24-25"319-{!> ../docs_src/commands/help/tutorial005.py!}320-```321-322-////323-260 With 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>.261 262 And the same as before, the help text overwritten for the command `delete` can also use Markdown.263 66 unmodified lines330 ### Help Panels for Commands331 332 To set the panel for a command you can pass the argument `rich_help_panel` with the name of the panel you want to use:333 398-//// tab | Python 3.7+334+{* docs_src/commands/help/tutorial006.py hl[22,30,38,46] *}335 400-```Python hl_lines="22 30 38 46"401-{!> ../docs_src/commands/help/tutorial006.py!}402-```403-404-////405-336 Commands without a panel will be shown in the default panel `Commands`, and the rest will be shown in the next panels:337 338 <div class="termy">339 31 unmodified lines371 The same way, you can configure the panels for *CLI arguments* and *CLI options* with `rich_help_panel`.372 373 And of course, in the same application you can also set the `rich_help_panel` for commands.374 445-//// tab | Python 3.7+375+{* docs_src/commands/help/tutorial007_an.py hl[15,21,27,37] *}376 447-```Python hl_lines="15 21 27 37"448-{!> ../docs_src/commands/help/tutorial007_an.py!}449-```450-451-////452-453-//// tab | Python 3.7+ non-Annotated454-455-/// tip456-457-Prefer to use the `Annotated` version if possible.458-459-///460-461-```Python hl_lines="12 16 21 30"462-{!> ../docs_src/commands/help/tutorial007.py!}463-```464-465-////466-377 Then if you run the application you will see all the *CLI parameters* in their respective panels.378 379 * 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 lines449 ## Epilog450 451 If you need, you can also add an epilog section to the help of your commands:452 543-```Python hl_lines="6"544-{!../docs_src/commands/help/tutorial008.py!}545-```453+{* docs_src/commands/help/tutorial008.py hl[6] *}454 455 And when you check the `--help` option it will look like:456 457 <div class="termy">24 unmodified linesmodified:docs/tutorial/commands/index.md+5 added-15 removed
52 unmodified lines53 In the *CLI options* and *CLI argument* tutorials you have seen how to create a single function and then pass that function to `typer.run()`.54 55 For example:56 57-```Python hl_lines="9"58-{!../docs_src/first_steps/tutorial002.py!}59-```57+{* docs_src/first_steps/tutorial002.py hl[9] *}58 59 But 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()`.60 61 There's also a more explicit way to achieve the same:62 65-```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] *}64 65 When you use `typer.run()`, **Typer** is doing more or less the same as above, it will:66 67 * Create a new `typer.Typer()` "application".107 unmodified lines175 We'll have a command to `create` users and another command to `delete` them.176 177 To begin, let's say it can only create and delete one single predefined user:178 183-```Python hl_lines="6 11"184-{!../docs_src/commands/index/tutorial002.py!}185-```179+{* docs_src/commands/index/tutorial002.py hl[6,11] *}180 181 Now we have a CLI application with 2 commands, `create` and `delete`:182 183 <div class="termy">40 unmodified lines224 By default, we need to specify `--help` to get the command's help page.225 226 However, by setting `no_args_is_help=True` when defining the `typer.Typer()` application, the help function will be shown whenever no argument is given:227 234-```Python hl_lines="3"235-{!../docs_src/commands/index/tutorial003.py!}236-```228+{* docs_src/commands/index/tutorial003.py hl[3] *}229 230 Now we can run this:231 232 <div class="termy">22 unmodified lines255 Note that by design, **Typer** shows the commands in the order they've been declared.256 257 So, if we take our original example, with `create` and `delete` commands, and reverse the order in the Python file:258 267-```Python hl_lines="7 12"268-{!../docs_src/commands/index/tutorial004.py!}269-```259+{* docs_src/commands/index/tutorial004.py hl[7,12] *}260 261 Then we will see the `delete` command first in the help output:262 263 <div class="termy">46 unmodified linesmodified:docs/tutorial/commands/name.md+1 added-3 removed
15 unmodified lines16 And what if you wanted the command to still be named `create`?17 18 For this, you can set the name of the command in the first parameter for the `@app.command()` decorator:19 20-```Python hl_lines="6 11"21-{!../docs_src/commands/name/tutorial001.py!}22-```20+{* docs_src/commands/name/tutorial001.py hl[6,11] *}21 22 Now, even though the functions are named `cli_create_user()` and `cli_delete_user()`, the commands will still be named `create` and `delete`:23 24 <div class="termy">32 unmodified linesmodified:docs/tutorial/commands/one-or-multiple.md+4 added-12 removed
1 # One or Multiple Commands2 3 You might have noticed that if you create a single command, as in the first example:4 5-```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] *}6 7 **Typer** is smart enough to create a CLI application with that single function as the main CLI application, not as a command/subcommand:8 9 <div class="termy">32 unmodified lines42 ///43 44 But if you add multiple commands, **Typer** will create one *CLI command* for each one of them:45 48-```Python hl_lines="6 11"49-{!../docs_src/commands/index/tutorial002.py!}50-```46+{* docs_src/commands/index/tutorial002.py hl[6,11] *}47 48 Here we have 2 commands `create` and `delete`:49 50 <div class="termy">28 unmodified lines79 ## One command and one callback80 81 If 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:82 87-```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] *}84 85 And now your CLI program will have a single command.86 87 Check it:26 unmodified lines114 ## Using the callback to document115 116 Now that you are using a callback just to have a single command, you might as well use it to add documentation for your app:117 124-```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] *}119 120 And now the docstring from the callback will be used as the help text:121 122 <div class="termy">26 unmodified linesmodified:docs/tutorial/commands/options.md+1 added-21 removed
2 unmodified lines3 Commands can also have their own *CLI options*.4 5 In fact, each command can have different *CLI arguments* and *CLI options*:6 7-//// tab | Python 3.7+7+{* docs_src/commands/options/tutorial001_an.py hl[8,14:17,27:29,38] *}8 9-```Python hl_lines="8 14-17 27-29 38"10-{!> ../docs_src/commands/options/tutorial001_an.py!}11-```12-13-////14-15-//// tab | Python 3.7+ non-Annotated16-17-/// tip18-19-Prefer to use the `Annotated` version if possible.20-21-///22-23-```Python hl_lines="7 13-14 24 33"24-{!> ../docs_src/commands/options/tutorial001.py!}25-```26-27-////28-9 Here we have multiple commands, with different *CLI parameters*:10 11 * `create`:12 * `username`: a *CLI argument*.87 unmodified linesmodified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+2 added-24 removed
2 unmodified lines3 *CLI arguments* can also receive multiple values.4 5 You can define the type of a *CLI argument* using `typing.List`.6 7-```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] *}8 9 And then you can pass it as many *CLI arguments* of that type as you want:10 11 <div class="termy">24 unmodified lines36 ## *CLI arguments* with tuples37 38 If you want a specific number of values and types, you can use a tuple, and it can even have default values:39 42-//// tab | Python 3.7+40+{* docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py hl[8:10] *}41 44-```Python hl_lines="8-10"45-{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py!}46-```47-48-////49-50-//// tab | Python 3.7+ non-Annotated51-52-/// tip53-54-Prefer to use the `Annotated` version if possible.55-56-///57-58-```Python hl_lines="7-8"59-{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002.py!}60-```61-62-////63-42 Check it:43 44 <div class="termy">45 33 unmodified linesmodified:docs/tutorial/multiple-values/multiple-options.md+2 added-42 removed
4 unmodified lines5 For example, let's say you want to accept several users in a single execution.6 7 For this, use the standard Python `typing.List` to declare it as a `list` of `str`:8 9-//// tab | Python 3.7+9+{* docs_src/multiple_values/multiple_options/tutorial001_an.py hl[1,7] *}10 11-```Python hl_lines="1 7"12-{!> ../docs_src/multiple_values/multiple_options/tutorial001_an.py!}13-```14-15-////16-17-//// tab | Python 3.7+ non-Annotated18-19-/// tip20-21-Prefer to use the `Annotated` version if possible.22-23-///24-25-```Python hl_lines="1 6"26-{!> ../docs_src/multiple_values/multiple_options/tutorial001.py!}27-```28-29-////30-11 You will receive the values as you declared them, as a `list` of `str`.12 13 Check it:14 24 unmodified lines39 ## Multiple `float`40 41 The same way, you can use other types and they will be converted by **Typer** to their declared type:42 63-//// tab | Python 3.7+43+{* docs_src/multiple_values/multiple_options/tutorial002_an.py hl[7] *}44 65-```Python hl_lines="7"66-{!> ../docs_src/multiple_values/multiple_options/tutorial002_an.py!}67-```68-69-////70-71-//// tab | Python 3.7+ non-Annotated72-73-/// tip74-75-Prefer to use the `Annotated` version if possible.76-77-///78-79-```Python hl_lines="6"80-{!> ../docs_src/multiple_values/multiple_options/tutorial002.py!}81-```82-83-////84-45 Check it:46 47 <div class="termy">48 17 unmodified linesmodified:docs/tutorial/multiple-values/options-with-multiple-values.md+1 added-21 removed
4 unmodified lines5 You can set the number of values and types to anything you want, but it has to be a fixed number of values.6 7 For this, use the standard Python `typing.Tuple`:8 9-//// tab | Python 3.7+9+{* docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py hl[1,7] *}10 11-```Python hl_lines="1 7"12-{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py!}13-```14-15-////16-17-//// tab | Python 3.7+ non-Annotated18-19-/// tip20-21-Prefer to use the `Annotated` version if possible.22-23-///24-25-```Python hl_lines="1 6"26-{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001.py!}27-```28-29-////30-11 Each of the internal types defines the type of each value in the tuple.12 13 So:14 79 unmodified linesmodified:docs/tutorial/options/callback-and-context.md+4 added-84 removed
6 unmodified lines7 ## Validate *CLI parameters*8 9 For example, you could do some validation before the rest of the code is executed.10 11-//// tab | Python 3.7+11+{* docs_src/options/callback/tutorial001_an.py hl[7:10,13] *}12 13-```Python hl_lines="7-10 13"14-{!> ../docs_src/options/callback/tutorial001_an.py!}15-```16-17-////18-19-//// tab | Python 3.7+ non-Annotated20-21-/// tip22-23-Prefer to use the `Annotated` version if possible.24-25-///26-27-```Python hl_lines="6-9 12"28-{!> ../docs_src/options/callback/tutorial001.py!}29-```30-31-////32-13 Here you pass a function to `typer.Option()` or `typer.Argument()` with the keyword argument `callback`.14 15 The function receives the value from the command line. It can do anything with it, and then return the value.16 76 unmodified lines93 ### Breaking completion in a callback94 95 Let's say that when the callback is running, we want to show a message saying that it's validating the name:96 117-//// tab | Python 3.7+97+{* docs_src/options/callback/tutorial002_an.py hl[8] *}98 119-```Python hl_lines="8"120-{!> ../docs_src/options/callback/tutorial002_an.py!}121-```122-123-////124-125-//// tab | Python 3.7+ non-Annotated126-127-/// tip128-129-Prefer to use the `Annotated` version if possible.130-131-///132-133-```Python hl_lines="7"134-{!> ../docs_src/options/callback/tutorial002.py!}135-```136-137-////138-99 And 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.100 101 It will look something like:102 25 unmodified lines128 But you can access the context by declaring a function parameter of type `typer.Context`.129 130 The "context" has some additional data about the current execution of your program:131 172-//// tab | Python 3.7+132+{* docs_src/options/callback/tutorial003_an.py hl[7:9] *}133 174-```Python hl_lines="7-9"175-{!> ../docs_src/options/callback/tutorial003_an.py!}176-```177-178-////179-180-//// tab | Python 3.7+ non-Annotated181-182-/// tip183-184-Prefer to use the `Annotated` version if possible.185-186-///187-188-```Python hl_lines="6-8"189-{!> ../docs_src/options/callback/tutorial003.py!}190-```191-192-////193-134 The `ctx.resilient_parsing` will be `True` when handling completion, so you can just return without printing anything else.135 136 But it will be `False` when calling the program normally. So you can continue the execution of your previous code.137 22 unmodified lines160 ## Using the `CallbackParam` object161 162 The 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.163 224-//// tab | Python 3.7+164+{* docs_src/options/callback/tutorial004_an.py hl[7,10] *}165 226-```Python hl_lines="7 10"227-{!> ../docs_src/options/callback/tutorial004_an.py!}228-```229-230-////231-232-//// tab | Python 3.7+ non-Annotated233-234-/// tip235-236-Prefer to use the `Annotated` version if possible.237-238-///239-240-```Python hl_lines="6 9"241-{!> ../docs_src/options/callback/tutorial004.py!}242-```243-244-////245-166 It's probably not very common, but you could do it if you need it.167 168 For 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 linesmodified:docs/tutorial/options/help.md+4 added-84 removed
2 unmodified lines3 You already saw how to add a help text for *CLI arguments* with the `help` parameter.4 5 Let's now do the same for *CLI options*:6 7-//// tab | Python 3.7+7+{* docs_src/options/help/tutorial001_an.py hl[7:8] *}8 9-```Python hl_lines="7-8"10-{!> ../docs_src/options/help/tutorial001_an.py!}11-```12-13-////14-15-//// tab | Python 3.7+ non-Annotated16-17-/// tip18-19-Prefer to use the `Annotated` version if possible.20-21-///22-23-```Python hl_lines="6-7"24-{!> ../docs_src/options/help/tutorial001.py!}25-```26-27-////28-9 The same way as with `typer.Argument()`, we can put `typer.Option()` inside of `Annotated`.10 11 We can then pass the `help` keyword parameter:12 42 unmodified lines55 The same as with *CLI arguments*, you can put the help for some *CLI options* in different panels to be shown with the `--help` option.56 57 If 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*:58 79-//// tab | Python 3.7+59+{* docs_src/options/help/tutorial002_an.py hl[11,17] *}60 81-```Python hl_lines="11 17"82-{!> ../docs_src/options/help/tutorial002_an.py!}83-```84-85-////86-87-//// tab | Python 3.7+ non-Annotated88-89-/// tip90-91-Prefer to use the `Annotated` version if possible.92-93-///94-95-```Python hl_lines="8 11"96-{!> ../docs_src/options/help/tutorial002.py!}97-```98-99-////100-61 Now, 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`.62 63 And 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 lines101 ## Hide default from help102 103 You can tell Typer to not show the default value in the help text with `show_default=False`:104 145-//// tab | Python 3.7+105+{* docs_src/options/help/tutorial003_an.py hl[5] *}106 147-```Python hl_lines="5"148-{!> ../docs_src/options/help/tutorial003_an.py!}149-```150-151-////152-153-//// tab | Python 3.7+ non-Annotated154-155-/// tip156-157-Prefer to use the `Annotated` version if possible.158-159-///160-161-```Python hl_lines="4"162-{!> ../docs_src/options/help/tutorial003.py!}163-```164-165-////166-107 And it will no longer show the default value in the help text:108 109 <div class="termy">110 27 unmodified lines138 ## Custom default string139 140 You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:141 202-//// tab | Python 3.7+142+{* docs_src/options/help/tutorial004_an.py hl[7] *}143 204-```Python hl_lines="7"205-{!> ../docs_src/options/help/tutorial004_an.py!}206-```207-208-////209-210-//// tab | Python 3.7+ non-Annotated211-212-/// tip213-214-Prefer to use the `Annotated` version if possible.215-216-///217-218-```Python hl_lines="6"219-{!> ../docs_src/options/help/tutorial004.py!}220-```221-222-////223-144 And it will be used in the help text:145 146 <div class="termy">147 18 unmodified linesmodified:docs/tutorial/options/name.md+5 added-109 removed
26 unmodified lines27 Let's say the function parameter name is `user_name` as above, but you want the *CLI option* to be just `--name`.28 29 You can pass the *CLI option* name that you want to have in the following positional argument passed to `typer.Option()`:30 31-//// tab | Python 3.7+31+{* docs_src/options/name/tutorial001_an.py hl[5] *}32 33-```Python hl_lines="5"34-{!> ../docs_src/options/name/tutorial001_an.py!}35-```36-37-Here you are passing the string `"--name"` as the first positional argument to `typer.Option()`.38-39-////40-41-//// tab | Python 3.7+ non-Annotated42-43-/// tip44-45-Prefer to use the `Annotated` version if possible.46-47-///48-49-```Python hl_lines="4"50-{!> ../docs_src/options/name/tutorial001.py!}51-```52-53-Here you are passing the string `"--name"` as the second positional argument to `typer.Option()`, as the first argument is `...` to mark it as required.54-55-////56-33 /// info34 35 "<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 lines184 You can overwrite the *CLI option* name to use as in the previous example, but you can also declare extra alternatives, including short names.185 186 For example, extending the previous example, let's add a *CLI option* short name `-n`:187 212-//// tab | Python 3.7+188+{* docs_src/options/name/tutorial002_an.py hl[5] *}189 214-```Python hl_lines="5"215-{!> ../docs_src/options/name/tutorial002_an.py!}216-```217-218-////219-220-//// tab | Python 3.7+ non-Annotated221-222-/// tip223-224-Prefer to use the `Annotated` version if possible.225-226-///227-228-```Python hl_lines="4"229-{!> ../docs_src/options/name/tutorial002.py!}230-```231-232-////233-190 Here 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`.191 192 Check it:193 21 unmodified lines215 ### *CLI option* only short name216 217 If 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.218 263-//// tab | Python 3.7+219+{* docs_src/options/name/tutorial003_an.py hl[5] *}220 265-```Python hl_lines="5"266-{!> ../docs_src/options/name/tutorial003_an.py!}267-```268-269-////270-271-//// tab | Python 3.7+ non-Annotated272-273-/// tip274-275-Prefer to use the `Annotated` version if possible.276-277-///278-279-```Python hl_lines="4"280-{!> ../docs_src/options/name/tutorial003.py!}281-```282-283-////284-221 Check it:222 223 <div class="termy">224 18 unmodified lines243 ### *CLI option* short name and default244 245 Continuing 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:246 311-//// tab | Python 3.7+247+{* docs_src/options/name/tutorial004_an.py hl[5] *}248 313-```Python hl_lines="5"314-{!> ../docs_src/options/name/tutorial004_an.py!}315-```316-317-////318-319-//// tab | Python 3.7+ non-Annotated320-321-/// tip322-323-Prefer to use the `Annotated` version if possible.324-325-///326-327-```Python hl_lines="4"328-{!> ../docs_src/options/name/tutorial004.py!}329-```330-331-////332-249 Check it:250 251 <div class="termy">252 24 unmodified lines277 You can create multiple short names and use them together.278 279 You don't have to do anything special for it to work (apart from declaring those short versions):280 365-//// tab | Python 3.7+281+{* docs_src/options/name/tutorial005_an.py hl[6:7] *}282 367-```Python hl_lines="6-7"368-{!> ../docs_src/options/name/tutorial005_an.py!}369-```370-371-////372-373-//// tab | Python 3.7+ non-Annotated374-283 /// tip284 377-Prefer to use the `Annotated` version if possible.378-379-///380-381-```Python hl_lines="5-6"382-{!> ../docs_src/options/name/tutorial005.py!}383-```384-385-////386-387-/// tip388-285 Notice that, again, we are declaring the long and short version of the *CLI option* names.286 287 ///288 29 unmodified linesmodified:docs/tutorial/options/password.md+2 added-42 removed
1 # Password CLI Option and Confirmation Prompt2 3 Apart from having a prompt, you can make a *CLI option* have a `confirmation_prompt=True`:4 5-//// tab | Python 3.7+5+{* docs_src/options/password/tutorial001_an.py hl[7] *}6 7-```Python hl_lines="7"8-{!> ../docs_src/options/password/tutorial001_an.py!}9-```10-11-////12-13-//// tab | Python 3.7+ non-Annotated14-15-/// tip16-17-Prefer to use the `Annotated` version if possible.18-19-///20-21-```Python hl_lines="5"22-{!> ../docs_src/options/password/tutorial001.py!}23-```24-25-////26-7 And the CLI program will ask for confirmation:8 9 <div class="termy">10 18 unmodified lines29 You can achieve the same using `hide_input=True`.30 31 And if you combine it with `confirmation_prompt=True` you can easily receive a password with double confirmation:32 53-//// tab | Python 3.7+33+{* docs_src/options/password/tutorial002_an.py hl[8] *}34 55-```Python hl_lines="8"56-{!> ../docs_src/options/password/tutorial002_an.py!}57-```58-59-////60-61-//// tab | Python 3.7+ non-Annotated62-63-/// tip64-65-Prefer to use the `Annotated` version if possible.66-67-///68-69-```Python hl_lines="6-8"70-{!> ../docs_src/options/password/tutorial002.py!}71-```72-73-////74-35 Check it:36 37 <div class="termy">38 13 unmodified linesmodified:docs/tutorial/options/prompt.md+3 added-63 removed
1 # CLI Option Prompt2 3 It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:4 5-//// tab | Python 3.7+5+{* docs_src/options/prompt/tutorial001_an.py hl[5] *}6 7-```Python hl_lines="5"8-{!> ../docs_src/options/prompt/tutorial001_an.py!}9-```10-11-////12-13-//// tab | Python 3.7+ non-Annotated14-15-/// tip16-17-Prefer to use the `Annotated` version if possible.18-19-///20-21-```Python hl_lines="4"22-{!> ../docs_src/options/prompt/tutorial001.py!}23-```24-25-////26-7 And then your program will ask the user for it in the terminal:8 9 <div class="termy">10 12 unmodified lines23 ## Customize the prompt24 25 You can also set a custom prompt, passing the string that you want to use instead of just `True`:26 47-//// tab | Python 3.7+27+{* docs_src/options/prompt/tutorial002_an.py hl[7] *}28 49-```Python hl_lines="7"50-{!> ../docs_src/options/prompt/tutorial002_an.py!}51-```52-53-////54-55-//// tab | Python 3.7+ non-Annotated56-57-/// tip58-59-Prefer to use the `Annotated` version if possible.60-61-///62-63-```Python hl_lines="5"64-{!> ../docs_src/options/prompt/tutorial002.py!}65-```66-67-////68-29 And then your program will ask for it using with your custom prompt:30 31 <div class="termy">32 16 unmodified lines49 You can do it passing the parameter `confirmation_prompt=True`.50 51 Let's say it's a CLI app to delete a project:52 93-//// tab | Python 3.7+53+{* docs_src/options/prompt/tutorial003_an.py hl[6] *}54 95-```Python hl_lines="6"96-{!> ../docs_src/options/prompt/tutorial003_an.py!}97-```98-99-////100-101-//// tab | Python 3.7+ non-Annotated102-103-/// tip104-105-Prefer to use the `Annotated` version if possible.106-107-///108-109-```Python hl_lines="4"110-{!> ../docs_src/options/prompt/tutorial003.py!}111-```112-113-////114-55 And it will prompt the user for a value and then for the confirmation:56 57 <div class="termy">58 26 unmodified linesmodified:docs/tutorial/options/required.md+3 added-21 removed
11 unmodified lines12 To make a *CLI option* required, you can put `typer.Option()` inside of `Annotated` and leave the parameter without a default value.13 14 Let's make `--lastname` a required *CLI option*:15 16-//// tab | Python 3.7+16+{* docs_src/options/required/tutorial001_an.py hl[5] *}17 18-```Python hl_lines="5"19-{!> ../docs_src/options/required/tutorial001_an.py!}20-```21-22-////23-18 The 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.19 26-//// tab | Python 3.7+ non-Annotated20+{* docs_src/options/required/tutorial001.py hl[4] *}21 28-```Python hl_lines="4"29-{!> ../docs_src/options/required/tutorial001.py!}30-```31-32-////33-22 Or you can explictily pass `...` to `typer.Option(default=...)`:23 36-//// tab | Python 3.7+ non-Annotated24+{* docs_src/options/required/tutorial002.py hl[4] *}25 38-```Python hl_lines="4"39-{!> ../docs_src/options/required/tutorial002.py!}40-```41-42-////43-26 /// info27 28 If 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 linesmodified:docs/tutorial/options/version.md+3 added-63 removed
6 unmodified lines7 ## First version of `--version`8 9 Let's see a first version of how it could look like:10 11-//// tab | Python 3.7+11+{* docs_src/options/version/tutorial001_an.py hl[9:12,17:19] *}12 13-```Python hl_lines="9-12 17-19"14-{!> ../docs_src/options/version/tutorial001_an.py!}15-```16-17-////18-19-//// tab | Python 3.7+ non-Annotated20-13 /// tip14 23-Prefer to use the `Annotated` version if possible.24-25-///26-27-```Python hl_lines="8-11 16-18"28-{!> ../docs_src/options/version/tutorial001.py!}29-```30-31-////32-33-/// tip34-15 Notice 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.16 17 ///18 37 unmodified lines56 ## Previous parameters and `is_eager`57 58 But 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:59 80-//// tab | Python 3.7+60+{* docs_src/options/version/tutorial002_an.py hl[15:17,22:24] *}61 82-```Python hl_lines="15-17 22-24"83-{!> ../docs_src/options/version/tutorial002_an.py!}84-```85-86-////87-88-//// tab | Python 3.7+ non-Annotated89-90-/// tip91-92-Prefer to use the `Annotated` version if possible.93-94-///95-96-```Python hl_lines="14-16 21-23"97-{!> ../docs_src/options/version/tutorial002.py!}98-```99-100-////101-62 Then 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:63 64 <div class="termy">65 29 unmodified lines95 For those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*) with `is_eager=True`.96 97 That will tell **Typer** (actually Click) that it should process this *CLI parameter* before the others:98 139-//// tab | Python 3.7+99+{* docs_src/options/version/tutorial003_an.py hl[23:26] *}100 141-```Python hl_lines="23-26"142-{!> ../docs_src/options/version/tutorial003_an.py!}143-```144-145-////146-147-//// tab | Python 3.7+ non-Annotated148-149-/// tip150-151-Prefer to use the `Annotated` version if possible.152-153-///154-155-```Python hl_lines="22-24"156-{!> ../docs_src/options/version/tutorial003.py!}157-```158-159-////160-101 Check it:102 103 <div class="termy">104 8 unmodified linesmodified:docs/tutorial/parameter-types/bool.md+4 added-84 removed
8 unmodified lines9 Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.10 11 We can do that by specifying the exact name we want:12 13-//// tab | Python 3.7+13+{* docs_src/parameter_types/bool/tutorial001_an.py hl[5] *}14 15-```Python hl_lines="5"16-{!> ../docs_src/parameter_types/bool/tutorial001_an.py!}17-```18-19-////20-21-//// tab | Python 3.7+ non-Annotated22-23-/// tip24-25-Prefer to use the `Annotated` version if possible.26-27-///28-29-```Python hl_lines="4"30-{!> ../docs_src/parameter_types/bool/tutorial001.py!}31-```32-33-////34-15 Now there's only a `--force` *CLI option*:16 17 <div class="termy">18 38 unmodified lines57 We might want to instead have `--accept` and `--reject`.58 59 We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:60 81-//// tab | Python 3.7+61+{* docs_src/parameter_types/bool/tutorial002_an.py hl[7] *}62 83-```Python hl_lines="7"84-{!> ../docs_src/parameter_types/bool/tutorial002_an.py!}85-```86-87-////88-89-//// tab | Python 3.7+ non-Annotated90-91-/// tip92-93-Prefer to use the `Annotated` version if possible.94-95-///96-97-```Python hl_lines="6"98-{!> ../docs_src/parameter_types/bool/tutorial002.py!}99-```100-101-////102-63 Check it:64 65 <div class="termy">66 31 unmodified lines98 The same way, you can declare short versions of the names for these *CLI options*.99 100 For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:101 142-//// tab | Python 3.7+102+{* docs_src/parameter_types/bool/tutorial003_an.py hl[5] *}103 144-```Python hl_lines="5"145-{!> ../docs_src/parameter_types/bool/tutorial003_an.py!}146-```147-148-////149-150-//// tab | Python 3.7+ non-Annotated151-152-/// tip153-154-Prefer to use the `Annotated` version if possible.155-156-///157-158-```Python hl_lines="4"159-{!> ../docs_src/parameter_types/bool/tutorial003.py!}160-```161-162-////163-104 Check it:105 106 <div class="termy">107 26 unmodified lines134 If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.135 136 To do that, use a space and a single `/` and pass the negative name after:137 198-//// tab | Python 3.7+138+{* docs_src/parameter_types/bool/tutorial004_an.py hl[5] *}139 200-```Python hl_lines="5"201-{!> ../docs_src/parameter_types/bool/tutorial004_an.py!}202-```203-204-////205-206-//// tab | Python 3.7+ non-Annotated207-140 /// tip141 210-Prefer to use the `Annotated` version if possible.211-212-///213-214-```Python hl_lines="4"215-{!> ../docs_src/parameter_types/bool/tutorial004.py!}216-```217-218-////219-220-/// tip221-142 Have in mind that it's a string with a preceding space and then a `/`.143 144 So, it's `" /-S"` not `"/-S"`.145 34 unmodified linesmodified:docs/tutorial/parameter-types/custom-types.md+2 added-28 removed
11 unmodified lines12 ## Type Parser13 14 `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>.15 16-//// tab | Python 3.7+16+{* docs_src/parameter_types/custom_types/tutorial001_an.py hl[13:14,18:19] *}17 18-```Python hl_lines="13-14 18-19"19-{!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}20-```21-22-////23-24-//// tab | Python 3.7+ non-Annotated25-26-/// tip27-28-Prefer to use the `Annotated` version if possible.29-30-///31-32-```Python hl_lines="12-13 17-18"33-{!> ../docs_src/parameter_types/custom_types/tutorial001.py!}34-```35-36-////37-18 The 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.19 20 ## Click Custom Type21 22 If 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.23 44-//// tab | Python 3.7+45-46-```Python hl_lines="14-18 22-25"47-{!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}48-```49-50-////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 lines3 You 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>.4 5 Your function will receive a standard Python `datetime` object, and again, your editor will give you completion, etc.6 7-```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] *}8 9 Typer will accept any string from the following formats:10 11 * `%Y-%m-%d`45 unmodified lines57 * separated with "`/`"58 59 ...It's a crazy example, but let's say you also needed that strange format:60 63-//// tab | Python 3.7+61+{* docs_src/parameter_types/datetime/tutorial002_an.py hl[11] *}62 65-```Python hl_lines="11"66-{!> ../docs_src/parameter_types/datetime/tutorial002_an.py!}67-```68-69-////70-71-//// tab | Python 3.7+ non-Annotated72-63 /// tip64 75-Prefer to use the `Annotated` version if possible.76-77-///78-79-```Python hl_lines="8"80-{!> ../docs_src/parameter_types/datetime/tutorial002.py!}81-```82-83-////84-85-/// tip86-65 Notice the last string in `formats`: `"%m/%d/%Y"`.66 67 ///68 17 unmodified linesmodified:docs/tutorial/parameter-types/enum.md+3 added-45 removed
1 # Enum - Choices2 3 To 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>:4 5-```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] *}6 7 /// tip8 9 Notice that the function parameter `network` will be an `Enum`, not a `str`.43 unmodified lines53 ### Case insensitive Enum choices54 55 You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:56 59-//// tab | Python 3.7+57+{* docs_src/parameter_types/enum/tutorial002_an.py hl[15] *}58 61-```Python hl_lines="15"62-{!> ../docs_src/parameter_types/enum/tutorial002_an.py!}63-```64-65-////66-67-//// tab | Python 3.7+ non-Annotated68-69-/// tip70-71-Prefer to use the `Annotated` version if possible.72-73-///74-75-```Python hl_lines="13"76-{!> ../docs_src/parameter_types/enum/tutorial002.py!}77-```78-79-////80-59 And then the values of the `Enum` will be checked no matter if lower case, upper case, or a mix:60 61 <div class="termy">62 14 unmodified lines77 ### List of Enum values78 79 A *CLI parameter* can also take a list of `Enum` values:80 103-//// tab | Python 3.7+81+{* docs_src/parameter_types/enum/tutorial003_an.py hl[14] *}82 105-```Python hl_lines="14"106-{!> ../docs_src/parameter_types/enum/tutorial003_an.py!}107-```108-109-////110-111-//// tab | Python 3.7+ non-Annotated112-113-/// tip114-115-Prefer to use the `Annotated` version if possible.116-117-///118-119-```Python hl_lines="13"120-{!> ../docs_src/parameter_types/enum/tutorial003.py!}121-```122-123-////124-83 This works just like any other parameter value taking a list of things:84 85 <div class="termy">86 27 unmodified linesmodified:docs/tutorial/parameter-types/file.md+5 added-105 removed
45 unmodified lines46 ```47 48 You will get all the correct editor support, attributes, methods, etc for the file-like object:`49 50-//// tab | Python 3.7+50+{* docs_src/parameter_types/file/tutorial001_an.py hl[5] *}51 52-```Python hl_lines="5"53-{!> ../docs_src/parameter_types/file/tutorial001_an.py!}54-```55-56-////57-58-//// tab | Python 3.7+ non-Annotated59-60-/// tip61-62-Prefer to use the `Annotated` version if possible.63-64-///65-66-```Python hl_lines="4"67-{!> ../docs_src/parameter_types/file/tutorial001.py!}68-```69-70-////71-52 Check it:53 54 <div class="termy">55 17 unmodified lines73 ## `FileTextWrite`74 75 For writing text, you can use `typer.FileTextWrite`:76 97-//// tab | Python 3.7+77+{* docs_src/parameter_types/file/tutorial002_an.py hl[5:6] *}78 99-```Python hl_lines="5-6"100-{!> ../docs_src/parameter_types/file/tutorial002_an.py!}101-```102-103-////104-105-//// tab | Python 3.7+ non-Annotated106-107-/// tip108-109-Prefer to use the `Annotated` version if possible.110-111-///112-113-```Python hl_lines="4-5"114-{!> ../docs_src/parameter_types/file/tutorial002.py!}115-```116-117-////118-79 This would be for writing human text, like:80 81 ```82 some settings34 unmodified lines117 You will receive `bytes` from it.118 119 It's useful for reading binary files like images:120 161-//// tab | Python 3.7+121+{* docs_src/parameter_types/file/tutorial003_an.py hl[5] *}122 163-```Python hl_lines="5"164-{!> ../docs_src/parameter_types/file/tutorial003_an.py!}165-```166-167-////168-169-//// tab | Python 3.7+ non-Annotated170-171-/// tip172-173-Prefer to use the `Annotated` version if possible.174-175-///176-177-```Python hl_lines="4"178-{!> ../docs_src/parameter_types/file/tutorial003.py!}179-```180-181-////182-123 Check it:124 125 <div class="termy">126 19 unmodified lines146 Have in mind that you have to pass `bytes` to its `.write()` method, not `str`.147 148 If you have a `str`, you have to encode it first to get `bytes`.149 210-//// tab | Python 3.7+150+{* docs_src/parameter_types/file/tutorial004_an.py hl[5] *}151 212-```Python hl_lines="5"213-{!> ../docs_src/parameter_types/file/tutorial004_an.py!}214-```215-216-////217-218-//// tab | Python 3.7+ non-Annotated219-220-/// tip221-222-Prefer to use the `Annotated` version if possible.223-224-///225-226-```Python hl_lines="4"227-{!> ../docs_src/parameter_types/file/tutorial004.py!}228-```229-230-////231-152 <div class="termy">153 154 ```console155 $ python main.py --file binary.dat40 unmodified lines196 You can override the `mode` from the defaults above.197 198 For example, you could use `mode="a"` to write "appending" to the same file:199 280-//// tab | Python 3.7+200+{* docs_src/parameter_types/file/tutorial005_an.py hl[5] *}201 282-```Python hl_lines="5"283-{!> ../docs_src/parameter_types/file/tutorial005_an.py!}284-```285-286-////287-288-//// tab | Python 3.7+ non-Annotated289-202 /// tip203 292-Prefer to use the `Annotated` version if possible.293-294-///295-296-```Python hl_lines="4"297-{!> ../docs_src/parameter_types/file/tutorial005.py!}298-```299-300-////301-302-/// tip303-204 As you are manually setting `mode="a"`, you can use `typer.FileText` or `typer.FileTextWrite`, both will work.205 206 ///207 45 unmodified linesmodified:docs/tutorial/parameter-types/index.md+1 added-3 removed
6 unmodified lines7 When you declare a *CLI parameter* with some type **Typer** will convert the data received in the command line to that data type.8 9 For example:10 11-```Python hl_lines="4"12-{!../docs_src/parameter_types/index/tutorial001.py!}13-```11+{* docs_src/parameter_types/index/tutorial001.py hl[4] *}12 13 In this example, the value received for the *CLI argument* `NAME` will be treated as `str`.14 15 The value for the *CLI option* `--age` will be converted to an `int` and `--height-meters` will be converted to a `float`.54 unmodified linesmodified:docs/tutorial/parameter-types/number.md+3 added-63 removed
1 # Number2 3 You can define numeric validations with `max` and `min` values for `int` and `float` *CLI parameters*:4 5-//// tab | Python 3.7+5+{* docs_src/parameter_types/number/tutorial001_an.py hl[6:8] *}6 7-```Python hl_lines="6-8"8-{!> ../docs_src/parameter_types/number/tutorial001_an.py!}9-```10-11-////12-13-//// tab | Python 3.7+ non-Annotated14-15-/// tip16-17-Prefer to use the `Annotated` version if possible.18-19-///20-21-```Python hl_lines="5-7"22-{!> ../docs_src/parameter_types/number/tutorial001.py!}23-```24-25-////26-7 *CLI arguments* and *CLI options* can both use these validations.8 9 You can specify `min`, `max` or both.10 61 unmodified lines72 You might want to, instead of showing an error, use the closest minimum or maximum valid values.73 74 You can do it with the `clamp` parameter:75 96-//// tab | Python 3.7+76+{* docs_src/parameter_types/number/tutorial002_an.py hl[6:8] *}77 98-```Python hl_lines="6-8"99-{!> ../docs_src/parameter_types/number/tutorial002_an.py!}100-```101-102-////103-104-//// tab | Python 3.7+ non-Annotated105-106-/// tip107-108-Prefer to use the `Annotated` version if possible.109-110-///111-112-```Python hl_lines="5-7"113-{!> ../docs_src/parameter_types/number/tutorial002.py!}114-```115-116-////117-78 And then, when you pass data that is out of the valid range, it will be "clamped", the closest valid value will be used:79 80 <div class="termy">81 19 unmodified lines101 ## Counter *CLI options*102 103 You can make a *CLI option* work as a counter with the `counter` parameter:104 145-//// tab | Python 3.7+105+{* docs_src/parameter_types/number/tutorial003_an.py hl[5] *}106 147-```Python hl_lines="5"148-{!> ../docs_src/parameter_types/number/tutorial003_an.py!}149-```150-151-////152-153-//// tab | Python 3.7+ non-Annotated154-155-/// tip156-157-Prefer to use the `Annotated` version if possible.158-159-///160-161-```Python hl_lines="4"162-{!> ../docs_src/parameter_types/number/tutorial003.py!}163-```164-165-////166-107 It means that the *CLI option* will be like a boolean flag, e.g. `--verbose`.108 109 And the value you receive in the function will be the amount of times that `--verbose` was added:110 35 unmodified linesmodified:docs/tutorial/parameter-types/path.md+2 added-42 removed
2 unmodified lines3 You 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>.4 5 This is what you would do for directory paths, file paths, etc:6 7-//// tab | Python 3.7+7+{* docs_src/parameter_types/path/tutorial001_an.py hl[1,8] *}8 9-```Python hl_lines="1 8"10-{!> ../docs_src/parameter_types/path/tutorial001_an.py!}11-```12-13-////14-15-//// tab | Python 3.7+ non-Annotated16-17-/// tip18-19-Prefer to use the `Annotated` version if possible.20-21-///22-23-```Python hl_lines="1 7"24-{!> ../docs_src/parameter_types/path/tutorial001.py!}25-```26-27-////28-9 And 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.10 11 Check it:12 51 unmodified lines64 ///65 66 For example:67 88-//// tab | Python 3.7+68+{* docs_src/parameter_types/path/tutorial002_an.py hl[11:16] *}69 90-```Python hl_lines="11-16"91-{!> ../docs_src/parameter_types/path/tutorial002_an.py!}92-```93-94-////95-96-//// tab | Python 3.7+ non-Annotated97-98-/// tip99-100-Prefer to use the `Annotated` version if possible.101-102-///103-104-```Python hl_lines="9-14"105-{!> ../docs_src/parameter_types/path/tutorial002.py!}106-```107-108-////109-70 Check it:71 72 <div class="termy">73 39 unmodified linesmodified:docs/tutorial/parameter-types/uuid.md+1 added-3 removed
20 unmodified lines21 ///22 23 You can declare a *CLI parameter* as a UUID:24 25-```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] *}26 27 Your 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.28 29 Check it:20 unmodified linesmodified:docs/tutorial/subcommands/add-typer.md+3 added-9 removed
8 unmodified lines9 Let's imagine that you are creating a *CLI program* to manage items in some distant land.10 11 It could be in an `items.py` file with this:12 13-```Python14-{!../docs_src/subcommands/tutorial001/items.py!}15-```13+{* docs_src/subcommands/tutorial001/items.py *}14 15 And you would use it like:16 17 <div class="termy">11 unmodified lines29 But then you realize that you also have to manage users from your *CLI app*.30 31 It could be a file `users.py` with something like:32 35-```Python36-{!../docs_src/subcommands/tutorial001/users.py!}37-```33+{* docs_src/subcommands/tutorial001/users.py *}34 35 And you would use it like:36 37 <div class="termy">15 unmodified lines53 In this case, as with `git remote`, we can put them together as subcommands in another `typer.Typer()` *CLI program*.54 55 Now create a `main.py` with:56 61-```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] *}58 59 Here's what we do in `main.py`:60 61 * Import the other Python modules (the files `users.py` and `items.py`).107 unmodified linesmodified:docs/tutorial/subcommands/callback-override.md+4 added-12 removed
4 unmodified lines5 When adding a Typer app inside of another, the sub-Typer can also have its own callback.6 7 It can handle any *CLI parameters* that go before its own commands and execute any extra code:8 9-```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] *}10 11 In this case it doesn't define any *CLI parameters*, it just writes a message.12 13 Check it:13 unmodified lines27 ## Add a callback on creation28 29 It's also possible to add a callback when creating the `typer.Typer()` app that will be added to another Typer app:30 33-```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] *}32 33 This achieves exactly the same as above, it's just another place to add the callback.34 35 Check it:14 unmodified lines50 If a callback was added when creating the `typer.Typer()` app, it's possible to override it with a new one using `@app.callback()`.51 52 This 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:53 58-```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] *}55 56 Here 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()`.57 58 As `@app.callback()` takes precedence over `typer.Typer(callback=some_function)`, now our CLI app will use this new callback.17 unmodified lines76 Lastly, you can override the callback defined anywhere else when adding a sub-Typer with `app.add_typer()` using the `callback` parameter.77 78 This has the highest priority:79 86-```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] *}81 82 Notice 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.83 84 Now when you use the CLI program it will use the new callback function `callback_for_add_typer()`.14 unmodified linesmodified:docs/tutorial/subcommands/name-and-help.md+8 added-24 removed
10 unmodified lines11 ## Add a help text12 13 We can also set the `help` while adding a Typer:14 15-```Python hl_lines="6"16-{!../docs_src/subcommands/name_help/tutorial001.py!}17-```15+{* docs_src/subcommands/name_help/tutorial001.py hl[6] *}16 17 And then we get that help text for that command in the *CLI program*:18 19 <div class="termy">68 unmodified lines88 And 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.89 90 Here's an example:91 94-```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] *}93 94 Notice that now we added the sub-Typer without specifying a `name` nor a `help`.95 96 They are now inferred from the callback function.45 unmodified lines142 This has the lowest priority, we'll see later what has a higher priority and can override it.143 144 Check the code:145 150-```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] *}147 148 This achieves exactly the same as the previous example.149 150 Check it:35 unmodified lines186 ### Override a callback set in `typer.Typer()` with `@app.callback()`187 188 The 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:189 196-```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] *}191 192 Now 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.`.193 194 Check it:39 unmodified lines234 This takes precedence over inferring the name and help from a callback set in `@sub_app.callback()` and `typer.Typer(callback=sub_app_callback)`.235 236 Check the code:237 246-```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] *}239 240 Now 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.241 242 Check it:61 unmodified lines304 If you set it explicitly, that takes precedence over inferring.305 306 You can set it when creating a new `typer.Typer()`:307 318-```Python hl_lines="12"319-{!../docs_src/subcommands/name_help/tutorial006.py!}320-```308+{* docs_src/subcommands/name_help/tutorial006.py hl[12] *}309 310 /// info311 312 The 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 lines358 Any parameter that you use when creating a `typer.Typer()` app can be overridden in the parameters of `@app.callback()`.359 360 Continuing with the previous example, we now override the values in `@user_app.callback()`:361 374-```Python hl_lines="24"375-{!../docs_src/subcommands/name_help/tutorial007.py!}376-```362+{* docs_src/subcommands/name_help/tutorial007.py hl[24] *}363 364 And now the command name will be `call-users` and the help text will be `Help from callback for users.`.365 366 Check it:35 unmodified lines402 ### Name and help in `app.add_typer()`403 404 And 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:405 420-```Python hl_lines="21"421-{!../docs_src/subcommands/name_help/tutorial008.py!}422-```406+{* docs_src/subcommands/name_help/tutorial008.py hl[21] *}407 408 And 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.`.409 410 Check it:47 unmodified linesmodified:docs/tutorial/subcommands/nested-subcommands.md+10 added-30 removed
10 unmodified lines11 ## A CLI app for reigns12 13 Let's start with a file `reigns.py`:14 15-```Python16-{!../docs_src/subcommands/tutorial003/reigns.py!}17-```15+{* docs_src/subcommands/tutorial003/reigns.py *}16 17 This is already a simple *CLI program* to manage reigns:18 19 <div class="termy">28 unmodified lines48 ## A CLI app for towns49 50 And now the equivalent for managing towns in `towns.py`:51 54-```Python55-{!../docs_src/subcommands/tutorial003/towns.py!}56-```52+{* docs_src/subcommands/tutorial003/towns.py *}53 54 With it, you can manage towns:55 56 <div class="termy">28 unmodified lines85 ## Manage the land in a CLI app86 87 Now let's put the `reigns` and `towns` together in the same *CLI program* in `lands.py`:88 93-```Python94-{!../docs_src/subcommands/tutorial003/lands.py!}95-```89+{* docs_src/subcommands/tutorial003/lands.py *}90 91 And 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.92 93 Check it:92 unmodified lines186 This already is a quite deeply nested "tree" of commands/command groups.187 188 But to achieve that, we just have to add the `lands` **Typer** app to the same `main.py` file we already had:189 196-```Python hl_lines="4 10"197-{!../docs_src/subcommands/tutorial003/main.py!}198-```190+{* docs_src/subcommands/tutorial003/main.py hl[4,10] *}191 192 And now we have everything in a single *CLI program*:193 194 <div class="termy">42 unmodified lines237 Here are all the files if you want to review/copy them:238 239 `reigns.py`:240 249-```Python250-{!../docs_src/subcommands/tutorial003/reigns.py!}251-```241+{* docs_src/subcommands/tutorial003/reigns.py *}242 243 `towns.py`:244 255-```Python256-{!../docs_src/subcommands/tutorial003/towns.py!}257-```245+{* docs_src/subcommands/tutorial003/towns.py *}246 247 `lands.py`:248 261-```Python262-{!../docs_src/subcommands/tutorial003/lands.py!}263-```249+{* docs_src/subcommands/tutorial003/lands.py *}250 251 `users.py`:252 267-```Python268-{!../docs_src/subcommands/tutorial003/users.py!}269-```253+{* docs_src/subcommands/tutorial003/users.py *}254 255 `items.py`:256 273-```Python274-{!../docs_src/subcommands/tutorial003/items.py!}275-```257+{* docs_src/subcommands/tutorial003/items.py *}258 259 `main.py`:260 279-```Python280-{!../docs_src/subcommands/tutorial003/main.py!}281-```261+{* docs_src/subcommands/tutorial003/main.py *}262 263 /// tip264 265 All these files have an `if __name__ == "__main__"` block just to demonstrate how each of them can also be an independent *CLI app*.18 unmodified linesmodified:docs/tutorial/subcommands/single-file.md+4 added-12 removed
2 unmodified lines3 In some cases, it's possible that your application code needs to live on a single file.4 5 You can still use the same ideas:6 7-```Python8-{!../docs_src/subcommands/tutorial002/main.py!}9-```7+{* docs_src/subcommands/tutorial002/main.py *}8 9 There are several things to notice here...10 11 ## Apps at the top1 unmodified line13 First, you can create `typer.Typer()` objects and add them to another one at the top.14 15 It doesn't have to be done after creating the subcommands:16 19-```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] *}18 19 You can add the commands (subcommands) to each `typer.Typer()` app later and it will still work.20 21 ## Function names1 unmodified line23 As 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.24 25 So we use longer names:26 31-```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] *}28 29 ## Command name30 31 We are naming the functions with longer names so that they don't overwrite each other.23 unmodified lines55 </div>56 57 So we pass the name we want to use for each subcommand as the function argument to the decorator:58 65-```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] *}60 61 ## Check it62 63 It still works the same:79 unmodified linesmodified:docs/tutorial/app-dir.md+1 added-3 removed
1 # CLI Application Directory2 3 You can get the application directory where you can, for example, save configuration files with `typer.get_app_dir()`:4 5-```Python hl_lines="9"6-{!../docs_src/app_dir/tutorial001.py!}7-```5+{* docs_src/app_dir/tutorial001.py hl[9] *}6 7 It will give you a directory for storing configurations appropriate for your CLI program for the current user in each operating system.8 9 Check it:34 unmodified linesmodified:docs/tutorial/exceptions.md+4 added-12 removed
6 unmodified lines7 ## Example Broken App8 9 Let's take this example broken app:10 11-```Python hl_lines="5"12-{!../docs_src/exceptions/tutorial001.py!}13-```11+{* docs_src/exceptions/tutorial001.py hl[5] *}12 13 This code is broken because you can't sum a string and a number (`name + 3`).14 15 ## Exceptions with Rich68 unmodified lines84 But if it was something like a password, you would have liked to hide it.85 86 In that case, you can create the `typer.Typer()` application explicitly and set the parameter `pretty_exceptions_show_locals=False`:87 90-```Python hl_lines="3"91-{!../docs_src/exceptions/tutorial002.py!}92-```88+{* docs_src/exceptions/tutorial002.py hl[3] *}89 90 And now when you run it, you will see the error without the local variables:91 92 <div class="termy">24 unmodified lines117 ## Disable Short Output118 119 If you want to show the full exception, including the parts in Typer and Click, you can use the parameter `pretty_exceptions_short=False`:120 125-```Python hl_lines="3"126-{!../docs_src/exceptions/tutorial003.py!}127-```121+{* docs_src/exceptions/tutorial003.py hl[3] *}122 123 Now when you run it, you will see the whole output:124 125 <div class="termy">73 unmodified lines199 ## Disable Pretty Exceptions200 201 You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`:202 209-```Python hl_lines="3"210-{!../docs_src/exceptions/tutorial004.py!}211-```203+{* docs_src/exceptions/tutorial004.py hl[3] *}204 205 And now you will see the full standard exception as with any other Python program:206 207 <div class="termy">66 unmodified linesmodified:docs/tutorial/first-steps.md+6 added-18 removed
2 unmodified lines3 ## The simplest example4 5 The simplest **Typer** file could look like this:6 7-```Python8-{!../docs_src/first_steps/tutorial001.py!}9-```7+{* docs_src/first_steps/tutorial001.py *}8 9 Copy that to a file `main.py`.10 11 Test it:48 unmodified lines60 ## Add a CLI argument61 62 Update the previous example with an argument `name`:63 66-```Python hl_lines="4 5"67-{!../docs_src/first_steps/tutorial002.py!}68-```64+{* docs_src/first_steps/tutorial002.py hl[4,5] *}65 66 <div class="termy">67 68 ```console33 unmodified lines102 Now let's say we want to have the name and last name separated.103 104 So, extend that to have 2 arguments, `name` and `lastname`:105 110-```Python hl_lines="4 5"111-{!../docs_src/first_steps/tutorial003.py!}112-```106+{* docs_src/first_steps/tutorial003.py hl[4,5] *}107 108 <div class="termy">109 110 ```console116 unmodified lines227 ## Add one *CLI option*228 229 Now add a `--formal` *CLI option*:230 237-```Python hl_lines="4 5"238-{!../docs_src/first_steps/tutorial004.py!}239-```231+{* docs_src/first_steps/tutorial004.py hl[4,5] *}232 233 Here `formal` is a `bool` that is `False` by default.234 235 <div class="termy">52 unmodified lines288 ## A *CLI option* with a value289 290 To convert the `lastname` from a *CLI argument* to a *CLI option*, give it a default value of `""`:291 300-```Python hl_lines="4"301-{!../docs_src/first_steps/tutorial005.py!}302-```292+{* docs_src/first_steps/tutorial005.py hl[4] *}293 294 As `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*.295 296 <div class="termy">62 unmodified lines359 ## Document your CLI app360 361 If 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:362 373-```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] *}364 365 Now see it with the `--help` option:366 367 <div class="termy">142 unmodified linesmodified:docs/tutorial/launch.md+2 added-6 removed
2 unmodified lines3 You can launch applications from your CLI program with `typer.launch()`.4 5 It will launch the appropriate application depending on the URL or file type you pass it:6 7-```Python hl_lines="6"8-{!../docs_src/launch/tutorial001.py!}9-```7+{* docs_src/launch/tutorial001.py hl[6] *}8 9 Check it:10 11 <div class="termy">11 unmodified lines23 ## Locating a file24 25 You can also make the operating system open the file browser indicating where a file is located with `locate=True`:26 29-```Python hl_lines="17"30-{!../docs_src/launch/tutorial002.py!}31-```27+{* docs_src/launch/tutorial002.py hl[17] *}28 29 /// tip30 31 The rest of the code in this example is just making sure the app directory exists and creating the config file.18 unmodified linesmodified:docs/tutorial/options-autocompletion.md+9 added-189 removed
14 unmodified lines15 To check it quickly without creating a new Python package, use the `typer` command.16 17 Then let's create small example program:18 19-//// tab | Python 3.7+19+{* docs_src/options_autocompletion/tutorial001_an.py *}20 21-```Python22-{!> ../docs_src/options_autocompletion/tutorial001_an.py!}23-```24-25-////26-27-//// tab | Python 3.7+ non-Annotated28-29-/// tip30-31-Prefer to use the `Annotated` version if possible.32-33-///34-35-```Python36-{!> ../docs_src/options_autocompletion/tutorial001.py!}37-```38-39-////40-21 And let's try it with the `typer` command to get completion:22 23 <div class="termy">24 24 unmodified lines49 Right now we get completion for the *CLI option* names, but not for the values.50 51 We 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}:52 73-//// tab | Python 3.7+53+{* docs_src/options_autocompletion/tutorial002_an.py hl[5:6,15] *}54 75-```Python hl_lines="5-6 15"76-{!> ../docs_src/options_autocompletion/tutorial002_an.py!}77-```78-79-////80-81-//// tab | Python 3.7+ non-Annotated82-83-/// tip84-85-Prefer to use the `Annotated` version if possible.86-87-///88-89-```Python hl_lines="4-5 14"90-{!> ../docs_src/options_autocompletion/tutorial002.py!}91-```92-93-////94-55 We return a `list` of strings from the `complete_name()` function.56 57 And then we get those values when using completion:58 19 unmodified lines78 Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.79 80 Then we can check and return only the values that start with the incomplete value from the command line:81 122-//// tab | Python 3.7+82+{* docs_src/options_autocompletion/tutorial003_an.py hl[7:12] *}83 124-```Python hl_lines="7-12"125-{!> ../docs_src/options_autocompletion/tutorial003_an.py!}126-```127-128-////129-130-//// tab | Python 3.7+ non-Annotated131-132-/// tip133-134-Prefer to use the `Annotated` version if possible.135-136-///137-138-```Python hl_lines="6-11"139-{!> ../docs_src/options_autocompletion/tutorial003.py!}140-```141-142-////143-84 Now let's try it:85 86 <div class="termy">87 29 unmodified lines117 In 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.118 119 So, in the end, we return a `list` of `tuples` of `str`:120 181-//// tab | Python 3.7+121+{* docs_src/options_autocompletion/tutorial004_an.py hl[4:8,11:17] *}122 183-```Python hl_lines="4-8 11-17"184-{!> ../docs_src/options_autocompletion/tutorial004_an.py!}185-```186-187-////188-189-//// tab | Python 3.7+ non-Annotated190-123 /// tip124 193-Prefer to use the `Annotated` version if possible.194-195-///196-197-```Python hl_lines="3-7 10-16"198-{!> ../docs_src/options_autocompletion/tutorial004.py!}199-```200-201-////202-203-/// tip204-125 If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.126 127 Click checks specifically for a `tuple` when extracting the help text.128 29 unmodified lines158 Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.159 160 That 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:161 242-//// tab | Python 3.7+162+{* docs_src/options_autocompletion/tutorial005_an.py hl[11:14] *}163 244-```Python hl_lines="11-14"245-{!> ../docs_src/options_autocompletion/tutorial005_an.py!}246-```247-248-////249-250-//// tab | Python 3.7+ non-Annotated251-252-/// tip253-254-Prefer to use the `Annotated` version if possible.255-256-///257-258-```Python hl_lines="10-13"259-{!> ../docs_src/options_autocompletion/tutorial005.py!}260-```261-262-////263-164 That simplifies our code a bit and works the same.165 166 /// tip167 26 unmodified lines194 ///195 196 For this we use a `List` of `str`:197 298-//// tab | Python 3.7+198+{* docs_src/options_autocompletion/tutorial006_an.py hl[9:14] *}199 300-```Python hl_lines="9-14"301-{!> ../docs_src/options_autocompletion/tutorial006_an.py!}302-```303-304-////305-306-//// tab | Python 3.7+ non-Annotated307-308-/// tip309-310-Prefer to use the `Annotated` version if possible.311-312-///313-314-```Python hl_lines="8-11"315-{!> ../docs_src/options_autocompletion/tutorial006.py!}316-```317-318-////319-200 And then we can use it like:201 202 <div class="termy">203 15 unmodified lines219 But you can access the context by declaring a function parameter of type `typer.Context`.220 221 And from that context you can get the current values for each parameter.222 343-//// tab | Python 3.7+223+{* docs_src/options_autocompletion/tutorial007_an.py hl[13:14,16] *}224 345-```Python hl_lines="13-14 16"346-{!> ../docs_src/options_autocompletion/tutorial007_an.py!}347-```348-349-////350-351-//// tab | Python 3.7+ non-Annotated352-353-/// tip354-355-Prefer to use the `Annotated` version if possible.356-357-///358-359-```Python hl_lines="12-13 15"360-{!> ../docs_src/options_autocompletion/tutorial007.py!}361-```362-363-////364-225 We are getting the `names` already provided with `--name` in the command line before this completion was triggered.226 227 If 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 lines295 You can print to "standard error" with a **Rich** `Console(stderr=True)`.296 297 Using `stderr=True` tells **Rich** that the output should be shown in "standard error".298 439-//// tab | Python 3.7+299+{* docs_src/options_autocompletion/tutorial008_an.py hl[13,16:17] *}300 441-```Python hl_lines="13 16-17"442-{!> ../docs_src/options_autocompletion/tutorial008_an.py!}443-```444-445-////446-447-//// tab | Python 3.7+ non-Annotated448-449-/// tip450-451-Prefer to use the `Annotated` version if possible.452-453-///454-455-```Python hl_lines="12 15-16"456-{!> ../docs_src/options_autocompletion/tutorial008.py!}457-```458-459-////460-301 /// info302 303 If 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 lines343 ## Getting the Context and the raw *CLI parameters*344 345 Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:346 507-//// tab | Python 3.7+347+{* docs_src/options_autocompletion/tutorial009_an.py hl[16] *}348 509-```Python hl_lines="16"510-{!> ../docs_src/options_autocompletion/tutorial009_an.py!}511-```512-513-////514-515-//// tab | Python 3.7+ non-Annotated516-517-/// tip518-519-Prefer to use the `Annotated` version if possible.520-521-///522-523-```Python hl_lines="15"524-{!> ../docs_src/options_autocompletion/tutorial009.py!}525-```526-527-////528-349 Check it:350 351 <div class="termy">352 34 unmodified linesmodified:docs/tutorial/printing.md+7 added-21 removed
1 # Printing and Colors2 3 You can use the normal `print()` to show information on the screen:4 5-```Python hl_lines="5"6-{!../docs_src/first_steps/tutorial001.py!}7-```5+{* docs_src/first_steps/tutorial001.py hl[5] *}6 7 It will show the output normally:8 9 <div class="termy">13 unmodified lines23 ### Use Rich `print`24 25 For the simplest cases, you can just import `print` from `rich` and use it instead of the standard `print`:26 29-```Python hl_lines="2 15"30-{!../docs_src/printing/tutorial001.py!}31-```27+{* docs_src/printing/tutorial001.py hl[2,15] *}28 29 Just with that, **Rich** will be able to print your data with nice colors and structure:30 31 <div class="termy">19 unmodified lines51 ### Rich Markup52 53 Rich 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:54 59-```Python hl_lines="6"60-{!../docs_src/printing/tutorial002.py!}61-```55+{* docs_src/printing/tutorial002.py hl[6] *}56 57 <div class="termy">58 59 ```console15 unmodified lines75 When you call Rich's `print`, it automatically creates this object and uses it.76 77 But for advanced use cases, you could create a `Console` yourself.78 85-```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] *}80 81 In this example, we create a `Console`, and a `Table`. And then we can add some rows to the table, and print it.82 83 If you run it, you will see a nicely formatted table:67 unmodified lines151 ///152 153 Using `stderr=True` tells **Rich** that the output should be shown in "standard error".154 163-```Python hl_lines="4 8"164-{!../docs_src/printing/tutorial004.py!}165-```155+{* docs_src/printing/tutorial004.py hl[4,8] *}156 157 When you try it in the terminal, it will probably just look the same:158 159 <div class="termy">67 unmodified lines227 ///228 229 You 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()`:230 241-```Python hl_lines="7 9"242-{!../docs_src/printing/tutorial005.py!}243-```231+{* docs_src/printing/tutorial005.py hl[7,9] *}232 233 /// tip234 235 The 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 lines273 ///274 275 There'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()`:276 289-```Python hl_lines="5"290-{!../docs_src/printing/tutorial006.py!}291-```277+{* docs_src/printing/tutorial006.py hl[5] *}278 279 Check it:280 281 <div class="use-termynal" data-termynal>3 unmodified linesmodified:docs/tutorial/progressbar.md+6 added-18 removed
4 unmodified lines5 ## Progress Bar6 7 You 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:8 9-```Python hl_lines="4 9"10-{!../docs_src/progressbar/tutorial001.py!}11-```9+{* docs_src/progressbar/tutorial001.py hl[4,9] *}10 11 You put the thing that you want to iterate over inside of Rich's `track()`, and then iterate over that.12 13 Check it:31 unmodified lines45 Rich allows you to display many things in complex and advanced ways.46 47 For example, this will show two spinners:48 51-```Python hl_lines="4 8-15"52-{!../docs_src/progressbar/tutorial002.py!}53-```49+{* docs_src/progressbar/tutorial002.py hl[4,8:15] *}50 51 I can't show you the beautiful animation here in the docs. 😅52 53 But at some point in time it will look like this (imagine it's spinning). 🤓45 unmodified lines99 ```100 101 And you pass as function argument to `typer.progressbar()` the thing that you would normally iterate over.102 107-```Python hl_lines="8"108-{!../docs_src/progressbar/tutorial003.py!}109-```103+{* docs_src/progressbar/tutorial003.py hl[8] *}104 105 So, if you have a list of users, this could be:106 107 ```Python54 unmodified lines162 The progress bar is generated from the length of the iterable (e.g. the list of users).163 164 But 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()`.165 172-```Python hl_lines="14"173-{!../docs_src/progressbar/tutorial004.py!}174-```166+{* docs_src/progressbar/tutorial004.py hl[14] *}167 168 Check it:169 170 <div class="termy">39 unmodified lines210 ///211 212 You can also set a `label`:213 222-```Python hl_lines="8"223-{!../docs_src/progressbar/tutorial005.py!}224-```214+{* docs_src/progressbar/tutorial005.py hl[8] *}215 216 Check it:217 218 <div class="use-termynal">7 unmodified lines226 If 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()`.227 228 And then calling the `.update()` method in the object from the `with` statement:229 240-```Python hl_lines="8 12"241-{!../docs_src/progressbar/tutorial006.py!}242-```230+{* docs_src/progressbar/tutorial006.py hl[8,12] *}231 232 Check it:233 234 <div class="use-termynal">4 unmodified linesmodified:docs/tutorial/prompt.md+4 added-12 removed
2 unmodified lines3 When 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).4 5 But if you absolutely need to ask for interactive information without using a *CLI option*, you can use `typer.prompt()`:6 7-```Python hl_lines="5"8-{!../docs_src/prompt/tutorial001.py!}9-```7+{* docs_src/prompt/tutorial001.py hl[5] *}8 9 Check it:10 11 <div class="termy">11 unmodified lines23 ## Confirm24 25 There'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}:26 29-```Python hl_lines="5"30-{!../docs_src/prompt/tutorial002.py!}31-```27+{* docs_src/prompt/tutorial002.py hl[5] *}28 29 Check it:30 31 <div class="termy">19 unmodified lines51 ## Confirm or abort52 53 As it's very common to abort if the user doesn't confirm, there's an integrated parameter `abort` that does it automatically:54 59-```Python hl_lines="5"60-{!../docs_src/prompt/tutorial003.py!}61-```55+{* docs_src/prompt/tutorial003.py hl[5] *}56 57 <div class="termy">58 59 ```console16 unmodified lines76 ## Prompt with Rich77 78 If 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:79 86-```Python hl_lines="2 6"87-{!../docs_src/prompt/tutorial004.py!}88-```80+{* docs_src/prompt/tutorial004.py hl[2,6] *}81 82 And when you run it, it will look like:83 84 <div class="termy">10 unmodified linesmodified:docs/tutorial/terminating.md+3 added-9 removed
10 unmodified lines11 This doesn't have to mean that there's an error, just that nothing else needs to be executed.12 13 In that case, you can raise a `typer.Exit()` exception:14 15-```Python hl_lines="9"16-{!../docs_src/terminating/tutorial001.py!}17-```15+{* docs_src/terminating/tutorial001.py hl[9] *}16 17 There are several things to see in this example.18 19 * The CLI program is the function `main()`, not the others. This is the one that takes a *CLI argument*.35 unmodified lines55 `typer.Exit()` takes an optional `code` parameter. By default, `code` is `0`, meaning there was no error.56 57 You 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:58 61-```Python hl_lines="7"62-{!../docs_src/terminating/tutorial002.py!}63-```59+{* docs_src/terminating/tutorial002.py hl[7] *}60 61 Check it:62 63 <div class="termy">34 unmodified lines98 There's a special exception that you can use to "abort" a program.99 100 It 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:101 106-```Python hl_lines="7"107-{!../docs_src/terminating/tutorial003.py!}108-```102+{* docs_src/terminating/tutorial003.py hl[7] *}103 104 Check it:105 106 <div class="termy">14 unmodified linesmodified:docs/tutorial/testing.md+8 added-42 removed
2 unmodified lines3 Testing **Typer** applications is very easy with <a href="https://docs.pytest.org/en/latest/" class="external-link" target="_blank">pytest</a>.4 5 Let's say you have an application `app/main.py` with:6 7-```Python8-{!../docs_src/testing/app01/main.py!}9-```7+{* docs_src/testing/app01/main.py *}8 9 So, you would use it like:10 11 <div class="termy">20 unmodified lines32 Import `CliRunner` and create a `runner` object.33 34 This runner is what will "invoke" or "call" your command line application.35 38-```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] *}37 38 /// tip39 40 It'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 lines50 The first parameter to `runner.invoke()` is a `Typer` app.51 52 The second parameter is a `list` of `str`, with all the text you would pass in the command line, right as you would pass it:53 58-```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] *}55 56 /// tip57 58 The name of the function has to start with `test_`, that way pytest can detect it and use it automatically.3 unmodified lines62 ### Check the result63 64 Then, inside of the test function, add `assert` statements to ensure that everything in the result of the call is as it should be.65 72-```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] *}67 68 Here we are checking that the exit code is 0, as it is for programs that exit without errors.69 70 Then we check that the text printed to "standard output" contains the text that our CLI program prints.37 unmodified lines108 ## Testing input109 110 If you have a CLI with prompts, like:111 120-//// tab | Python 3.7+112+{* docs_src/testing/app02_an/main.py hl[8] *}113 122-```Python hl_lines="8"123-{!> ../docs_src/testing/app02_an/main.py!}124-```125-126-////127-128-//// tab | Python 3.7+ non-Annotated129-130-/// tip131-132-Prefer to use the `Annotated` version if possible.133-134-///135-136-```Python hl_lines="7"137-{!> ../docs_src/testing/app02/main.py!}138-```139-140-////141-114 That you would use like:115 116 <div class="termy">117 20 unmodified lines138 When 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"`.139 140 So, if you use `input="camila@example.com\n"` it means: "type `camila@example.com` in the terminal, then hit the <kbd>ENTER</kbd> key":141 170-```Python hl_lines="9"171-{!../docs_src/testing/app02/test_main.py!}172-```142+{* docs_src/testing/app02/test_main.py hl[9] *}143 144 ## Test a function145 146 If you have a script and you never created an explicit `typer.Typer` app, like:147 178-```Python hl_lines="9"179-{!../docs_src/testing/app03/main.py!}180-```148+{* docs_src/testing/app03/main.py hl[9] *}149 150 ...you can still test it, by creating an app during testing:151 184-```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] *}153 154 Of 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.155 156 But 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 linesmodified:docs/tutorial/typer-command.md+1 added-3 removed
239 unmodified lines240 ### Sample script with docs241 242 For example, you could have a script like:243 244-```Python245-{!../docs_src/commands/help/tutorial001.py!}246-```244+{* docs_src/commands/help/tutorial001.py *}245 246 ### Generate docs with the `typer` command247 248 Then you could generate docs for it with the `typer` command.131 unmodified linesmodified:docs/tutorial/using-click.md+4 added-12 removed
26 unmodified lines27 A `Command` can have its own *CLI arguments* and *CLI options*, and it has a function that it calls.28 29 For example, in this Click app:30 31-```Python hl_lines="7 14"32-{!../docs_src/using_click/tutorial001.py!}33-```31+{* docs_src/using_click/tutorial001.py hl[7,14] *}32 33 The 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`).34 35 #### Click `Group`7 unmodified lines43 And a `Group` can also have a function that it calls, right before calling the function for any specific subcommand.44 45 For example:46 49-```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] *}48 49 The `cli` variable is converted by Click from a function to a `Group` object. And the original `cli` function is used by that `Group` internally.50 51 /// tip33 unmodified lines85 ### Including a Click app in a **Typer** app86 87 For example, you could have a **Typer** app, generate a Click `Group` from it, and then include other Click apps in it:88 93-```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] *}90 91 Notice 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`.92 93 Then 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 lines129 ### Including a **Typer** app in a Click app130 131 The same way, you can do the contrary and include a **Typer** sub app in a bigger Click app:132 139-```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] *}134 135 Notice 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.136 137 Then 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 linesmodified:mkdocs.yml+1 added-1 removed
190 unmodified lines191 alternate_style: True192 193 # Other extensions194 mdx_include:195- base_path: docs195+ markdown_include_variants:196 197 extra:198 analytics:199 provider: google38 unmodified linesmodified:requirements-docs.txt+1 added
14 unmodified lines15 # griffe-typingdoc==0.2.516 # For griffe, it formats with black17 # black==24.3.018 mkdocs-macros-plugin==1.3.719+markdown-include-variants==0.0.3