🔧 Update tabs and admonitions with new syntax and new MkDocs features (#906)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 📝 Update docs tabs to use new pymdownextensions tab blocks * 📝 Update docs with new admonitions * 🔧 Update MkDocs config with new Markdown plugins and configs
authored by Sebastián Ramírez · committed by GitHub · parent a4a3db3 · Browse files · 49 files·+2079 added−1174 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
49 files+2079 added−1174 removed
- other:+2079 added−1174 removed
Provenance
- Commit
289b230- Tree
c8ffd62- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a4a3db3
49 files changed+2079 added−1174 removed
modified:docs/tutorial/arguments/default.md+45 added-25 removed
4 unmodified lines56789910111213111213141515161718171820212219202425212227232425262728293031323334353637383940414226 unmodified lines6970717263737465666775767778697980717281827475768384858687888990919293949596979882839910010110210310410510610710832 unmodified lines4 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`:
=== "Python 3.7+"//// tab | Python 3.7+
```Python hl_lines="5" {!> ../docs_src/arguments/default/tutorial001_an.py!} ``````Python hl_lines="5"{!> ../docs_src/arguments/default/tutorial001_an.py!}```
=== "Python 3.7+ non-Annotated"////
!!! tip Prefer to use the `Annotated` version if possible.//// tab | Python 3.7+ non-Annotated
```Python hl_lines="4" {!> ../docs_src/arguments/default/tutorial001.py!} ```/// tip
!!! 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]`.Prefer to use the `Annotated` version if possible.
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.///
```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.
///
Check it:
<div class="termy">
26 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:
=== "Python 3.7+"//// tab | Python 3.7+
```Python hl_lines="7-8 11" {!> ../docs_src/arguments/default/tutorial002_an.py!} ``````Python hl_lines="7-8 11"{!> ../docs_src/arguments/default/tutorial002_an.py!}```
=== "Python 3.7+ non-Annotated"////
!!! tip Prefer to use the `Annotated` version if possible.//// tab | Python 3.7+ non-Annotated
```Python hl_lines="6-7 10" {!> ../docs_src/arguments/default/tutorial002.py!} ```/// 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()`.
!!! tip The word "factory" in `default_factory` is just a fancy way of saying "function that will create the default value"./// tip
The word "factory" in `default_factory` is just a fancy way of saying "function that will create the default value".
///
Check it:
<div class="termy">
32 unmodified linesmodified:docs/tutorial/arguments/envvar.md+57 added-33 removed
1 You can also configure a *CLI argument* to read a value from an environment variable if it is not provided in the command line as a *CLI argument*.2 3 To do that, use the `envvar` parameter for `typer.Argument()`:4 5-=== "Python 3.7+"5+//// tab | Python 3.7+6 7- ```Python hl_lines="5"8- {!> ../docs_src/arguments/envvar/tutorial001_an.py!}9- ```7+```Python hl_lines="5"8+{!> ../docs_src/arguments/envvar/tutorial001_an.py!}9+```10 11-=== "Python 3.7+ non-Annotated"11+////12 13- !!! tip14- Prefer to use the `Annotated` version if possible.13+//// tab | Python 3.7+ non-Annotated14 16- ```Python hl_lines="4"17- {!> ../docs_src/arguments/envvar/tutorial001.py!}18- ```15+/// tip16 17+Prefer to use the `Annotated` version if possible.18+19+///20+21+```Python hl_lines="4"22+{!> ../docs_src/arguments/envvar/tutorial001.py!}23+```24+25+////26+27 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:28 29 <div class="termy">30 35 unmodified lines66 ## Multiple environment variables67 68 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:69 63-=== "Python 3.7+"70+//// tab | Python 3.7+71 65- ```Python hl_lines="6"66- {!> ../docs_src/arguments/envvar/tutorial002_an.py!}67- ```72+```Python hl_lines="6"73+{!> ../docs_src/arguments/envvar/tutorial002_an.py!}74+```75 69-=== "Python 3.7+ non-Annotated"76+////77 71- !!! tip72- Prefer to use the `Annotated` version if possible.78+//// tab | Python 3.7+ non-Annotated79 74- ```Python hl_lines="4"75- {!> ../docs_src/arguments/envvar/tutorial002.py!}76- ```80+/// tip81 82+Prefer to use the `Annotated` version if possible.83+84+///85+86+```Python hl_lines="4"87+{!> ../docs_src/arguments/envvar/tutorial002.py!}88+```89+90+////91+92 Check it:93 94 <div class="termy">95 25 unmodified lines121 ## Hide an env var from the help text122 123 By default, environment variables used will be shown in the help text, but you can disable them with `show_envvar=False`:124 111-=== "Python 3.7+"125+//// tab | Python 3.7+126 113- ```Python hl_lines="7"114- {!> ../docs_src/arguments/envvar/tutorial003_an.py!}115- ```127+```Python hl_lines="7"128+{!> ../docs_src/arguments/envvar/tutorial003_an.py!}129+```130 117-=== "Python 3.7+ non-Annotated"131+////132 119- !!! tip120- Prefer to use the `Annotated` version if possible.133+//// tab | Python 3.7+ non-Annotated134 122- ```Python hl_lines="4"123- {!> ../docs_src/arguments/envvar/tutorial003.py!}124- ```135+/// tip136 137+Prefer to use the `Annotated` version if possible.138+139+///140+141+```Python hl_lines="4"142+{!> ../docs_src/arguments/envvar/tutorial003.py!}143+```144+145+////146+147 Check it:148 149 <div class="termy">150 17 unmodified lines168 ```169 170 </div>171 151-!!! note "Technical Details"152- In Click applications the env vars are hidden by default. 🙈172+/// note | Technical Details173 174+In Click applications the env vars are hidden by default. 🙈175+154- In **Typer** these env vars are shown by default. 👀176+In **Typer** these env vars are shown by default. 👀177+178+///modified:docs/tutorial/arguments/help.md+153 added-88 removed
10 unmodified lines11 ## 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 15-=== "Python 3.7+"15+//// tab | Python 3.7+16 17- ```Python hl_lines="5"18- {!> ../docs_src/arguments/help/tutorial001_an.py!}19- ```17+```Python hl_lines="5"18+{!> ../docs_src/arguments/help/tutorial001_an.py!}19+```20 21-=== "Python 3.7+ non-Annotated"21+////22 23- !!! tip24- Prefer to use the `Annotated` version if possible.23+//// tab | Python 3.7+ non-Annotated24 26- ```Python hl_lines="4"27- {!> ../docs_src/arguments/help/tutorial001.py!}28- ```25+/// tip26 27+Prefer to use the `Annotated` version if possible.28+29+///30+31+```Python hl_lines="4"32+{!> ../docs_src/arguments/help/tutorial001.py!}33+```34+35+////36+37 And it will be used in the automatic `--help` option:38 39 <div class="termy">40 15 unmodified lines56 ## Combine help text and docstrings57 58 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>:59 53-=== "Python 3.7+"60+//// tab | Python 3.7+61 55- ```Python hl_lines="5-8"56- {!> ../docs_src/arguments/help/tutorial002_an.py!}57- ```62+```Python hl_lines="5-8"63+{!> ../docs_src/arguments/help/tutorial002_an.py!}64+```65 59-=== "Python 3.7+ non-Annotated"66+////67 61- !!! tip62- Prefer to use the `Annotated` version if possible.68+//// tab | Python 3.7+ non-Annotated69 64- ```Python hl_lines="4-7"65- {!> ../docs_src/arguments/help/tutorial002.py!}66- ```70+/// tip71 72+Prefer to use the `Annotated` version if possible.73+74+///75+76+```Python hl_lines="4-7"77+{!> ../docs_src/arguments/help/tutorial002.py!}78+```79+80+////81+82 And the `--help` option will combine all the information:83 84 <div class="termy">85 17 unmodified lines103 ## Help with defaults104 105 If you have a *CLI argument* with a default value, like `"World"`:106 93-=== "Python 3.7+"107+//// tab | Python 3.7+108 95- ```Python hl_lines="5"96- {!> ../docs_src/arguments/help/tutorial003_an.py!}97- ```109+```Python hl_lines="5"110+{!> ../docs_src/arguments/help/tutorial003_an.py!}111+```112 99-=== "Python 3.7+ non-Annotated"113+////114 101- !!! tip102- Prefer to use the `Annotated` version if possible.115+//// tab | Python 3.7+ non-Annotated116 104- ```Python hl_lines="4"105- {!> ../docs_src/arguments/help/tutorial003.py!}106- ```117+/// tip118 119+Prefer to use the `Annotated` version if possible.120+121+///122+123+```Python hl_lines="4"124+{!> ../docs_src/arguments/help/tutorial003.py!}125+```126+127+////128+129 It will show that default value in the help text:130 131 <div class="termy">132 15 unmodified lines148 </div>149 150 But you can disable that if you want to, with `show_default=False`:151 131-=== "Python 3.7+"152+//// tab | Python 3.7+153 133- ```Python hl_lines="7"134- {!> ../docs_src/arguments/help/tutorial004_an.py!}135- ```154+```Python hl_lines="7"155+{!> ../docs_src/arguments/help/tutorial004_an.py!}156+```157 137-=== "Python 3.7+ non-Annotated"158+////159 139- !!! tip140- Prefer to use the `Annotated` version if possible.160+//// tab | Python 3.7+ non-Annotated161 142- ```Python hl_lines="4"143- {!> ../docs_src/arguments/help/tutorial004.py!}144- ```162+/// tip163 164+Prefer to use the `Annotated` version if possible.165+166+///167+168+```Python hl_lines="4"169+{!> ../docs_src/arguments/help/tutorial004.py!}170+```171+172+////173+174 And then it won't show the default value:175 176 <div class="termy">177 13 unmodified lines191 ```192 193 </div>194 167-!!! note "Technical Details"168- In Click applications the default values are hidden by default. 🙈195+/// note | Technical Details196 170- In **Typer** these default values are shown by default. 👀197+In Click applications the default values are hidden by default. 🙈198 199+In **Typer** these default values are shown by default. 👀200+201+///202+203 ## Custom default string204 205 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:206 176-=== "Python 3.7+"207+//// tab | Python 3.7+208 178- ```Python hl_lines="9"179- {!> ../docs_src/arguments/help/tutorial005_an.py!}180- ```209+```Python hl_lines="9"210+{!> ../docs_src/arguments/help/tutorial005_an.py!}211+```212 182-=== "Python 3.7+ non-Annotated"213+////214 184- !!! tip185- Prefer to use the `Annotated` version if possible.215+//// tab | Python 3.7+ non-Annotated216 187- ```Python hl_lines="6"188- {!> ../docs_src/arguments/help/tutorial005.py!}189- ```217+/// tip218 219+Prefer to use the `Annotated` version if possible.220+221+///222+223+```Python hl_lines="6"224+{!> ../docs_src/arguments/help/tutorial005.py!}225+```226+227+////228+229 And it will be used in the help text:230 231 <div class="termy">232 35 unmodified lines268 But you can customize it with the `metavar` parameter for `typer.Argument()`.269 270 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:271 234-=== "Python 3.7+"272+//// tab | Python 3.7+273 236- ```Python hl_lines="5"237- {!> ../docs_src/arguments/help/tutorial006_an.py!}238- ```274+```Python hl_lines="5"275+{!> ../docs_src/arguments/help/tutorial006_an.py!}276+```277 240-=== "Python 3.7+ non-Annotated"278+////279 242- !!! tip243- Prefer to use the `Annotated` version if possible.280+//// tab | Python 3.7+ non-Annotated281 245- ```Python hl_lines="4"246- {!> ../docs_src/arguments/help/tutorial006.py!}247- ```282+/// tip283 284+Prefer to use the `Annotated` version if possible.285+286+///287+288+```Python hl_lines="4"289+{!> ../docs_src/arguments/help/tutorial006.py!}290+```291+292+////293+294 Now the generated help text will have `✨username✨` instead of `NAME`:295 296 <div class="termy">297 16 unmodified lines314 You might want to show the help information for *CLI arguments* in different panels when using the `--help` option.315 316 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:317 273-=== "Python 3.7+"318+//// tab | Python 3.7+319 275- ```Python hl_lines="8 12"276- {!> ../docs_src/arguments/help/tutorial007_an.py!}277- ```320+```Python hl_lines="8 12"321+{!> ../docs_src/arguments/help/tutorial007_an.py!}322+```323 279-=== "Python 3.7+ non-Annotated"324+////325 281- !!! tip282- Prefer to use the `Annotated` version if possible.326+//// tab | Python 3.7+ non-Annotated327 284- ```Python hl_lines="7 10"285- {!> ../docs_src/arguments/help/tutorial007.py!}286- ```328+/// tip329 330+Prefer to use the `Annotated` version if possible.331+332+///333+334+```Python hl_lines="7 10"335+{!> ../docs_src/arguments/help/tutorial007.py!}336+```337+338+////339+340 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`.341 342 And next you will see other panels for the *CLI arguments* that have a custom panel set in the `rich_help_panel` parameter:343 33 unmodified lines377 If you want, you can make a *CLI argument* **not** show up in the `Arguments` section in the help text.378 379 You will probably not want to do this normally, but it's possible:380 329-=== "Python 3.7+"381+//// tab | Python 3.7+382 331- ```Python hl_lines="5"332- {!> ../docs_src/arguments/help/tutorial008_an.py!}333- ```383+```Python hl_lines="5"384+{!> ../docs_src/arguments/help/tutorial008_an.py!}385+```386 335-=== "Python 3.7+ non-Annotated"387+////388 337- !!! tip338- Prefer to use the `Annotated` version if possible.389+//// tab | Python 3.7+ non-Annotated390 340- ```Python hl_lines="4"341- {!> ../docs_src/arguments/help/tutorial008.py!}342- ```391+/// tip392 393+Prefer to use the `Annotated` version if possible.394+395+///396+397+```Python hl_lines="4"398+{!> ../docs_src/arguments/help/tutorial008.py!}399+```400+401+////402+403 Check it:404 405 <div class="termy">406 10 unmodified lines417 ```418 419 </div>420 362-!!! info363- Have in mind that the *CLI argument* will still show up in the first line with `Usage`.421+/// info422 365- But it won't show up in the main help text under the `Arguments` section.423+Have in mind that the *CLI argument* will still show up in the first line with `Usage`.424 425+But it won't show up in the main help text under the `Arguments` section.426+427+///428+429 ### Help text for *CLI arguments* in Click430 431 Click itself doesn't support adding help for *CLI arguments*, and it doesn't generate help for them as in the "`Arguments:`" sections in the examples above.432 12 unmodified lines445 This is also to help you create CLI programs that are ✨ awesome ✨ *by default*. With very little code.446 447 If you want to keep Click's convention in a **Typer** app, you can do it with the `hidden` parameter as described above.448 387-!!! note "Technical Details"388- To support `help` in *CLI arguments* **Typer** does a lot of internal work in its own sub-classes of Click's internal classes.449+/// note | Technical Details450+451+To support `help` in *CLI arguments* **Typer** does a lot of internal work in its own sub-classes of Click's internal classes.452+453+///modified:docs/tutorial/arguments/optional.md+33 added-15 removed
42 unmodified lines43 ```Python hl_lines="5"44 {!> ../docs_src/arguments/optional/tutorial001_an.py!}45 ```46 47-!!! info48- Typer added support for `Annotated` (and started recommending it) in version 0.9.0.47+/// info48 50- If you have an older version, you would get errors when trying to use `Annotated`.49+Typer added support for `Annotated` (and started recommending it) in version 0.9.0.50 52- Make sure you upgrade the Typer version to at least 0.9.0 before using `Annotated`.51+If you have an older version, you would get errors when trying to use `Annotated`.52 53+Make sure you upgrade the Typer version to at least 0.9.0 before using `Annotated`.54+55+///56+57 Before, you had this function parameter:58 59 ```Python60 name: str63 unmodified lines124 ```125 126 Because we are using `typer.Argument()` **Typer** will know that this is a *CLI argument* (no matter if *required* or *optional*).127 125-!!! tip126- By using `Optional` your editor will be able to know that the value *could* be `None`, and will be able to warn you if you do something assuming it is a `str` that would break if it was `None`.128+/// tip129 130+By using `Optional` your editor will be able to know that the value *could* be `None`, and will be able to warn you if you do something assuming it is a `str` that would break if it was `None`.131+132+///133+134 Check the help:135 136 <div class="termy">137 11 unmodified lines149 ```150 151 </div>152 147-!!! tip148- Notice that `NAME` is still a *CLI argument*, it's shown up there in the "`Usage: main.py` ...".153+/// tip154 150- Also notice that now `[NAME]` has brackets ("`[`" and "`]`") around (before it was just `NAME`) to denote that it's **optional**, not **required**.155+Notice that `NAME` is still a *CLI argument*, it's shown up there in the "`Usage: main.py` ...".156 157+Also notice that now `[NAME]` has brackets ("`[`" and "`]`") around (before it was just `NAME`) to denote that it's **optional**, not **required**.158+159+///160+161 Now run it and test it:162 163 <div class="termy">164 10 unmodified lines175 ```176 177 </div>178 170-!!! tip171- Notice that "`Camila`" here is an optional *CLI argument*, not a *CLI option*, because we didn't use something like "`--name Camila`", we just passed "`Camila`" directly to the program.179+/// tip180 181+Notice that "`Camila`" here is an optional *CLI argument*, not a *CLI option*, because we didn't use something like "`--name Camila`", we just passed "`Camila`" directly to the program.182+183+///184+185 ## Alternative (old) `typer.Argument()` as the default value186 187 **Typer** also supports another older alternative syntax for declaring *CLI arguments* with additional metadata.188 2 unmodified lines191 ```Python hl_lines="4"192 {!> ../docs_src/arguments/optional/tutorial001.py!}193 ```194 183-!!! tip184- Prefer to use the `Annotated` version if possible.195+/// tip196 197+Prefer to use the `Annotated` version if possible.198+199+///200+201 Before, because `name` didn't have any default value it would be a **required parameter** for the Python function, in Python terms.202 203 When using `typer.Argument()` as the default value **Typer** does the same and makes it a **required** *CLI argument*.204 12 unmodified lines217 ```Python218 name: str = typer.Argument(default=...)219 ```220 206-!!! info207- 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>.221+/// info222 223+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>.224+225+///226+227 ```Python hl_lines="4"228 {!> ../docs_src/arguments/optional/tutorial003.py!}229 ```230 21 unmodified linesmodified:docs/tutorial/commands/arguments.md+12 added-6 removed
26 unmodified lines2728293031323132343533343735363738394041423943444526 unmodified lines```
</div>
!!! tip Everything to the *right* of the *command* are *CLI parameters* (*CLI arguments* and *CLI options*) for that command./// tip
!!! note "Technical Details" Actually, it's everything to the right of that command, *before any subcommand*.Everything to the *right* of the *command* are *CLI parameters* (*CLI arguments* and *CLI options*) for that command.
It's possible to have groups of *subcommands*, it's like if one *command* also had *subcommands*. And then those *subcommands* could have their own *CLI parameters*, taking their own *CLI parameters*.///
/// note | Technical Details
Actually, it's everything to the right of that command, *before any subcommand*.
It's possible to have groups of *subcommands*, it's like if one *command* also had *subcommands*. And then those *subcommands* could have their own *CLI parameters*, taking their own *CLI parameters*.
You will see about them later in another section.You will see about them later in another section.
///modified:docs/tutorial/commands/callback.md+12 added-6 removed
14 unmodified lines15 ```16 17 Here we create a `callback` with a `--verbose` *CLI option*.18 19-!!! tip20- After getting the `--verbose` flag, we modify a global `state`, and we use it in the other commands.19+/// tip20 22- There are other ways to achieve the same, but this will suffice for this example.21+After getting the `--verbose` flag, we modify a global `state`, and we use it in the other commands.22 23+There are other ways to achieve the same, but this will suffice for this example.24+25+///26+27 And as we added a docstring to the callback function, by default it will be extracted and used as the help text.28 29 Check it:30 150 unmodified lines181 ```182 183 The original function `cli` would be the equivalent of a Typer callback.184 182-!!! note "Technical Details"183- When using Click, it converts that `cli` variable to a Click `Group` object. And then the original function no longer exists in that variable.185+/// note | Technical Details186 187+When using Click, it converts that `cli` variable to a Click `Group` object. And then the original function no longer exists in that variable.188+185- **Typer** doesn't do that, the callback function is not modified, only registered in the `typer.Typer` app. This is intentional, it's part of **Typer**'s design, to allow having editor auto completion and type checks.189+**Typer** doesn't do that, the callback function is not modified, only registered in the `typer.Typer` app. This is intentional, it's part of **Typer**'s design, to allow having editor auto completion and type checks.190+191+///modified:docs/tutorial/commands/context.md+5 added-2 removed
124 unmodified lines125 ```126 127 </div>128 129-!!! tip130- Notice that it saves all the extra *CLI parameters* as a raw `list` of `str`, including the *CLI option* names and values, everything together.129+/// tip130+131+Notice that it saves all the extra *CLI parameters* as a raw `list` of `str`, including the *CLI option* names and values, everything together.132+133+///modified:docs/tutorial/commands/help.md+90 added-51 removed
1 The same as before, you can add help for the commands in the docstrings and the *CLI options*.2 3 And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:4 5-=== "Python 3.7+"5+//// tab | Python 3.7+6 7- ```Python hl_lines="4 9-11 22 26-30 43 47-51 60-62"8- {!> ../docs_src/commands/help/tutorial001_an.py!}9- ```7+```Python hl_lines="4 9-11 22 26-30 43 47-51 60-62"8+{!> ../docs_src/commands/help/tutorial001_an.py!}9+```10 11-=== "Python 3.7+ non-Annotated"11+////12 13- !!! tip14- Prefer to use the `Annotated` version if possible.13+//// tab | Python 3.7+ non-Annotated14 16- ```Python hl_lines="3 8-10 20 23-27 39 42-46 55-57"17- {!> ../docs_src/commands/help/tutorial001.py!}18- ```15+/// tip16 17+Prefer to use the `Annotated` version if possible.18+19+///20+21+```Python hl_lines="3 8-10 20 23-27 39 42-46 55-57"22+{!> ../docs_src/commands/help/tutorial001.py!}23+```24+25+////26+27 Check it:28 29 <div class="termy">30 66 unmodified lines97 ```98 99 </div>100 94-!!! tip95- `typer.Typer()` receives several other parameters for other things, we'll see that later.101+/// tip102 97- You will also see how to use "Callbacks" later, and those include a way to add this same help message in a function docstring.103+`typer.Typer()` receives several other parameters for other things, we'll see that later.104 105+You will also see how to use "Callbacks" later, and those include a way to add this same help message in a function docstring.106+107+///108+109 ## Overwrite command help110 111 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()`:112 90 unmodified lines203 If you have **Rich** installed as described in [Printing and Colors](../printing.md){.internal-link target=_blank}, you can configure your app to enable markup text with the parameter `rich_markup_mode`.204 205 Then you can use more formatting in the docstrings and the `help` parameter for *CLI arguments* and *CLI options*. You will see more about it below. 👇206 197-!!! info198- By default, `rich_markup_mode` is `None`, which disables any rich text formatting.207+/// info208 209+By default, `rich_markup_mode` is `None`, which disables any rich text formatting.210+211+///212+213 ### Rich Markup214 215 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:216 204-=== "Python 3.7+"217+//// tab | Python 3.7+218 206- ```Python hl_lines="4 10 14-16 21 24 27"207- {!> ../docs_src/commands/help/tutorial004_an.py!}208- ```219+```Python hl_lines="4 10 14-16 21 24 27"220+{!> ../docs_src/commands/help/tutorial004_an.py!}221+```222 210-=== "Python 3.7+ non-Annotated"223+////224 212- !!! tip213- Prefer to use the `Annotated` version if possible.225+//// tab | Python 3.7+ non-Annotated226 215- ```Python hl_lines="3 9 13-15 20 22 24"216- {!> ../docs_src/commands/help/tutorial004.py!}217- ```227+/// tip228 229+Prefer to use the `Annotated` version if possible.230+231+///232+233+```Python hl_lines="3 9 13-15 20 22 24"234+{!> ../docs_src/commands/help/tutorial004.py!}235+```236+237+////238+239 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>.240 241 You can also use markup in the help for the `username` CLI Argument.242 53 unmodified lines296 ### Rich Markdown297 298 If you set `rich_markup_mode="markdown"` when creating the `typer.Typer()` app, you will be able to use Markdown in the docstring:299 280-=== "Python 3.7+"300+//// tab | Python 3.7+301 282- ```Python hl_lines="4 9 12-20 25 27-28"283- {!> ../docs_src/commands/help/tutorial005_an.py!}284- ```302+```Python hl_lines="4 9 12-20 25 27-28"303+{!> ../docs_src/commands/help/tutorial005_an.py!}304+```305 286-=== "Python 3.7+ non-Annotated"306+////307 288- !!! tip289- Prefer to use the `Annotated` version if possible.308+//// tab | Python 3.7+ non-Annotated309 291- ```Python hl_lines="3 7 9-17 22 24-25"292- {!> ../docs_src/commands/help/tutorial005.py!}293- ```310+/// tip311 312+Prefer to use the `Annotated` version if possible.313+314+///315+316+```Python hl_lines="3 7 9-17 22 24-25"317+{!> ../docs_src/commands/help/tutorial005.py!}318+```319+320+////321+322 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>.323 324 And the same as before, the help text overwritten for the command `delete` can also use Markdown.325 50 unmodified lines376 ```377 378 </div>379 353-!!! info354- Notice that in Markdown you cannot define colors. For colors you might prefer to use Rich markup.380+/// info381 382+Notice that in Markdown you cannot define colors. For colors you might prefer to use Rich markup.383+384+///385+386 ## Help Panels387 388 If you have many commands or CLI parameters, you might want to show their documentation in different panels when using the `--help` option.389 2 unmodified lines392 ### Help Panels for Commands393 394 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:395 366-=== "Python 3.7+"396+//// tab | Python 3.7+397 368- ```Python hl_lines="22 30 38 46"369- {!> ../docs_src/commands/help/tutorial006.py!}370- ```398+```Python hl_lines="22 30 38 46"399+{!> ../docs_src/commands/help/tutorial006.py!}400+```401 402+////403+404 Commands without a panel will be shown in the default panel `Commands`, and the rest will be shown in the next panels:405 406 <div class="termy">407 31 unmodified lines439 The same way, you can configure the panels for *CLI arguments* and *CLI options* with `rich_help_panel`.440 441 And of course, in the same application you can also set the `rich_help_panel` for commands.442 411-=== "Python 3.7+"443+//// tab | Python 3.7+444 413- ```Python hl_lines="15 21 27 37"414- {!> ../docs_src/commands/help/tutorial007_an.py!}415- ```445+```Python hl_lines="15 21 27 37"446+{!> ../docs_src/commands/help/tutorial007_an.py!}447+```448 417-=== "Python 3.7+ non-Annotated"449+////450 419- !!! tip420- Prefer to use the `Annotated` version if possible.451+//// tab | Python 3.7+ non-Annotated452 422- ```Python hl_lines="12 16 21 30"423- {!> ../docs_src/commands/help/tutorial007.py!}424- ```453+/// tip454 455+Prefer to use the `Annotated` version if possible.456+457+///458+459+```Python hl_lines="12 16 21 30"460+{!> ../docs_src/commands/help/tutorial007.py!}461+```462+463+////464+465 Then if you run the application you will see all the *CLI parameters* in their respective panels.466 467 * First the ***CLI arguments*** that don't have a panel name set in a **default** one named "`Arguments`".468 * Next the ***CLI arguments*** with a **custom panel**. In this example named "`Secondary Arguments`".103 unmodified linesmodified:docs/tutorial/commands/index.md+36 added-18 removed
26 unmodified lines27 Another command of `git` is `git pull`, it also has some *CLI parameters*.28 29 It's like if the same big program `git` had several small programs inside.30 31-!!! tip32- A command looks the same as a *CLI argument*, it's just some name without a preceding `--`. But commands have a predefined name, and are used to group different sets of functionalities into the same CLI application.31+/// tip32 33+A command looks the same as a *CLI argument*, it's just some name without a preceding `--`. But commands have a predefined name, and are used to group different sets of functionalities into the same CLI application.34+35+///36+37 ## Command or subcommand38 39 It's common to call a CLI program a "command".40 28 unmodified lines69 * Create a new `typer.Typer()` "application".70 * Create a new "`command`" with your function.71 * Call the same "application" as if it was a function with "`app()`".72 70-!!! info "`@decorator` Info"71- That `@something` syntax in Python is called a "decorator".73+/// info | `@decorator` Info74 73- You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).75+That `@something` syntax in Python is called a "decorator".76 75- A "decorator" takes the function below and does something with it.77+You put it on top of a function. Like a pretty decorative hat (I guess that's where the term came from).78 77- In our case, this decorator tells **Typer** that the function below is a "`command`".79+A "decorator" takes the function below and does something with it.80 81+In our case, this decorator tells **Typer** that the function below is a "`command`".82+83+///84+85 Both ways, with `typer.run()` and creating the explicit application, achieve almost the same.86 81-!!! tip82- If your use case is solved with just `typer.run()`, that's fine, you don't have to create the explicit `app` and use `@app.command()`, etc.87+/// tip88 84- You might want to do that later when your app needs the extra features, but if it doesn't need them yet, that's fine.89+If your use case is solved with just `typer.run()`, that's fine, you don't have to create the explicit `app` and use `@app.command()`, etc.90 91+You might want to do that later when your app needs the extra features, but if it doesn't need them yet, that's fine.92+93+///94+95 If you run the second example, with the explicit `app`, it works exactly the same:96 97 <div class="termy">98 117 unmodified lines216 </div>217 218 Notice that the help text now shows the 2 commands: `create` and `delete`.219 211-!!! tip212- By default, the names of the commands are generated from the function name.220+/// tip221 222+By default, the names of the commands are generated from the function name.223+224+///225+226 ## Show the help message if no command is given227 228 By default, we need to specify `--help` to get the command's help page.229 28 unmodified lines258 ## Click Group259 260 If you come from Click, a `typer.Typer` app with subcommands is more or less the equivalent of a <a href="https://click.palletsprojects.com/en/7.x/quickstart/#nesting-commands" class="external-link" target="_blank">Click Group</a>.261 250-!!! note "Technical Details"251- A `typer.Typer` app is *not* a Click Group, but it provides the equivalent functionality. And it creates a Click Group when calling it.262+/// note | Technical Details263 253- It is not directly a Group because **Typer** doesn't modify the functions in your code to convert them to another type of object, it only registers them.264+A `typer.Typer` app is *not* a Click Group, but it provides the equivalent functionality. And it creates a Click Group when calling it.265 266+It is not directly a Group because **Typer** doesn't modify the functions in your code to convert them to another type of object, it only registers them.267+268+///269+270 ## Decorator Technical Details271 272 When you use `@app.command()` the function under the decorator is registered in the **Typer** application and is then used later by the application.273 274 But Typer doesn't modify that function itself, the function is left as is.275 276 That means that if your function is simple enough that you could create it without using `typer.Option()` or `typer.Argument()`, you could use the same function for a **Typer** application and a **FastAPI** application putting both decorators on top, or similar tricks.277 263-!!! note "Click Technical Details"264- This behavior is a design difference with Click.278+/// note | Click Technical Details279 280+This behavior is a design difference with Click.281+266- In Click, when you add a `@click.command()` decorator it actually modifies the function underneath and replaces it with an object.282+In Click, when you add a `@click.command()` decorator it actually modifies the function underneath and replaces it with an object.283+284+///modified:docs/tutorial/commands/one-or-multiple.md+5 added-2 removed
33 unmodified lines34 ```35 36 </div>37 38-!!! tip39- Notice that it doesn't show a command `main`, even though the function name is `main`.38+/// tip39 40+Notice that it doesn't show a command `main`, even though the function name is `main`.41+42+///43+44 But if you add multiple commands, **Typer** will create one *CLI command* for each one of them:45 46 ```Python hl_lines="6 11"47 {!../docs_src/commands/index/tutorial002.py!}107 unmodified linesmodified:docs/tutorial/commands/options.md+22 added-12 removed
1 Commands can also have their own *CLI options*.2 3 In fact, each command can have different *CLI arguments* and *CLI options*:4 5-=== "Python 3.7+"5+//// tab | Python 3.7+6 7- ```Python hl_lines="8 14-17 27-29 38"8- {!> ../docs_src/commands/options/tutorial001_an.py!}9- ```7+```Python hl_lines="8 14-17 27-29 38"8+{!> ../docs_src/commands/options/tutorial001_an.py!}9+```10 11-=== "Python 3.7+ non-Annotated"11+////12 13- !!! tip14- Prefer to use the `Annotated` version if possible.13+//// tab | Python 3.7+ non-Annotated14 16- ```Python hl_lines="7 13-14 24 33"17- {!> ../docs_src/commands/options/tutorial001.py!}18- ```15+/// tip16 17+Prefer to use the `Annotated` version if possible.18+19+///20+21+```Python hl_lines="7 13-14 24 33"22+{!> ../docs_src/commands/options/tutorial001.py!}23+```24+25+////26+27 Here we have multiple commands, with different *CLI parameters*:28 29 * `create`:30 * `username`: a *CLI argument*.26 unmodified lines57 ```58 59 </div>60 54-!!! tip55- Check the command `delete-all`, by default command names are generated from the function name, replacing `_` with `-`.61+/// tip62 63+Check the command `delete-all`, by default command names are generated from the function name, replacing `_` with `-`.64+65+///66+67 Test it:68 69 <div class="termy">70 47 unmodified linesmodified:docs/tutorial/multiple-values/arguments-with-multiple-values.md+27 added-14 removed
19 unmodified lines20 ```21 22 </div>23 24-!!! tip25- We also declared a final *CLI argument* `celebration`, and it's correctly used even if we pass an arbitrary number of `files` first.24+/// tip25 27-!!! info28- A `List` can only be used in the last command (if there are subcommands), as this will take anything to the right and assume it's part of the expected *CLI arguments*.26+We also declared a final *CLI argument* `celebration`, and it's correctly used even if we pass an arbitrary number of `files` first.27 28+///29+30+/// info31+32+A `List` can only be used in the last command (if there are subcommands), as this will take anything to the right and assume it's part of the expected *CLI arguments*.33+34+///35+36 ## *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 34-=== "Python 3.7+"40+//// tab | Python 3.7+41 36- ```Python hl_lines="8-10"37- {!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py!}38- ```42+```Python hl_lines="8-10"43+{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002_an.py!}44+```45 40-=== "Python 3.7+ non-Annotated"46+////47 42- !!! tip43- Prefer to use the `Annotated` version if possible.48+//// tab | Python 3.7+ non-Annotated49 45- ```Python hl_lines="7-8"46- {!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002.py!}47- ```50+/// tip51 52+Prefer to use the `Annotated` version if possible.53+54+///55+56+```Python hl_lines="7-8"57+{!> ../docs_src/multiple_values/arguments_with_multiple_values/tutorial002.py!}58+```59+60+////61+62 Check it:63 64 <div class="termy">65 33 unmodified linesmodified:docs/tutorial/multiple-values/multiple-options.md+34 added-20 removed
2 unmodified lines3 For example, let's say you want to accept several users in a single execution.4 5 For this, use the standard Python `typing.List` to declare it as a `list` of `str`:6 7-=== "Python 3.7+"7+//// tab | Python 3.7+8 9- ```Python hl_lines="1 7"10- {!> ../docs_src/multiple_values/multiple_options/tutorial001_an.py!}11- ```9+```Python hl_lines="1 7"10+{!> ../docs_src/multiple_values/multiple_options/tutorial001_an.py!}11+```12 13-=== "Python 3.7+ non-Annotated"13+////14 15- !!! tip16- Prefer to use the `Annotated` version if possible.15+//// tab | Python 3.7+ non-Annotated16 18- ```Python hl_lines="1 6"19- {!> ../docs_src/multiple_values/multiple_options/tutorial001.py!}20- ```17+/// tip18 19+Prefer to use the `Annotated` version if possible.20+21+///22+23+```Python hl_lines="1 6"24+{!> ../docs_src/multiple_values/multiple_options/tutorial001.py!}25+```26+27+////28+29 You will receive the values as you declared them, as a `list` of `str`.30 31 Check it:32 24 unmodified lines57 ## Multiple `float`58 59 The same way, you can use other types and they will be converted by **Typer** to their declared type:60 54-=== "Python 3.7+"61+//// tab | Python 3.7+62 56- ```Python hl_lines="7"57- {!> ../docs_src/multiple_values/multiple_options/tutorial002_an.py!}58- ```63+```Python hl_lines="7"64+{!> ../docs_src/multiple_values/multiple_options/tutorial002_an.py!}65+```66 60-=== "Python 3.7+ non-Annotated"67+////68 62- !!! tip63- Prefer to use the `Annotated` version if possible.69+//// tab | Python 3.7+ non-Annotated70 65- ```Python hl_lines="6"66- {!> ../docs_src/multiple_values/multiple_options/tutorial002.py!}67- ```71+/// tip72 73+Prefer to use the `Annotated` version if possible.74+75+///76+77+```Python hl_lines="6"78+{!> ../docs_src/multiple_values/multiple_options/tutorial002.py!}79+```80+81+////82+83 Check it:84 85 <div class="termy">86 17 unmodified linesmodified:docs/tutorial/multiple-values/options-with-multiple-values.md+23 added-13 removed
2 unmodified lines3 You can set the number of values and types to anything you want, but it has to be a fixed number of values.4 5 For this, use the standard Python `typing.Tuple`:6 7-=== "Python 3.7+"7+//// tab | Python 3.7+8 9- ```Python hl_lines="1 7"10- {!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py!}11- ```9+```Python hl_lines="1 7"10+{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001_an.py!}11+```12 13-=== "Python 3.7+ non-Annotated"13+////14 15- !!! tip16- Prefer to use the `Annotated` version if possible.15+//// tab | Python 3.7+ non-Annotated16 18- ```Python hl_lines="1 6"19- {!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001.py!}20- ```17+/// tip18 19+Prefer to use the `Annotated` version if possible.20+21+///22+23+```Python hl_lines="1 6"24+{!> ../docs_src/multiple_values/options_with_multiple_values/tutorial001.py!}25+```26+27+////28+29 Each of the internal types defines the type of each value in the tuple.30 31 So:32 32 unmodified lines65 coins = user[1]66 is_wizard = user[2]67 ```68 62-!!! tip63- Notice that the default is a tuple with `(None, None, None)`.69+/// tip70 65- You cannot simply use `None` here as the default because <a href="https://github.com/pallets/click/issues/472" class="external-link" target="_blank">Click doesn't support it</a>.71+Notice that the default is a tuple with `(None, None, None)`.72 73+You cannot simply use `None` here as the default because <a href="https://github.com/pallets/click/issues/472" class="external-link" target="_blank">Click doesn't support it</a>.74+75+///76+77 ## Check it78 79 Now let's see how this works in the terminal:80 31 unmodified linesmodified:docs/tutorial/options/callback-and-context.md+68 added-40 removed
4 unmodified lines5 ## Validate *CLI parameters*6 7 For example, you could do some validation before the rest of the code is executed.8 9-=== "Python 3.7+"9+//// tab | Python 3.7+10 11- ```Python hl_lines="7-10 13"12- {!> ../docs_src/options/callback/tutorial001_an.py!}13- ```11+```Python hl_lines="7-10 13"12+{!> ../docs_src/options/callback/tutorial001_an.py!}13+```14 15-=== "Python 3.7+ non-Annotated"15+////16 17- !!! tip18- Prefer to use the `Annotated` version if possible.17+//// tab | Python 3.7+ non-Annotated18 20- ```Python hl_lines="6-9 12"21- {!> ../docs_src/options/callback/tutorial001.py!}22- ```19+/// tip20 21+Prefer to use the `Annotated` version if possible.22+23+///24+25+```Python hl_lines="6-9 12"26+{!> ../docs_src/options/callback/tutorial001.py!}27+```28+29+////30+31 Here you pass a function to `typer.Option()` or `typer.Argument()` with the keyword argument `callback`.32 33 The function receives the value from the command line. It can do anything with it, and then return the value.34 76 unmodified lines111 ### Breaking completion in a callback112 113 Let's say that when the callback is running, we want to show a message saying that it's validating the name:114 108-=== "Python 3.7+"115+//// tab | Python 3.7+116 110- ```Python hl_lines="8"111- {!> ../docs_src/options/callback/tutorial002_an.py!}112- ```117+```Python hl_lines="8"118+{!> ../docs_src/options/callback/tutorial002_an.py!}119+```120 114-=== "Python 3.7+ non-Annotated"121+////122 116- !!! tip117- Prefer to use the `Annotated` version if possible.123+//// tab | Python 3.7+ non-Annotated124 119- ```Python hl_lines="7"120- {!> ../docs_src/options/callback/tutorial002.py!}121- ```125+/// tip126 127+Prefer to use the `Annotated` version if possible.128+129+///130+131+```Python hl_lines="7"132+{!> ../docs_src/options/callback/tutorial002.py!}133+```134+135+////136+137 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.138 139 It will look something like:140 25 unmodified lines166 But you can access the context by declaring a function parameter of type `typer.Context`.167 168 The "context" has some additional data about the current execution of your program:169 156-=== "Python 3.7+"170+//// tab | Python 3.7+171 158- ```Python hl_lines="7-9"159- {!> ../docs_src/options/callback/tutorial003_an.py!}160- ```172+```Python hl_lines="7-9"173+{!> ../docs_src/options/callback/tutorial003_an.py!}174+```175 162-=== "Python 3.7+ non-Annotated"176+////177 164- !!! tip165- Prefer to use the `Annotated` version if possible.178+//// tab | Python 3.7+ non-Annotated179 167- ```Python hl_lines="6-8"168- {!> ../docs_src/options/callback/tutorial003.py!}169- ```180+/// tip181 182+Prefer to use the `Annotated` version if possible.183+184+///185+186+```Python hl_lines="6-8"187+{!> ../docs_src/options/callback/tutorial003.py!}188+```189+190+////191+192 The `ctx.resilient_parsing` will be `True` when handling completion, so you can just return without printing anything else.193 194 But it will be `False` when calling the program normally. So you can continue the execution of your previous code.195 22 unmodified lines218 ## Using the `CallbackParam` object219 220 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.221 201-=== "Python 3.7+"222+//// tab | Python 3.7+223 203- ```Python hl_lines="7 10"204- {!> ../docs_src/options/callback/tutorial004_an.py!}205- ```224+```Python hl_lines="7 10"225+{!> ../docs_src/options/callback/tutorial004_an.py!}226+```227 207-=== "Python 3.7+ non-Annotated"228+////229 209- !!! tip210- Prefer to use the `Annotated` version if possible.230+//// tab | Python 3.7+ non-Annotated231 212- ```Python hl_lines="6 9"213- {!> ../docs_src/options/callback/tutorial004.py!}214- ```232+/// tip233 234+Prefer to use the `Annotated` version if possible.235+236+///237+238+```Python hl_lines="6 9"239+{!> ../docs_src/options/callback/tutorial004.py!}240+```241+242+////243+244 It's probably not very common, but you could do it if you need it.245 246 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.247 42 unmodified linesmodified:docs/tutorial/options/help.md+74 added-43 removed
1 You already saw how to add a help text for *CLI arguments* with the `help` parameter.2 3 Let's now do the same for *CLI options*:4 5-=== "Python 3.7+"5+//// tab | Python 3.7+6 7- ```Python hl_lines="7-8"8- {!> ../docs_src/options/help/tutorial001_an.py!}9- ```7+```Python hl_lines="7-8"8+{!> ../docs_src/options/help/tutorial001_an.py!}9+```10 11-=== "Python 3.7+ non-Annotated"11+////12 13- !!! tip14- Prefer to use the `Annotated` version if possible.13+//// tab | Python 3.7+ non-Annotated14 16- ```Python hl_lines="6-7"17- {!> ../docs_src/options/help/tutorial001.py!}18- ```15+/// tip16 17+Prefer to use the `Annotated` version if possible.18+19+///20+21+```Python hl_lines="6-7"22+{!> ../docs_src/options/help/tutorial001.py!}23+```24+25+////26+27 The same way as with `typer.Argument()`, we can put `typer.Option()` inside of `Annotated`.28 29 We can then pass the `help` keyword parameter:30 42 unmodified lines73 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.74 75 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*:76 70-=== "Python 3.7+"77+//// tab | Python 3.7+78 72- ```Python hl_lines="11 17"73- {!> ../docs_src/options/help/tutorial002_an.py!}74- ```79+```Python hl_lines="11 17"80+{!> ../docs_src/options/help/tutorial002_an.py!}81+```82 76-=== "Python 3.7+ non-Annotated"83+////84 78- !!! tip79- Prefer to use the `Annotated` version if possible.85+//// tab | Python 3.7+ non-Annotated86 81- ```Python hl_lines="8 11"82- {!> ../docs_src/options/help/tutorial002.py!}83- ```87+/// tip88 89+Prefer to use the `Annotated` version if possible.90+91+///92+93+```Python hl_lines="8 11"94+{!> ../docs_src/options/help/tutorial002.py!}95+```96+97+////98+99 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`.100 101 And below you will see other panels for the *CLI options* that have a custom panel set in the `rich_help_panel` parameter:102 36 unmodified lines139 ## Hide default from help140 141 You can tell Typer to not show the default value in the help text with `show_default=False`:142 129-=== "Python 3.7+"143+//// tab | Python 3.7+144 131- ```Python hl_lines="5"132- {!> ../docs_src/options/help/tutorial003_an.py!}133- ```145+```Python hl_lines="5"146+{!> ../docs_src/options/help/tutorial003_an.py!}147+```148 135-=== "Python 3.7+ non-Annotated"149+////150 137- !!! tip138- Prefer to use the `Annotated` version if possible.151+//// tab | Python 3.7+ non-Annotated152 140- ```Python hl_lines="4"141- {!> ../docs_src/options/help/tutorial003.py!}142- ```153+/// tip154 155+Prefer to use the `Annotated` version if possible.156+157+///158+159+```Python hl_lines="4"160+{!> ../docs_src/options/help/tutorial003.py!}161+```162+163+////164+165 And it will no longer show the default value in the help text:166 167 <div class="termy">168 15 unmodified lines184 ```185 186 </div>187 167-!!! note "Technical Details"168- In Click applications the default values are hidden by default. 🙈188+/// note | Technical Details189 170- In **Typer** these default values are shown by default. 👀190+In Click applications the default values are hidden by default. 🙈191 192+In **Typer** these default values are shown by default. 👀193+194+///195+196 ## Custom default string197 198 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:199 176-=== "Python 3.7+"200+//// tab | Python 3.7+201 178- ```Python hl_lines="7"179- {!> ../docs_src/options/help/tutorial004_an.py!}180- ```202+```Python hl_lines="7"203+{!> ../docs_src/options/help/tutorial004_an.py!}204+```205 182-=== "Python 3.7+ non-Annotated"206+////207 184- !!! tip185- Prefer to use the `Annotated` version if possible.208+//// tab | Python 3.7+ non-Annotated209 187- ```Python hl_lines="6"188- {!> ../docs_src/options/help/tutorial004.py!}189- ```210+/// tip211 212+Prefer to use the `Annotated` version if possible.213+214+///215+216+```Python hl_lines="6"217+{!> ../docs_src/options/help/tutorial004.py!}218+```219+220+////221+222 And it will be used in the help text:223 224 <div class="termy">225 18 unmodified linesmodified:docs/tutorial/options/name.md+106 added-62 removed
24 unmodified lines25 Let's say the function parameter name is `user_name` as above, but you want the *CLI option* to be just `--name`.26 27 You can pass the *CLI option* name that you want to have in the following positional argument passed to `typer.Option()`:28 29-=== "Python 3.7+"29+//// tab | Python 3.7+30 31- ```Python hl_lines="5"32- {!> ../docs_src/options/name/tutorial001_an.py!}33- ```31+```Python hl_lines="5"32+{!> ../docs_src/options/name/tutorial001_an.py!}33+```34 35- Here you are passing the string `"--name"` as the first positional argument to `typer.Option()`.35+Here you are passing the string `"--name"` as the first positional argument to `typer.Option()`.36 37-=== "Python 3.7+ non-Annotated"37+////38 39- !!! tip40- Prefer to use the `Annotated` version if possible.39+//// tab | Python 3.7+ non-Annotated40 42- ```Python hl_lines="4"43- {!> ../docs_src/options/name/tutorial001.py!}44- ```41+/// tip42 46- 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.43+Prefer to use the `Annotated` version if possible.44 48-!!! info49- "<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.45+///46 51- For example `show_default=True` is a keyword argument. "`show_default`" is the keyword.47+```Python hl_lines="4"48+{!> ../docs_src/options/name/tutorial001.py!}49+```50 53- But in `"--name"` there's no `option_name="--name"` or something similar, it's just the string value `"--name"` that goes in `typer.Option()`.51+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.52 55- That's a "positional argument" in a function.53+////54 55+/// info56+57+"<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.58+59+For example `show_default=True` is a keyword argument. "`show_default`" is the keyword.60+61+But in `"--name"` there's no `option_name="--name"` or something similar, it's just the string value `"--name"` that goes in `typer.Option()`.62+63+That's a "positional argument" in a function.64+65+///66+67 Check it:68 69 <div class="termy">70 123 unmodified lines194 In **Typer** you can also define *CLI option* short names the same way you can customize the long names.195 196 You can pass *positional* arguments to `typer.Option()` to define the *CLI option* name(s).197 188-!!! tip189- Remember the *positional* function arguments are those that don't have a keyword.198+/// tip199 191- All the other function arguments/parameters you pass to `typer.Option()` like `prompt=True` and `help="This option blah, blah"` require the keyword.200+Remember the *positional* function arguments are those that don't have a keyword.201 202+All the other function arguments/parameters you pass to `typer.Option()` like `prompt=True` and `help="This option blah, blah"` require the keyword.203+204+///205+206 You can overwrite the *CLI option* name to use as in the previous example, but you can also declare extra alternatives, including short names.207 208 For example, extending the previous example, let's add a *CLI option* short name `-n`:209 197-=== "Python 3.7+"210+//// tab | Python 3.7+211 199- ```Python hl_lines="5"200- {!> ../docs_src/options/name/tutorial002_an.py!}201- ```212+```Python hl_lines="5"213+{!> ../docs_src/options/name/tutorial002_an.py!}214+```215 203-=== "Python 3.7+ non-Annotated"216+////217 205- !!! tip206- Prefer to use the `Annotated` version if possible.218+//// tab | Python 3.7+ non-Annotated219 208- ```Python hl_lines="4"209- {!> ../docs_src/options/name/tutorial002.py!}210- ```220+/// tip221 222+Prefer to use the `Annotated` version if possible.223+224+///225+226+```Python hl_lines="4"227+{!> ../docs_src/options/name/tutorial002.py!}228+```229+230+////231+232 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`.233 234 Check it:235 21 unmodified lines257 ### *CLI option* only short name258 259 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.260 241-=== "Python 3.7+"261+//// tab | Python 3.7+262 243- ```Python hl_lines="5"244- {!> ../docs_src/options/name/tutorial003_an.py!}245- ```263+```Python hl_lines="5"264+{!> ../docs_src/options/name/tutorial003_an.py!}265+```266 247-=== "Python 3.7+ non-Annotated"267+////268 249- !!! tip250- Prefer to use the `Annotated` version if possible.269+//// tab | Python 3.7+ non-Annotated270 252- ```Python hl_lines="4"253- {!> ../docs_src/options/name/tutorial003.py!}254- ```271+/// tip272 273+Prefer to use the `Annotated` version if possible.274+275+///276+277+```Python hl_lines="4"278+{!> ../docs_src/options/name/tutorial003.py!}279+```280+281+////282+283 Check it:284 285 <div class="termy">286 18 unmodified lines305 ### *CLI option* short name and default306 307 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:308 282-=== "Python 3.7+"309+//// tab | Python 3.7+310 284- ```Python hl_lines="5"285- {!> ../docs_src/options/name/tutorial004_an.py!}286- ```311+```Python hl_lines="5"312+{!> ../docs_src/options/name/tutorial004_an.py!}313+```314 288-=== "Python 3.7+ non-Annotated"315+////316 290- !!! tip291- Prefer to use the `Annotated` version if possible.317+//// tab | Python 3.7+ non-Annotated318 293- ```Python hl_lines="4"294- {!> ../docs_src/options/name/tutorial004.py!}295- ```319+/// tip320 321+Prefer to use the `Annotated` version if possible.322+323+///324+325+```Python hl_lines="4"326+{!> ../docs_src/options/name/tutorial004.py!}327+```328+329+////330+331 Check it:332 333 <div class="termy">334 24 unmodified lines359 You can create multiple short names and use them together.360 361 You don't have to do anything special for it to work (apart from declaring those short versions):362 329-=== "Python 3.7+"363+//// tab | Python 3.7+364 331- ```Python hl_lines="6-7"332- {!> ../docs_src/options/name/tutorial005_an.py!}333- ```365+```Python hl_lines="6-7"366+{!> ../docs_src/options/name/tutorial005_an.py!}367+```368 335-=== "Python 3.7+ non-Annotated"369+////370 337- !!! tip338- Prefer to use the `Annotated` version if possible.371+//// tab | Python 3.7+ non-Annotated372 340- ```Python hl_lines="5-6"341- {!> ../docs_src/options/name/tutorial005.py!}342- ```373+/// tip374 344-!!! tip345- Notice that, again, we are declaring the long and short version of the *CLI option* names.375+Prefer to use the `Annotated` version if possible.376 377+///378+379+```Python hl_lines="5-6"380+{!> ../docs_src/options/name/tutorial005.py!}381+```382+383+////384+385+/// tip386+387+Notice that, again, we are declaring the long and short version of the *CLI option* names.388+389+///390+391 Check it:392 393 <div class="termy">394 25 unmodified linesmodified:docs/tutorial/options/password.md+34 added-20 removed
1 Apart from having a prompt, you can make a *CLI option* have a `confirmation_prompt=True`:2 3-=== "Python 3.7+"3+//// tab | Python 3.7+4 5- ```Python hl_lines="7"6- {!> ../docs_src/options/password/tutorial001_an.py!}7- ```5+```Python hl_lines="7"6+{!> ../docs_src/options/password/tutorial001_an.py!}7+```8 9-=== "Python 3.7+ non-Annotated"9+////10 11- !!! tip12- Prefer to use the `Annotated` version if possible.11+//// tab | Python 3.7+ non-Annotated12 14- ```Python hl_lines="5"15- {!> ../docs_src/options/password/tutorial001.py!}16- ```13+/// tip14 15+Prefer to use the `Annotated` version if possible.16+17+///18+19+```Python hl_lines="5"20+{!> ../docs_src/options/password/tutorial001.py!}21+```22+23+////24+25 And the CLI program will ask for confirmation:26 27 <div class="termy">28 18 unmodified lines47 You can achieve the same using `hide_input=True`.48 49 And if you combine it with `confirmation_prompt=True` you can easily receive a password with double confirmation:50 44-=== "Python 3.7+"51+//// tab | Python 3.7+52 46- ```Python hl_lines="8"47- {!> ../docs_src/options/password/tutorial002_an.py!}48- ```53+```Python hl_lines="8"54+{!> ../docs_src/options/password/tutorial002_an.py!}55+```56 50-=== "Python 3.7+ non-Annotated"57+////58 52- !!! tip53- Prefer to use the `Annotated` version if possible.59+//// tab | Python 3.7+ non-Annotated60 55- ```Python hl_lines="6-8"56- {!> ../docs_src/options/password/tutorial002.py!}57- ```61+/// tip62 63+Prefer to use the `Annotated` version if possible.64+65+///66+67+```Python hl_lines="6-8"68+{!> ../docs_src/options/password/tutorial002.py!}69+```70+71+////72+73 Check it:74 75 <div class="termy">76 13 unmodified linesmodified:docs/tutorial/options/prompt.md+51 added-30 removed
1 It's also possible to, instead of just showing an error, ask for the missing value with `prompt=True`:2 3-=== "Python 3.7+"3+//// tab | Python 3.7+4 5- ```Python hl_lines="5"6- {!> ../docs_src/options/prompt/tutorial001_an.py!}7- ```5+```Python hl_lines="5"6+{!> ../docs_src/options/prompt/tutorial001_an.py!}7+```8 9-=== "Python 3.7+ non-Annotated"9+////10 11- !!! tip12- Prefer to use the `Annotated` version if possible.11+//// tab | Python 3.7+ non-Annotated12 14- ```Python hl_lines="4"15- {!> ../docs_src/options/prompt/tutorial001.py!}16- ```13+/// tip14 15+Prefer to use the `Annotated` version if possible.16+17+///18+19+```Python hl_lines="4"20+{!> ../docs_src/options/prompt/tutorial001.py!}21+```22+23+////24+25 And then your program will ask the user for it in the terminal:26 27 <div class="termy">28 12 unmodified lines41 ## Customize the prompt42 43 You can also set a custom prompt, passing the string that you want to use instead of just `True`:44 38-=== "Python 3.7+"45+//// tab | Python 3.7+46 40- ```Python hl_lines="7"41- {!> ../docs_src/options/prompt/tutorial002_an.py!}42- ```47+```Python hl_lines="7"48+{!> ../docs_src/options/prompt/tutorial002_an.py!}49+```50 44-=== "Python 3.7+ non-Annotated"51+////52 46- !!! tip47- Prefer to use the `Annotated` version if possible.53+//// tab | Python 3.7+ non-Annotated54 49- ```Python hl_lines="5"50- {!> ../docs_src/options/prompt/tutorial002.py!}51- ```55+/// tip56 57+Prefer to use the `Annotated` version if possible.58+59+///60+61+```Python hl_lines="5"62+{!> ../docs_src/options/prompt/tutorial002.py!}63+```64+65+////66+67 And then your program will ask for it using with your custom prompt:68 69 <div class="termy">70 16 unmodified lines87 You can do it passing the parameter `confirmation_prompt=True`.88 89 Let's say it's a CLI app to delete a project:90 77-=== "Python 3.7+"91+//// tab | Python 3.7+92 79- ```Python hl_lines="6"80- {!> ../docs_src/options/prompt/tutorial003_an.py!}81- ```93+```Python hl_lines="6"94+{!> ../docs_src/options/prompt/tutorial003_an.py!}95+```96 83-=== "Python 3.7+ non-Annotated"97+////98 85- !!! tip86- Prefer to use the `Annotated` version if possible.99+//// tab | Python 3.7+ non-Annotated100 88- ```Python hl_lines="4"89- {!> ../docs_src/options/prompt/tutorial003.py!}90- ```101+/// tip102 103+Prefer to use the `Annotated` version if possible.104+105+///106+107+```Python hl_lines="4"108+{!> ../docs_src/options/prompt/tutorial003.py!}109+```110+111+////112+113 And it will prompt the user for a value and then for the confirmation:114 115 <div class="termy">116 26 unmodified linesmodified:docs/tutorial/options/required.md+28 added-16 removed
9 unmodified lines10 To make a *CLI option* required, you can put `typer.Option()` inside of `Annotated` and leave the parameter without a default value.11 12 Let's make `--lastname` a required *CLI option*:13 14-=== "Python 3.7+"14+//// tab | Python 3.7+15 16- ```Python hl_lines="5"17- {!> ../docs_src/options/required/tutorial001_an.py!}18- ```16+```Python hl_lines="5"17+{!> ../docs_src/options/required/tutorial001_an.py!}18+```19 20+////21+22 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.23 22-=== "Python 3.7+ non-Annotated"24+//// tab | Python 3.7+ non-Annotated25 24- ```Python hl_lines="4"25- {!> ../docs_src/options/required/tutorial001.py!}26- ```26+```Python hl_lines="4"27+{!> ../docs_src/options/required/tutorial001.py!}28+```29 30+////31+32 Or you can explictily pass `...` to `typer.Option(default=...)`:33 30-=== "Python 3.7+ non-Annotated"34+//// tab | Python 3.7+ non-Annotated35 32- ```Python hl_lines="4"33- {!> ../docs_src/options/required/tutorial002.py!}34- ```36+```Python hl_lines="4"37+{!> ../docs_src/options/required/tutorial002.py!}38+```39 36-!!! info37- 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>.40+////41 42+/// info43+44+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>.45+46+///47+48 That will tell **Typer** that it's still a *CLI option*, but it doesn't have a default value, and it's required.49 41-!!! tip42- Again, prefer to use the `Annotated` version if possible. That way your code will mean the same in standard Python and in **Typer**.50+/// tip51 52+Again, prefer to use the `Annotated` version if possible. That way your code will mean the same in standard Python and in **Typer**.53+54+///55+56 And test it:57 58 <div class="termy">59 28 unmodified linesmodified:docs/tutorial/options/version.md+71 added-38 removed
4 unmodified lines5 ## First version of `--version`6 7 Let's see a first version of how it could look like:8 9-=== "Python 3.7+"9+//// tab | Python 3.7+10 11- ```Python hl_lines="9-12 17-19"12- {!> ../docs_src/options/version/tutorial001_an.py!}13- ```11+```Python hl_lines="9-12 17-19"12+{!> ../docs_src/options/version/tutorial001_an.py!}13+```14 15-=== "Python 3.7+ non-Annotated"15+////16 17- !!! tip18- Prefer to use the `Annotated` version if possible.17+//// tab | Python 3.7+ non-Annotated18 20- ```Python hl_lines="8-11 16-18"21- {!> ../docs_src/options/version/tutorial001.py!}22- ```19+/// tip20 24-!!! tip25- 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.21+Prefer to use the `Annotated` version if possible.22 23+///24+25+```Python hl_lines="8-11 16-18"26+{!> ../docs_src/options/version/tutorial001.py!}27+```28+29+////30+31+/// tip32+33+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.34+35+///36+37 If the `--version` *CLI option* is passed, we get a value `True` in the callback.38 39 Then we can print the version and raise `typer.Exit()` to make sure the program is terminated before anything else is executed.40 33 unmodified lines74 ## Previous parameters and `is_eager`75 76 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:77 68-=== "Python 3.7+"78+//// tab | Python 3.7+79 70- ```Python hl_lines="15-17 22-24"71- {!> ../docs_src/options/version/tutorial002_an.py!}72- ```80+```Python hl_lines="15-17 22-24"81+{!> ../docs_src/options/version/tutorial002_an.py!}82+```83 74-=== "Python 3.7+ non-Annotated"84+////85 76- !!! tip77- Prefer to use the `Annotated` version if possible.86+//// tab | Python 3.7+ non-Annotated87 79- ```Python hl_lines="14-16 21-23"80- {!> ../docs_src/options/version/tutorial002.py!}81- ```88+/// tip89 90+Prefer to use the `Annotated` version if possible.91+92+///93+94+```Python hl_lines="14-16 21-23"95+{!> ../docs_src/options/version/tutorial002.py!}96+```97+98+////99+100 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:101 102 <div class="termy">103 5 unmodified lines109 ```110 111 </div>112 96-!!! tip97- We don't have to check for `ctx.resilient_parsing` in the `name_callback()` for completion to work, because we are not using `typer.echo()`, instead we are raising a `typer.BadParameter`.113+/// tip114 99-!!! note "Technical Details"100- `typer.BadParameter` prints the error to "standard error", not to "standard output", and because the completion system only reads from "standard output", it won't break completion.115+We don't have to check for `ctx.resilient_parsing` in the `name_callback()` for completion to work, because we are not using `typer.echo()`, instead we are raising a `typer.BadParameter`.116 102-!!! info103- If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](../printing.md#standard-output-and-standard-error){.internal-link target=_blank}.117+///118 119+/// note | Technical Details120+121+`typer.BadParameter` prints the error to "standard error", not to "standard output", and because the completion system only reads from "standard output", it won't break completion.122+123+///124+125+/// info126+127+If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](../printing.md#standard-output-and-standard-error){.internal-link target=_blank}.128+129+///130+131 ### Fix with `is_eager`132 133 For those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*) with `is_eager=True`.134 135 That will tell **Typer** (actually Click) that it should process this *CLI parameter* before the others:136 111-=== "Python 3.7+"137+//// tab | Python 3.7+138 113- ```Python hl_lines="23-26"114- {!> ../docs_src/options/version/tutorial003_an.py!}115- ```139+```Python hl_lines="23-26"140+{!> ../docs_src/options/version/tutorial003_an.py!}141+```142 117-=== "Python 3.7+ non-Annotated"143+////144 119- !!! tip120- Prefer to use the `Annotated` version if possible.145+//// tab | Python 3.7+ non-Annotated146 122- ```Python hl_lines="22-24"123- {!> ../docs_src/options/version/tutorial003.py!}124- ```147+/// tip148 149+Prefer to use the `Annotated` version if possible.150+151+///152+153+```Python hl_lines="22-24"154+{!> ../docs_src/options/version/tutorial003.py!}155+```156+157+////158+159 Check it:160 161 <div class="termy">162 8 unmodified linesmodified:docs/tutorial/parameter-types/bool.md+74 added-43 removed
6 unmodified lines7 Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.8 9 We can do that by specifying the exact name we want:10 11-=== "Python 3.7+"11+//// tab | Python 3.7+12 13- ```Python hl_lines="5"14- {!> ../docs_src/parameter_types/bool/tutorial001_an.py!}15- ```13+```Python hl_lines="5"14+{!> ../docs_src/parameter_types/bool/tutorial001_an.py!}15+```16 17-=== "Python 3.7+ non-Annotated"17+////18 19- !!! tip20- Prefer to use the `Annotated` version if possible.19+//// tab | Python 3.7+ non-Annotated20 22- ```Python hl_lines="4"23- {!> ../docs_src/parameter_types/bool/tutorial001.py!}24- ```21+/// tip22 23+Prefer to use the `Annotated` version if possible.24+25+///26+27+```Python hl_lines="4"28+{!> ../docs_src/parameter_types/bool/tutorial001.py!}29+```30+31+////32+33 Now there's only a `--force` *CLI option*:34 35 <div class="termy">36 38 unmodified lines75 We might want to instead have `--accept` and `--reject`.76 77 We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:78 72-=== "Python 3.7+"79+//// tab | Python 3.7+80 74- ```Python hl_lines="7"75- {!> ../docs_src/parameter_types/bool/tutorial002_an.py!}76- ```81+```Python hl_lines="7"82+{!> ../docs_src/parameter_types/bool/tutorial002_an.py!}83+```84 78-=== "Python 3.7+ non-Annotated"85+////86 80- !!! tip81- Prefer to use the `Annotated` version if possible.87+//// tab | Python 3.7+ non-Annotated88 83- ```Python hl_lines="6"84- {!> ../docs_src/parameter_types/bool/tutorial002.py!}85- ```89+/// tip90 91+Prefer to use the `Annotated` version if possible.92+93+///94+95+```Python hl_lines="6"96+{!> ../docs_src/parameter_types/bool/tutorial002.py!}97+```98+99+////100+101 Check it:102 103 <div class="termy">104 31 unmodified lines136 The same way, you can declare short versions of the names for these *CLI options*.137 138 For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:139 126-=== "Python 3.7+"140+//// tab | Python 3.7+141 128- ```Python hl_lines="5"129- {!> ../docs_src/parameter_types/bool/tutorial003_an.py!}130- ```142+```Python hl_lines="5"143+{!> ../docs_src/parameter_types/bool/tutorial003_an.py!}144+```145 132-=== "Python 3.7+ non-Annotated"146+////147 134- !!! tip135- Prefer to use the `Annotated` version if possible.148+//// tab | Python 3.7+ non-Annotated149 137- ```Python hl_lines="4"138- {!> ../docs_src/parameter_types/bool/tutorial003.py!}139- ```150+/// tip151 152+Prefer to use the `Annotated` version if possible.153+154+///155+156+```Python hl_lines="4"157+{!> ../docs_src/parameter_types/bool/tutorial003.py!}158+```159+160+////161+162 Check it:163 164 <div class="termy">165 26 unmodified lines192 If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.193 194 To do that, use a space and a single `/` and pass the negative name after:195 175-=== "Python 3.7+"196+//// tab | Python 3.7+197 177- ```Python hl_lines="5"178- {!> ../docs_src/parameter_types/bool/tutorial004_an.py!}179- ```198+```Python hl_lines="5"199+{!> ../docs_src/parameter_types/bool/tutorial004_an.py!}200+```201 181-=== "Python 3.7+ non-Annotated"202+////203 183- !!! tip184- Prefer to use the `Annotated` version if possible.204+//// tab | Python 3.7+ non-Annotated205 186- ```Python hl_lines="4"187- {!> ../docs_src/parameter_types/bool/tutorial004.py!}188- ```206+/// tip207 190-!!! tip191- Have in mind that it's a string with a preceding space and then a `/`.208+Prefer to use the `Annotated` version if possible.209 193- So, it's `" /-S"` not `"/-S"`.210+///211 212+```Python hl_lines="4"213+{!> ../docs_src/parameter_types/bool/tutorial004.py!}214+```215+216+////217+218+/// tip219+220+Have in mind that it's a string with a preceding space and then a `/`.221+222+So, it's `" /-S"` not `"/-S"`.223+224+///225+226 Check it:227 228 <div class="termy">229 28 unmodified linesmodified:docs/tutorial/parameter-types/custom-types.md+22 added-22 removed
9 unmodified lines10 ## Type Parser11 12 `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>.13 14-=== "Python 3.7+"14+//// tab | Python 3.7+15 16- ```Python hl_lines="13-14 18-19"17- {!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}18- ```16+```Python hl_lines="13-14 18-19"17+{!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}18+```19 20-=== "Python 3.7+ non-Annotated"20+////21 22- !!! tip23- Prefer to use the `Annotated` version if possible.22+//// tab | Python 3.7+ non-Annotated23 25- ```Python hl_lines="12-13 17-18"26- {!> ../docs_src/parameter_types/custom_types/tutorial001.py!}27- ```24+/// tip25 26+Prefer to use the `Annotated` version if possible.27+28+///29+30+```Python hl_lines="12-13 17-18"31+{!> ../docs_src/parameter_types/custom_types/tutorial001.py!}32+```33+34+////35+36 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.37 38 ## Click Custom Type39 40 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.41 35-=== "Python 3.7+"42+//// tab | Python 3.7+43 37- ```Python hl_lines="14-18 22-25"38- {!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}39- ```44+```Python hl_lines="14-18 22-25"45+{!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}46+```47 41-=== "Python 3.7+ non-Annotated"42-43- !!! tip44- Prefer to use the `Annotated` version if possible.45-46- ```Python hl_lines="13-17 21-22"47- {!> ../docs_src/parameter_types/custom_types/tutorial002.py!}48- ```48+////modified:docs/tutorial/parameter-types/datetime.md+22 added-12 removed
56 unmodified lines57 * separated with "`/`"58 59 ...It's a crazy example, but let's say you also needed that strange format:60 61-=== "Python 3.7+"61+//// tab | Python 3.7+62 63- ```Python hl_lines="11"64- {!> ../docs_src/parameter_types/datetime/tutorial002_an.py!}65- ```63+```Python hl_lines="11"64+{!> ../docs_src/parameter_types/datetime/tutorial002_an.py!}65+```66 67-=== "Python 3.7+ non-Annotated"67+////68 69- !!! tip70- Prefer to use the `Annotated` version if possible.69+//// tab | Python 3.7+ non-Annotated70 72- ```Python hl_lines="8"73- {!> ../docs_src/parameter_types/datetime/tutorial002.py!}74- ```71+/// tip72 76-!!! tip77- Notice the last string in `formats`: `"%m/%d/%Y"`.73+Prefer to use the `Annotated` version if possible.74 75+///76+77+```Python hl_lines="8"78+{!> ../docs_src/parameter_types/datetime/tutorial002.py!}79+```80+81+////82+83+/// tip84+85+Notice the last string in `formats`: `"%m/%d/%Y"`.86+87+///88+89 Check it:90 91 <div class="termy">92 13 unmodified linesmodified:docs/tutorial/parameter-types/enum.md+40 added-23 removed
2 unmodified lines3 ```Python hl_lines="1 6 7 8 9 12 13"4 {!../docs_src/parameter_types/enum/tutorial001.py!}5 ```6 7-!!! tip8- Notice that the function parameter `network` will be an `Enum`, not a `str`.7+/// tip8 10- To get the `str` value in your function's code use `network.value`.9+Notice that the function parameter `network` will be an `Enum`, not a `str`.10 11+To get the `str` value in your function's code use `network.value`.12+13+///14+15 Check it:16 17 <div class="termy">18 34 unmodified lines53 ### Case insensitive Enum choices54 55 You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:56 54-=== "Python 3.7+"57+//// tab | Python 3.7+58 56- ```Python hl_lines="15"57- {!> ../docs_src/parameter_types/enum/tutorial002_an.py!}58- ```59+```Python hl_lines="15"60+{!> ../docs_src/parameter_types/enum/tutorial002_an.py!}61+```62 60-=== "Python 3.7+ non-Annotated"63+////64 62- !!! tip63- Prefer to use the `Annotated` version if possible.65+//// tab | Python 3.7+ non-Annotated66 65- ```Python hl_lines="13"66- {!> ../docs_src/parameter_types/enum/tutorial002.py!}67- ```67+/// tip68 69+Prefer to use the `Annotated` version if possible.70+71+///72+73+```Python hl_lines="13"74+{!> ../docs_src/parameter_types/enum/tutorial002.py!}75+```76+77+////78+79 And then the values of the `Enum` will be checked no matter if lower case, upper case, or a mix:80 81 <div class="termy">82 14 unmodified lines97 ### List of Enum values98 99 A *CLI parameter* can also take a list of `Enum` values:100 91-=== "Python 3.7+"101+//// tab | Python 3.7+102 93- ```Python hl_lines="14"94- {!> ../docs_src/parameter_types/enum/tutorial003_an.py!}95- ```103+```Python hl_lines="14"104+{!> ../docs_src/parameter_types/enum/tutorial003_an.py!}105+```106 97-=== "Python 3.7+ non-Annotated"107+////108 99- !!! tip100- Prefer to use the `Annotated` version if possible.109+//// tab | Python 3.7+ non-Annotated110 102- ```Python hl_lines="13"103- {!> ../docs_src/parameter_types/enum/tutorial003.py!}104- ```111+/// tip112 113+Prefer to use the `Annotated` version if possible.114+115+///116+117+```Python hl_lines="13"118+{!> ../docs_src/parameter_types/enum/tutorial003.py!}119+```120+121+////122+123 This works just like any other parameter value taking a list of things:124 125 <div class="termy">126 27 unmodified linesmodified:docs/tutorial/parameter-types/file.md+108 added-61 removed
1 Apart from `Path` *CLI parameters* you can also declare some types of "files".2 3-!!! tip4- In most of the cases you are probably fine just using `Path`.3+/// tip4 6- You can read and write data with `Path` the same way.5+In most of the cases you are probably fine just using `Path`.6 7+You can read and write data with `Path` the same way.8+9+///10+11 The difference is that these types will give you a Python <a href="https://docs.python.org/3/glossary.html#term-file-object" class="external-link" target="_blank">file-like object</a> instead of a Python <a href="https://docs.python.org/3/library/pathlib.html#basic-use" class="external-link" target="_blank">Path</a>.12 13 A "file-like object" is the same type of object returned by `open()` as in:14 29 unmodified lines44 ```45 46 You will get all the correct editor support, attributes, methods, etc for the file-like object:`47 45-=== "Python 3.7+"48+//// tab | Python 3.7+49 47- ```Python hl_lines="5"48- {!> ../docs_src/parameter_types/file/tutorial001_an.py!}49- ```50+```Python hl_lines="5"51+{!> ../docs_src/parameter_types/file/tutorial001_an.py!}52+```53 51-=== "Python 3.7+ non-Annotated"54+////55 53- !!! tip54- Prefer to use the `Annotated` version if possible.56+//// tab | Python 3.7+ non-Annotated57 56- ```Python hl_lines="4"57- {!> ../docs_src/parameter_types/file/tutorial001.py!}58- ```58+/// tip59 60+Prefer to use the `Annotated` version if possible.61+62+///63+64+```Python hl_lines="4"65+{!> ../docs_src/parameter_types/file/tutorial001.py!}66+```67+68+////69+70 Check it:71 72 <div class="termy">73 17 unmodified lines91 ## `FileTextWrite`92 93 For writing text, you can use `typer.FileTextWrite`:94 85-=== "Python 3.7+"95+//// tab | Python 3.7+96 87- ```Python hl_lines="5-6"88- {!> ../docs_src/parameter_types/file/tutorial002_an.py!}89- ```97+```Python hl_lines="5-6"98+{!> ../docs_src/parameter_types/file/tutorial002_an.py!}99+```100 91-=== "Python 3.7+ non-Annotated"101+////102 93- !!! tip94- Prefer to use the `Annotated` version if possible.103+//// tab | Python 3.7+ non-Annotated104 96- ```Python hl_lines="4-5"97- {!> ../docs_src/parameter_types/file/tutorial002.py!}98- ```105+/// tip106 107+Prefer to use the `Annotated` version if possible.108+109+///110+111+```Python hl_lines="4-5"112+{!> ../docs_src/parameter_types/file/tutorial002.py!}113+```114+115+////116+117 This would be for writing human text, like:118 119 ```120 some settings18 unmodified lines139 ```140 141 </div>142 126-!!! info "Technical Details"127- `typer.FileTextWrite` is a just a convenience class.143+/// info | Technical Details144 129- It's the same as using `typer.FileText` and setting `mode="w"`. You will learn about `mode` later below.145+`typer.FileTextWrite` is a just a convenience class.146 147+It's the same as using `typer.FileText` and setting `mode="w"`. You will learn about `mode` later below.148+149+///150+151 ## `FileBinaryRead`152 153 To read binary data you can use `typer.FileBinaryRead`.154 155 You will receive `bytes` from it.156 157 It's useful for reading binary files like images:158 139-=== "Python 3.7+"159+//// tab | Python 3.7+160 141- ```Python hl_lines="5"142- {!> ../docs_src/parameter_types/file/tutorial003_an.py!}143- ```161+```Python hl_lines="5"162+{!> ../docs_src/parameter_types/file/tutorial003_an.py!}163+```164 145-=== "Python 3.7+ non-Annotated"165+////166 147- !!! tip148- Prefer to use the `Annotated` version if possible.167+//// tab | Python 3.7+ non-Annotated168 150- ```Python hl_lines="4"151- {!> ../docs_src/parameter_types/file/tutorial003.py!}152- ```169+/// tip170 171+Prefer to use the `Annotated` version if possible.172+173+///174+175+```Python hl_lines="4"176+{!> ../docs_src/parameter_types/file/tutorial003.py!}177+```178+179+////180+181 Check it:182 183 <div class="termy">184 19 unmodified lines204 Have in mind that you have to pass `bytes` to its `.write()` method, not `str`.205 206 If you have a `str`, you have to encode it first to get `bytes`.207 181-=== "Python 3.7+"208+//// tab | Python 3.7+209 183- ```Python hl_lines="5"184- {!> ../docs_src/parameter_types/file/tutorial004_an.py!}185- ```210+```Python hl_lines="5"211+{!> ../docs_src/parameter_types/file/tutorial004_an.py!}212+```213 187-=== "Python 3.7+ non-Annotated"214+////215 189- !!! tip190- Prefer to use the `Annotated` version if possible.216+//// tab | Python 3.7+ non-Annotated217 192- ```Python hl_lines="4"193- {!> ../docs_src/parameter_types/file/tutorial004.py!}194- ```218+/// tip219 220+Prefer to use the `Annotated` version if possible.221+222+///223+224+```Python hl_lines="4"225+{!> ../docs_src/parameter_types/file/tutorial004.py!}226+```227+228+////229+230 <div class="termy">231 232 ```console233 $ python main.py --file binary.dat40 unmodified lines274 You can override the `mode` from the defaults above.275 276 For example, you could use `mode="a"` to write "appending" to the same file:277 244-=== "Python 3.7+"278+//// tab | Python 3.7+279 246- ```Python hl_lines="5"247- {!> ../docs_src/parameter_types/file/tutorial005_an.py!}248- ```280+```Python hl_lines="5"281+{!> ../docs_src/parameter_types/file/tutorial005_an.py!}282+```283 250-=== "Python 3.7+ non-Annotated"284+////285 252- !!! tip253- Prefer to use the `Annotated` version if possible.286+//// tab | Python 3.7+ non-Annotated287 255- ```Python hl_lines="4"256- {!> ../docs_src/parameter_types/file/tutorial005.py!}257- ```288+/// tip289 259-!!! tip260- As you are manually setting `mode="a"`, you can use `typer.FileText` or `typer.FileTextWrite`, both will work.290+Prefer to use the `Annotated` version if possible.291 292+///293+294+```Python hl_lines="4"295+{!> ../docs_src/parameter_types/file/tutorial005.py!}296+```297+298+////299+300+/// tip301+302+As you are manually setting `mode="a"`, you can use `typer.FileText` or `typer.FileTextWrite`, both will work.303+304+///305+306 Check it:307 308 <div class="termy">309 22 unmodified lines332 </div>333 334 ## About the different types335 292-!!! info293- These are technical details about why the different types/classes provided by **Typer**.336+/// info337 295- But you don't need this information to be able to use them. You can skip it.338+These are technical details about why the different types/classes provided by **Typer**.339 340+But you don't need this information to be able to use them. You can skip it.341+342+///343+344 **Typer** provides you these different types (classes) because they inherit directly from the actual Python implementation that will be provided underneath for each case.345 346 This way your editor will give you the right type checks and completion for each type.347 3 unmodified linesmodified:docs/tutorial/parameter-types/index.md+5 added-2 removed
60 unmodified lines61 62 See more about specific types and validations in the next sections...63 64 65-!!! info "Technical Details"66- All the types you will see in the next sections are handled underneath by <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" class="external-link" target="_blank">Click's Parameter Types</a>.65+/// info | Technical Details66+67+All the types you will see in the next sections are handled underneath by <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" class="external-link" target="_blank">Click's Parameter Types</a>.68+69+///modified:docs/tutorial/parameter-types/number.md+51 added-30 removed
1 You can define numeric validations with `max` and `min` values for `int` and `float` *CLI parameters*:2 3-=== "Python 3.7+"3+//// tab | Python 3.7+4 5- ```Python hl_lines="6-8"6- {!> ../docs_src/parameter_types/number/tutorial001_an.py!}7- ```5+```Python hl_lines="6-8"6+{!> ../docs_src/parameter_types/number/tutorial001_an.py!}7+```8 9-=== "Python 3.7+ non-Annotated"9+////10 11- !!! tip12- Prefer to use the `Annotated` version if possible.11+//// tab | Python 3.7+ non-Annotated12 14- ```Python hl_lines="5-7"15- {!> ../docs_src/parameter_types/number/tutorial001.py!}16- ```13+/// tip14 15+Prefer to use the `Annotated` version if possible.16+17+///18+19+```Python hl_lines="5-7"20+{!> ../docs_src/parameter_types/number/tutorial001.py!}21+```22+23+////24+25 *CLI arguments* and *CLI options* can both use these validations.26 27 You can specify `min`, `max` or both.28 61 unmodified lines90 You might want to, instead of showing an error, use the closest minimum or maximum valid values.91 92 You can do it with the `clamp` parameter:93 87-=== "Python 3.7+"94+//// tab | Python 3.7+95 89- ```Python hl_lines="6-8"90- {!> ../docs_src/parameter_types/number/tutorial002_an.py!}91- ```96+```Python hl_lines="6-8"97+{!> ../docs_src/parameter_types/number/tutorial002_an.py!}98+```99 93-=== "Python 3.7+ non-Annotated"100+////101 95- !!! tip96- Prefer to use the `Annotated` version if possible.102+//// tab | Python 3.7+ non-Annotated103 98- ```Python hl_lines="5-7"99- {!> ../docs_src/parameter_types/number/tutorial002.py!}100- ```104+/// tip105 106+Prefer to use the `Annotated` version if possible.107+108+///109+110+```Python hl_lines="5-7"111+{!> ../docs_src/parameter_types/number/tutorial002.py!}112+```113+114+////115+116 And then, when you pass data that is out of the valid range, it will be "clamped", the closest valid value will be used:117 118 <div class="termy">119 19 unmodified lines139 ## Counter *CLI options*140 141 You can make a *CLI option* work as a counter with the `counter` parameter:142 129-=== "Python 3.7+"143+//// tab | Python 3.7+144 131- ```Python hl_lines="5"132- {!> ../docs_src/parameter_types/number/tutorial003_an.py!}133- ```145+```Python hl_lines="5"146+{!> ../docs_src/parameter_types/number/tutorial003_an.py!}147+```148 135-=== "Python 3.7+ non-Annotated"149+////150 137- !!! tip138- Prefer to use the `Annotated` version if possible.151+//// tab | Python 3.7+ non-Annotated152 140- ```Python hl_lines="4"141- {!> ../docs_src/parameter_types/number/tutorial003.py!}142- ```153+/// tip154 155+Prefer to use the `Annotated` version if possible.156+157+///158+159+```Python hl_lines="4"160+{!> ../docs_src/parameter_types/number/tutorial003.py!}161+```162+163+////164+165 It means that the *CLI option* will be like a boolean flag, e.g. `--verbose`.166 167 And the value you receive in the function will be the amount of times that `--verbose` was added:168 35 unmodified linesmodified:docs/tutorial/parameter-types/path.md+50 added-27 removed
1 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>.2 3 This is what you would do for directory paths, file paths, etc:4 5-=== "Python 3.7+"5+//// tab | Python 3.7+6 7- ```Python hl_lines="1 8"8- {!> ../docs_src/parameter_types/path/tutorial001_an.py!}9- ```7+```Python hl_lines="1 8"8+{!> ../docs_src/parameter_types/path/tutorial001_an.py!}9+```10 11-=== "Python 3.7+ non-Annotated"11+////12 13- !!! tip14- Prefer to use the `Annotated` version if possible.13+//// tab | Python 3.7+ non-Annotated14 16- ```Python hl_lines="1 7"17- {!> ../docs_src/parameter_types/path/tutorial001.py!}18- ```15+/// tip16 17+Prefer to use the `Annotated` version if possible.18+19+///20+21+```Python hl_lines="1 7"22+{!> ../docs_src/parameter_types/path/tutorial001.py!}23+```24+25+////26+27 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.28 29 Check it:30 37 unmodified lines68 * `writable`: if true, a writable check is performed.69 * `readable`: if true, a readable check is performed.70 * `resolve_path`: if this is true, then the path is fully resolved before the value is passed onwards. This means that it’s absolute and <abbr title="symbolic links, also known as shortcuts. Links in a file system that point to other location. For example, some applications when installed create symlinks in the desktop to launch them.">symlinks</abbr> are resolved.71 65-!!! note "Technical Details"66- It will not expand a tilde-prefix (something with `~`, like `~/Documents/`), as this is supposed to be done by the shell only.72+/// note | Technical Details73 68-!!! tip69- All these parameters come directly from <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" class="external-link" target="_blank">Click</a>.74+It will not expand a tilde-prefix (something with `~`, like `~/Documents/`), as this is supposed to be done by the shell only.75 76+///77+78+/// tip79+80+All these parameters come directly from <a href="https://click.palletsprojects.com/en/7.x/parameters/#parameter-types" class="external-link" target="_blank">Click</a>.81+82+///83+84 For example:85 73-=== "Python 3.7+"86+//// tab | Python 3.7+87 75- ```Python hl_lines="11-16"76- {!> ../docs_src/parameter_types/path/tutorial002_an.py!}77- ```88+```Python hl_lines="11-16"89+{!> ../docs_src/parameter_types/path/tutorial002_an.py!}90+```91 79-=== "Python 3.7+ non-Annotated"92+////93 81- !!! tip82- Prefer to use the `Annotated` version if possible.94+//// tab | Python 3.7+ non-Annotated95 84- ```Python hl_lines="9-14"85- {!> ../docs_src/parameter_types/path/tutorial002.py!}86- ```96+/// tip97 98+Prefer to use the `Annotated` version if possible.99+100+///101+102+```Python hl_lines="9-14"103+{!> ../docs_src/parameter_types/path/tutorial002.py!}104+```105+106+////107+108 Check it:109 110 <div class="termy">111 25 unmodified lines137 </div>138 139 ### Advanced `Path` configurations140 121-!!! warning "Advanced Details"122- You probably won't need these configurations at first, you may want to skip it.141+/// warning | Advanced Details142 124- They are used for more advanced use cases.143+You probably won't need these configurations at first, you may want to skip it.144 145+They are used for more advanced use cases.146+147+///148+149 * `allow_dash`: If this is set to True, a single dash to indicate standard streams is permitted.150 * `path_type`: optionally a string type that should be used to represent the path. The default is None which means the return value will be either bytes or unicode depending on what makes most sense given the input data Click deals with.modified:docs/tutorial/parameter-types/uuid.md+11 added-10 removed
1-!!! info2- A UUID is a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier" class="external-link" target="_blank">"Universally Unique Identifier"</a>.1+/// info2 4- It's a standard format for identifiers, like passport numbers, but for anything, not just people in countries.3+A UUID is a <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier" class="external-link" target="_blank">"Universally Unique Identifier"</a>.4 6- They look like this:5+It's a standard format for identifiers, like passport numbers, but for anything, not just people in countries.6 8- ```9- d48edaa6-871a-4082-a196-4daab372d4a110- ```7+They look like this:8 12- The way they are generated makes them sufficiently long and random that you could assume that every UUID generated is unique. Even if it was generated by a different application, database, or system.9+```10+d48edaa6-871a-4082-a196-4daab372d4a111+```12 14- So, if your system uses UUIDs to identify your data, you could mix it with the data from some other system that also uses UUIDs with some confidence that their IDs (UUIDs) won't clash with yours.13+The way they are generated makes them sufficiently long and random that you could assume that every UUID generated is unique. Even if it was generated by a different application, database, or system.14 16- This wouldn't be true if you just used `int`s as identifiers, as most databases do.15+So, if your system uses UUIDs to identify your data, you could mix it with the data from some other system that also uses UUIDs with some confidence that their IDs (UUIDs) won't clash with yours.16 17+This wouldn't be true if you just used `int`s as identifiers, as most databases do.18 19+///20 21 You can declare a *CLI parameter* as a UUID:22 23 ```Python hl_lines="1 6 7 8"26 unmodified linesmodified:docs/tutorial/subcommands/add-typer.md+5 added-2 removed
125 unmodified lines126 ```127 128 </div>129 130-!!! tip131- Notice that we are still calling `$ python main.py` but now we are using the command `items`.130+/// tip131 132+Notice that we are still calling `$ python main.py` but now we are using the command `items`.133+134+///135+136 And now check the command `users`, with all its subcommands:137 138 <div class="termy">139 33 unmodified linesmodified:docs/tutorial/subcommands/name-and-help.md+16 added-7 removed
50 unmodified lines51 We can set the `name` and `help` in several places, each one taking precedence over the other, overriding the previous value.52 53 Let's see those locations.54 55-!!! tip56- There are other attributes that can be set in that same way in the same places we'll see next.55+/// tip56 58- But those are documented later in another section.57+There are other attributes that can be set in that same way in the same places we'll see next.58 59+But those are documented later in another section.60+61+///62+63 ## Inferring name and help from callback64 65 ### Inferring a command's name and help66 231 unmodified lines298 ## Set the name and help299 300 Let's now see the places where you can set the command name and help text, from lowest priority to highest.301 299-!!! tip300- Setting the name and help text explicitly always has a higher precedence than inferring from a callback function.302+/// tip303 304+Setting the name and help text explicitly always has a higher precedence than inferring from a callback function.305+306+///307+308 ### Name and help in `typer.Typer()`309 310 You could have all the callbacks and overrides we defined before, but the name and help text was inferred from the function name and docstring.311 4 unmodified lines316 ```Python hl_lines="12"317 {!../docs_src/subcommands/name_help/tutorial006.py!}318 ```319 314-!!! info315- 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.320+/// info321 322+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.323+324+///325+326 We set an explicit name `exp-users`, and an explicit help `Explicit help.`.327 328 So that will take precedence now.329 142 unmodified linesmodified:docs/tutorial/subcommands/nested-subcommands.md+12 added-6 removed
276 unmodified lines277 ```Python278 {!../docs_src/subcommands/tutorial003/main.py!}279 ```280 281-!!! tip282- All these files have an `if __name__ == "__main__"` block just to demonstrate how each of them can also be an independent *CLI app*.281+/// tip282 284- But for your final application, only `main.py` would need it.283+All these files have an `if __name__ == "__main__"` block just to demonstrate how each of them can also be an independent *CLI app*.284 285+But for your final application, only `main.py` would need it.286+287+///288+289 ## Recap290 291 That's it, you can just add **Typer** applications one inside another as much as you want and create complex *CLI programs* while writing simple code.292 293 You can probably achieve a simpler *CLI program* design that's easier to use than the example here. But if your requirements are complex, **Typer** helps you build your *CLI app* easily.294 292-!!! tip293- Auto completion helps a lot, specially with complex programs.295+/// tip296 297+Auto completion helps a lot, specially with complex programs.298+295- Check the docs about adding auto completion to your *CLI apps*.299+Check the docs about adding auto completion to your *CLI apps*.300+301+///modified:docs/tutorial/first-steps.md+64 added-37 removed
91 unmodified lines92 ```93 94 </div>95 96-!!! tip97- If you need to pass a single value that contains spaces to a *CLI argument*, use quotes (`"`) around it.96+/// tip97 98+If you need to pass a single value that contains spaces to a *CLI argument*, use quotes (`"`) around it.99+100+///101+102 ## Two CLI arguments103 104 Now let's say we want to have the name and last name separated.105 41 unmodified lines147 ```148 149 </div>150 148-!!! tip149- Notice that the order is important. The last name has to go after the first name.151+/// tip152 151- If you called it with:153+Notice that the order is important. The last name has to go after the first name.154 153- ```154- $ python main.py Gutiérrez Camila155- ```155+If you called it with:156 157- your app wouldn't have a way to know which is the `name` and which the `lastname`. It expects the first *CLI argument* to be the `name` and the second *CLI argument* to be the `lastname`.157+```158+$ python main.py Gutiérrez Camila159+```160 161+your app wouldn't have a way to know which is the `name` and which the `lastname`. It expects the first *CLI argument* to be the `name` and the second *CLI argument* to be the `lastname`.162+163+///164+165 ## What is a **CLI option**166 167 Here we will use the word **CLI option** to refer to *CLI parameters* passed to the CLI application with a specific name. For example, if you go to your terminal and type:168 50 unmodified lines219 220 * *CLI options* **start with `--`** and don't depend on the order221 * *CLI arguments* depend on the **sequence order**222 217-!!! tip218- In this example above the *CLI option* `--size` is just a "flag" or "switch" that will contain a boolean value, `True` or `False`, depending on if it was added to the command or not.223+/// tip224 220- This one doesn't receive any values. But *CLI options* can also receive values like *CLI arguments*. You'll see how later.225+In this example above the *CLI option* `--size` is just a "flag" or "switch" that will contain a boolean value, `True` or `False`, depending on if it was added to the command or not.226 227+This one doesn't receive any values. But *CLI options* can also receive values like *CLI arguments*. You'll see how later.228+229+///230+231 ## Add one *CLI option*232 233 Now add a `--formal` *CLI option*:234 23 unmodified lines258 ```259 260 </div>261 253-!!! tip254- Notice that it automatically creates a `--formal` and a `--no-formal` because it detected that `formal` is a `bool`.262+/// tip263 264+Notice that it automatically creates a `--formal` and a `--no-formal` because it detected that `formal` is a `bool`.265+266+///267+268 Now call it normally:269 270 <div class="termy">271 48 unmodified lines320 ```321 322 </div>323 312-!!! tip313- Notice the `--lastname`, and notice that it takes a textual value.324+/// tip325 315- A *CLI option* with a value like `--lastname` (contrary to a *CLI option* without a value, a `bool` flag, like `--formal` or `--size`) takes as its value whatever is at the *right side* of the *CLI option*.326+Notice the `--lastname`, and notice that it takes a textual value.327 328+A *CLI option* with a value like `--lastname` (contrary to a *CLI option* without a value, a `bool` flag, like `--formal` or `--size`) takes as its value whatever is at the *right side* of the *CLI option*.329+330+///331+332 <div class="termy">333 334 ```console335 // Call it without a --lastname8 unmodified lines344 ```345 346 </div>347 333-!!! tip334- Notice that "`Gutiérrez`" is at the right side of `--lastname`. A *CLI option* with a value takes as its value whatever is at the *right side*.348+/// tip349 350+Notice that "`Gutiérrez`" is at the right side of `--lastname`. A *CLI option* with a value takes as its value whatever is at the *right side*.351+352+///353+354 And as `--lastname` is now a *CLI option* that doesn't depend on the order, you can pass it before the name:355 356 <div class="termy">357 38 unmodified lines396 ```397 398 </div>399 382-!!! tip383- There is another place to document the specific *CLI options* and *CLI arguments* that will show up next to them in the help text as with `--install-completion` or `--help`, you will learn that later in the tutorial.400+/// tip401 402+There is another place to document the specific *CLI options* and *CLI arguments* that will show up next to them in the help text as with `--install-completion` or `--help`, you will learn that later in the tutorial.403+404+///405+406 ## Arguments, options, parameters, optional, required407 408 Be aware that these terms refer to multiple things depending on the context, and sadly, those "contexts" mix frequently, so it's easy to get confused.409 7 unmodified lines417 ```418 419 are called "Python function parameters" or "Python function arguments".420 400-!!! note "Technical Details"401- There's actually a very small distinction in Python between "parameter" and "argument".421+/// note | Technical Details422 403- It's quite technical... and somewhat pedantic.423+There's actually a very small distinction in Python between "parameter" and "argument".424 405- *Parameter* refers to the variable name in a function *declaration*. Like:425+It's quite technical... and somewhat pedantic.426 407- ```408- def bring_person(name: str, lastname: str = ""):409- pass410- ```427+*Parameter* refers to the variable name in a function *declaration*. Like:428 412- *Argument* refers to the value passed when *calling* a function. Like:429+```430+def bring_person(name: str, lastname: str = ""):431+ pass432+```433 414- ```415- person = bring_person("Camila", lastname="Gutiérrez")416- ```434+*Argument* refers to the value passed when *calling* a function. Like:435 418- ...but you will probably see them used interchangeably in most of the places (including here).436+```437+person = bring_person("Camila", lastname="Gutiérrez")438+```439 440+...but you will probably see them used interchangeably in most of the places (including here).441+442+///443+444 #### Python default values445 446 In Python, in a function, a parameter with a *default value* like `lastname` in:447 61 unmodified lines509 ...and it will give you auto completion in your terminal when you hit <kbd>TAB</kbd> for all your code.510 511 So you can use it to have auto completion for your own scripts as you continue with the tutorial.512 489-!!! tip490- Your CLI application built with **Typer** won't need the `typer` command to have auto completion once you create a Python package.513+/// tip514 515+Your CLI application built with **Typer** won't need the `typer` command to have auto completion once you create a Python package.516+492- But for short scripts and for learning, before creating a Python package, it might be useful.517+But for short scripts and for learning, before creating a Python package, it might be useful.518+519+///modified:docs/tutorial/index.md+12 added-9 removed
73 unmodified lines74 </div>75 76 By default, `typer` comes with `rich` and `shellingham`.77 78-!!! note79- If you are an advanced user and want to opt out of these default extra dependencies, you can instead install `typer-slim`.78+/// note79 81- ```bash82- pip install typer83- ```80+If you are an advanced user and want to opt out of these default extra dependencies, you can instead install `typer-slim`.81 85- ...includes the same optional dependencies as:82+```bash83+pip install typer84+```85 86+...includes the same optional dependencies as:87+87- ```bash88- pip install "typer-slim[standard]"89- ```88+```bash89+pip install "typer-slim[standard]"90+```91+92+///modified:docs/tutorial/launch.md+6 added-3 removed
26 unmodified lines27 ```Python hl_lines="17"28 {!../docs_src/launch/tutorial002.py!}29 ```30 31-!!! tip32- The rest of the code in this example is just making sure the app directory exists and creating the config file.31+/// tip32 34- But the most important part is the `typer.launch(config_file_str, locate=True)` with the argument `locate=True`.33+The rest of the code in this example is just making sure the app directory exists and creating the config file.34 35+But the most important part is the `typer.launch(config_file_str, locate=True)` with the argument `locate=True`.36+37+///38+39 Check it:40 41 <div class="termy">42 9 unmodified linesmodified:docs/tutorial/options-autocompletion.md+224 added-125 removed
12 unmodified lines13 To check it quickly without creating a new Python package, use the `typer` command.14 15 Then let's create small example program:16 17-=== "Python 3.7+"17+//// tab | Python 3.7+18 19- ```Python20- {!> ../docs_src/options_autocompletion/tutorial001_an.py!}21- ```19+```Python20+{!> ../docs_src/options_autocompletion/tutorial001_an.py!}21+```22 23-=== "Python 3.7+ non-Annotated"23+////24 25- !!! tip26- Prefer to use the `Annotated` version if possible.25+//// tab | Python 3.7+ non-Annotated26 28- ```Python29- {!> ../docs_src/options_autocompletion/tutorial001.py!}30- ```27+/// tip28 29+Prefer to use the `Annotated` version if possible.30+31+///32+33+```Python34+{!> ../docs_src/options_autocompletion/tutorial001.py!}35+```36+37+////38+39 And let's try it with the `typer` command to get completion:40 41 <div class="termy">42 24 unmodified lines67 Right now we get completion for the *CLI option* names, but not for the values.68 69 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}:70 64-=== "Python 3.7+"71+//// tab | Python 3.7+72 66- ```Python hl_lines="5-6 15"67- {!> ../docs_src/options_autocompletion/tutorial002_an.py!}68- ```73+```Python hl_lines="5-6 15"74+{!> ../docs_src/options_autocompletion/tutorial002_an.py!}75+```76 70-=== "Python 3.7+ non-Annotated"77+////78 72- !!! tip73- Prefer to use the `Annotated` version if possible.79+//// tab | Python 3.7+ non-Annotated80 75- ```Python hl_lines="4-5 14"76- {!> ../docs_src/options_autocompletion/tutorial002.py!}77- ```81+/// tip82 83+Prefer to use the `Annotated` version if possible.84+85+///86+87+```Python hl_lines="4-5 14"88+{!> ../docs_src/options_autocompletion/tutorial002.py!}89+```90+91+////92+93 We return a `list` of strings from the `complete_name()` function.94 95 And then we get those values when using completion:96 19 unmodified lines116 Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.117 118 Then we can check and return only the values that start with the incomplete value from the command line:119 106-=== "Python 3.7+"120+//// tab | Python 3.7+121 108- ```Python hl_lines="7-12"109- {!> ../docs_src/options_autocompletion/tutorial003_an.py!}110- ```122+```Python hl_lines="7-12"123+{!> ../docs_src/options_autocompletion/tutorial003_an.py!}124+```125 112-=== "Python 3.7+ non-Annotated"126+////127 114- !!! tip115- Prefer to use the `Annotated` version if possible.128+//// tab | Python 3.7+ non-Annotated129 117- ```Python hl_lines="6-11"118- {!> ../docs_src/options_autocompletion/tutorial003.py!}119- ```130+/// tip131 132+Prefer to use the `Annotated` version if possible.133+134+///135+136+```Python hl_lines="6-11"137+{!> ../docs_src/options_autocompletion/tutorial003.py!}138+```139+140+////141+142 Now let's try it:143 144 <div class="termy">145 7 unmodified lines153 </div>154 155 Now we are only returning the valid values, that start with `Ca`, we are no longer returning `Sebastian` as a completion option.156 136-!!! tip137- You have to declare the incomplete value of type `str` and that's what you will receive in the function.157+/// tip158 139- No matter if the actual value will be an `int`, or something else, when doing completion, you will only get a `str` as the incomplete value.159+You have to declare the incomplete value of type `str` and that's what you will receive in the function.160 141- And the same way, you can only return `str`, not `int`, etc.161+No matter if the actual value will be an `int`, or something else, when doing completion, you will only get a `str` as the incomplete value.162 163+And the same way, you can only return `str`, not `int`, etc.164+165+///166+167 ## Add help to completions168 169 Right now we are returning a `list` of `str`.170 4 unmodified lines175 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.176 177 So, in the end, we return a `list` of `tuples` of `str`:178 155-=== "Python 3.7+"179+//// tab | Python 3.7+180 157- ```Python hl_lines="4-8 11-17"158- {!> ../docs_src/options_autocompletion/tutorial004_an.py!}159- ```181+```Python hl_lines="4-8 11-17"182+{!> ../docs_src/options_autocompletion/tutorial004_an.py!}183+```184 161-=== "Python 3.7+ non-Annotated"185+////186 163- !!! tip164- Prefer to use the `Annotated` version if possible.187+//// tab | Python 3.7+ non-Annotated188 166- ```Python hl_lines="3-7 10-16"167- {!> ../docs_src/options_autocompletion/tutorial004.py!}168- ```189+/// tip190 170-!!! tip171- If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.191+Prefer to use the `Annotated` version if possible.192 173- Click checks specifically for a `tuple` when extracting the help text.193+///194 175- So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.195+```Python hl_lines="3-7 10-16"196+{!> ../docs_src/options_autocompletion/tutorial004.py!}197+```198 177-!!! info178- The help text will be visible in Zsh, Fish, and PowerShell.199+////200 180- Bash doesn't support showing the help text, but completion will still work the same.201+/// tip202 203+If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.204+205+Click checks specifically for a `tuple` when extracting the help text.206+207+So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.208+209+///210+211+/// info212+213+The help text will be visible in Zsh, Fish, and PowerShell.214+215+Bash doesn't support showing the help text, but completion will still work the same.216+217+///218+219 If you have a shell like Zsh, it would look like:220 221 <div class="termy">222 13 unmodified lines236 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.237 238 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:239 203-=== "Python 3.7+"240+//// tab | Python 3.7+241 205- ```Python hl_lines="11-14"206- {!> ../docs_src/options_autocompletion/tutorial005_an.py!}207- ```242+```Python hl_lines="11-14"243+{!> ../docs_src/options_autocompletion/tutorial005_an.py!}244+```245 209-=== "Python 3.7+ non-Annotated"246+////247 211- !!! tip212- Prefer to use the `Annotated` version if possible.248+//// tab | Python 3.7+ non-Annotated249 214- ```Python hl_lines="10-13"215- {!> ../docs_src/options_autocompletion/tutorial005.py!}216- ```250+/// tip251 252+Prefer to use the `Annotated` version if possible.253+254+///255+256+```Python hl_lines="10-13"257+{!> ../docs_src/options_autocompletion/tutorial005.py!}258+```259+260+////261+262 That simplifies our code a bit and works the same.263 220-!!! tip221- If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.264+/// tip265 223- In the end, that's just to save us a couple of lines of code.266+If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.267 225-!!! info226- The function can use `yield`, so it doesn't have to return strictly a `list`, it just has to be <a href="https://docs.python.org/3.8/glossary.html#term-iterable" class="external-link" target="_blank">iterable</a>.268+In the end, that's just to save us a couple of lines of code.269 228- But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).270+///271 272+/// info273+274+The function can use `yield`, so it doesn't have to return strictly a `list`, it just has to be <a href="https://docs.python.org/3.8/glossary.html#term-iterable" class="external-link" target="_blank">iterable</a>.275+276+But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).277+278+///279+280 ## Access other *CLI parameters* with the Context281 282 Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.283 284 So, we will allow multiple `--name` *CLI options*.285 236-!!! tip237- You will learn more about *CLI parameters* with multiple values later in the tutorial.286+/// tip287 239- So, for now, take this as a sneak peek 😉.288+You will learn more about *CLI parameters* with multiple values later in the tutorial.289 290+So, for now, take this as a sneak peek 😉.291+292+///293+294 For this we use a `List` of `str`:295 243-=== "Python 3.7+"296+//// tab | Python 3.7+297 245- ```Python hl_lines="9-14"246- {!> ../docs_src/options_autocompletion/tutorial006_an.py!}247- ```298+```Python hl_lines="9-14"299+{!> ../docs_src/options_autocompletion/tutorial006_an.py!}300+```301 249-=== "Python 3.7+ non-Annotated"302+////303 251- !!! tip252- Prefer to use the `Annotated` version if possible.304+//// tab | Python 3.7+ non-Annotated305 254- ```Python hl_lines="8-11"255- {!> ../docs_src/options_autocompletion/tutorial006.py!}256- ```306+/// tip307 308+Prefer to use the `Annotated` version if possible.309+310+///311+312+```Python hl_lines="8-11"313+{!> ../docs_src/options_autocompletion/tutorial006.py!}314+```315+316+////317+318 And then we can use it like:319 320 <div class="termy">321 15 unmodified lines337 But you can access the context by declaring a function parameter of type `typer.Context`.338 339 And from that context you can get the current values for each parameter.340 281-=== "Python 3.7+"341+//// tab | Python 3.7+342 283- ```Python hl_lines="13-14 16"284- {!> ../docs_src/options_autocompletion/tutorial007_an.py!}285- ```343+```Python hl_lines="13-14 16"344+{!> ../docs_src/options_autocompletion/tutorial007_an.py!}345+```346 287-=== "Python 3.7+ non-Annotated"347+////348 289- !!! tip290- Prefer to use the `Annotated` version if possible.349+//// tab | Python 3.7+ non-Annotated350 292- ```Python hl_lines="12-13 15"293- {!> ../docs_src/options_autocompletion/tutorial007.py!}294- ```351+/// tip352 353+Prefer to use the `Annotated` version if possible.354+355+///356+357+```Python hl_lines="12-13 15"358+{!> ../docs_src/options_autocompletion/tutorial007.py!}359+```360+361+////362+363 We are getting the `names` already provided with `--name` in the command line before this completion was triggered.364 365 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.366 28 unmodified lines395 ```396 397 </div>398 332-!!! tip333- It's quite possible that if there's only one option left, your shell will complete it right away instead of showing the option with the help text, to save you more typing.399+/// tip400 401+It's quite possible that if there's only one option left, your shell will complete it right away instead of showing the option with the help text, to save you more typing.402+403+///404+405 ## Getting the raw *CLI parameters*406 407 You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.408 409 For example, something like `["typer", "main.py", "run", "--name"]`.410 341-!!! tip342- This would be for advanced scenarios, in most use cases you would be better off using the context.411+/// tip412 344- But it's still possible if you need it.413+This would be for advanced scenarios, in most use cases you would be better off using the context.414 415+But it's still possible if you need it.416+417+///418+419 As a simple example, let's show it on the screen before completion.420 421 Because completion is based on the output printed by your program (handled internally by **Typer**), during completion we can't just print something else as we normally do.422 423 ### Printing to "standard error"424 352-!!! tip353- If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](./printing.md#standard-output-and-standard-error){.internal-link target=_blank}.425+/// tip426 427+If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](./printing.md#standard-output-and-standard-error){.internal-link target=_blank}.428+429+///430+431 The completion system only reads from "standard output", so, printing to "standard error" won't break completion. 🚀432 433 You can print to "standard error" with a **Rich** `Console(stderr=True)`.434 435 Using `stderr=True` tells **Rich** that the output should be shown in "standard error".436 361-=== "Python 3.7+"437+//// tab | Python 3.7+438 363- ```Python hl_lines="13 16-17"364- {!> ../docs_src/options_autocompletion/tutorial008_an.py!}365- ```439+```Python hl_lines="13 16-17"440+{!> ../docs_src/options_autocompletion/tutorial008_an.py!}441+```442 367-=== "Python 3.7+ non-Annotated"443+////444 369- !!! tip370- Prefer to use the `Annotated` version if possible.445+//// tab | Python 3.7+ non-Annotated446 372- ```Python hl_lines="12 15-16"373- {!> ../docs_src/options_autocompletion/tutorial008.py!}374- ```447+/// tip448 376-!!! info377- 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.449+Prefer to use the `Annotated` version if possible.450 451+///452+453+```Python hl_lines="12 15-16"454+{!> ../docs_src/options_autocompletion/tutorial008.py!}455+```456+457+////458+459+/// info460+461+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.462+463+///464+465 We get all the *CLI parameters* as a raw `list` of `str` by declaring a parameter with type `List[str]`, here it's named `args`.466 381-!!! tip382- Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.467+/// tip468 384- But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.469+Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.470 471+But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.472+473+///474+475 And then we just print it to "standard error".476 477 <div class="termy">478 10 unmodified lines489 ```490 491 </div>492 404-!!! tip405- This is a very simple (and quite useless) example, just so you know how it works and that you can use it.493+/// tip494 407- But it's probably useful only in very advanced use cases.495+This is a very simple (and quite useless) example, just so you know how it works and that you can use it.496 497+But it's probably useful only in very advanced use cases.498+499+///500+501 ## Getting the Context and the raw *CLI parameters*502 503 Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:504 413-=== "Python 3.7+"505+//// tab | Python 3.7+506 415- ```Python hl_lines="16"416- {!> ../docs_src/options_autocompletion/tutorial009_an.py!}417- ```507+```Python hl_lines="16"508+{!> ../docs_src/options_autocompletion/tutorial009_an.py!}509+```510 419-=== "Python 3.7+ non-Annotated"511+////512 421- !!! tip422- Prefer to use the `Annotated` version if possible.513+//// tab | Python 3.7+ non-Annotated514 424- ```Python hl_lines="15"425- {!> ../docs_src/options_autocompletion/tutorial009.py!}426- ```515+/// tip516 517+Prefer to use the `Annotated` version if possible.518+519+///520+521+```Python hl_lines="15"522+{!> ../docs_src/options_autocompletion/tutorial009.py!}523+```524+525+////526+527 Check it:528 529 <div class="termy">530 34 unmodified linesmodified:docs/tutorial/package.md+46 added-19 removed
8 unmodified lines9 You might even have your favorite already.10 11 Here's a very opinionated, short guide, showing one of the alternative ways of creating a Python package with a **Typer** app, from scratch.12 13-!!! tip14- If you already have a favorite way of creating Python packages, feel free to skip this.13+/// tip14 15+If you already have a favorite way of creating Python packages, feel free to skip this.16+17+///18+19 ## Prerequisites20 21 For this guide we'll use <a href="https://python-poetry.org/" class="external-link" target="_blank">Poetry</a>.22 120 unmodified lines143 """144 typer.echo("Loading portal gun")145 ```146 144-!!! tip145- As we are creating an installable Python package, there's no need to add a section with `if __name__ == "__main__":`.147+/// tip148 149+As we are creating an installable Python package, there's no need to add a section with `if __name__ == "__main__":`.150+151+///152+153 ## Modify the README154 155 Let's change the README to have something like:156 167 unmodified lines324 ```325 326 </div>327 322-!!! warning323- The `--user` is important, that ensures you install it in your user's directory and not in the global system.328+/// warning329 325- If you installed it in the global system (e.g. with `sudo`) you could install a version of a library (e.g. a sub-dependency) that is incompatible with your system.330+The `--user` is important, that ensures you install it in your user's directory and not in the global system.331 327-!!! tip328- Bonus points if you use <a href="https://github.com/pipxproject/pipx" class="external-link" target="_blank">`pipx`</a> to install it while keeping an isolated environment for your Python CLI programs 🚀332+If you installed it in the global system (e.g. with `sudo`) you could install a version of a library (e.g. a sub-dependency) that is incompatible with your system.333 334+///335+336+/// tip337+338+Bonus points if you use <a href="https://github.com/pipxproject/pipx" class="external-link" target="_blank">`pipx`</a> to install it while keeping an isolated environment for your Python CLI programs 🚀339+340+///341+342 Now you have your CLI program installed. And you can use it freely:343 344 <div class="termy">345 18 unmodified lines364 ```365 366 </div>367 356-!!! tip357- If you want to remove completion you can just delete the added line in that file.368+/// tip369 370+If you want to remove completion you can just delete the added line in that file.371+372+///373+374 And after you restart the terminal you will get completion for your new CLI program:375 376 <div class="termy">377 34 unmodified lines412 Here we pass `pip` as the value for `-m`, so, Python will execute the module `pip` as if it was a script. And then it will pass the rest of the *CLI Parameters* (`install fastapi`) to it.413 414 These two are more or less equivalent, the `install fastapi` will be passed to `pip`.415 401-!!! tip402- In the case of `pip`, in many occasions it's actually recommended that you run it with `python -m`, because if you create a virtual environment with its own `python`, that will ensure that you use the `pip` from *that* environment.416+/// tip417 418+In the case of `pip`, in many occasions it's actually recommended that you run it with `python -m`, because if you create a virtual environment with its own `python`, that will ensure that you use the `pip` from *that* environment.419+420+///421+422 ### Add a `__main__.py`423 424 You can support that same style of calling the package/module for your own package, simply by adding a file `__main__.py`.425 47 unmodified lines473 ```474 475 </div>476 459-!!! tip460- Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.477+/// tip478 479+Notice that you have to pass the importable version of the package name, so `rick_portal_gun` instead of `rick-portal-gun`.480+481+///482+483 That works! 🚀 Sort of... 🤔484 485 See the `__main__.py` in the help instead of `rick-portal-gun`? We'll fix that next.486 14 unmodified lines501 from .main import app502 app(prog_name="rick-portal-gun")503 ```504 484-!!! tip485- You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.505+/// tip506 507+You can pass all the arguments and keyword arguments you could pass to a Click application, including `prog_name`.508+509+///510+511 <div class="termy">512 513 ```console514 $ python -m rick_portal_gun172 unmodified lines687 You just have to pass it the module to import (`rick_portal_gun.main`) and it will detect the `typer.Typer` app automatically.688 689 By specifying the `--name` of the program it will be able to use it while generating the docs.690 667-!!! tip668- If you installed `typer-slim` and don't have the `typer` command, you can use `python -m typer` instead.691+/// tip692 693+If you installed `typer-slim` and don't have the `typer` command, you can use `python -m typer` instead.694+695+///696+697 ### Publish a new version with the docs698 699 Now you can publish a new version with the updated docs.700 73 unmodified linesmodified:docs/tutorial/printing.md+50 added-23 removed
134 unmodified lines135 And there's another "**virtual file**" called "**standard error**" that is normally only used for errors.136 137 But we can also "print" to "standard error". And both are shown on the terminal to the users.138 139-!!! info140- If you use PowerShell it's quite possible that what you print to "standard error" won't be shown in the terminal.139+/// info140 142- In PowerShell, to see "standard error" you would have to check the variable `$Error`.141+If you use PowerShell it's quite possible that what you print to "standard error" won't be shown in the terminal.142 144- But it will work normally in Bash, Zsh, and Fish.143+In PowerShell, to see "standard error" you would have to check the variable `$Error`.144 145+But it will work normally in Bash, Zsh, and Fish.146+147+///148+149 ### Printing to "standard error"150 151 You can print to "standard error" creating a Rich `Console` with `stderr=True`.152 150-!!! tip151- `stderr` is short for "standard error".153+/// tip154 155+`stderr` is short for "standard error".156+157+///158+159 Using `stderr=True` tells **Rich** that the output should be shown in "standard error".160 161 ```Python hl_lines="4 8"162 {!../docs_src/printing/tutorial004.py!}24 unmodified lines187 But understanding that will come handy in the future, for example for autocompletion and testing.188 189 ## Typer Echo190 185-!!! warning186- In most of the cases, for displaying advanced information, it is recommended to use <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a>.191+/// warning192 188- You can probably skip the rest of this section. 🎉😎193+In most of the cases, for displaying advanced information, it is recommended to use <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a>.194 195+You can probably skip the rest of this section. 🎉😎196+197+///198+199 **Typer** also has a small utility `typer.echo()` to print information on the screen, it comes directly from Click. But normally you shouldn't need it.200 201 For the simplest cases, you can use the standard Python `print()`.202 8 unmodified lines211 If you have some `bytes` objects, you would probably want to decode them intentionally and directly before trying to print them.212 213 And if you want to print data with colors and other features, you are much better off with the more advanced tools in **Rich**.214 206-!!! info207- `typer.echo()` comes directly from Click, you can read more about it in <a href="https://click.palletsprojects.com/en/7.x/quickstart/#echoing" class="external-link" target="_blank">Click's docs</a>.215+/// info216 217+`typer.echo()` comes directly from Click, you can read more about it in <a href="https://click.palletsprojects.com/en/7.x/quickstart/#echoing" class="external-link" target="_blank">Click's docs</a>.218+219+///220+221 ### Color222 211-!!! note "Technical Details"212- The way color works in terminals is by using some codes (ANSI escape sequences) as part of the text.223+/// note | Technical Details224 214- So, a colored text is still just a `str`.225+The way color works in terminals is by using some codes (ANSI escape sequences) as part of the text.226 216-!!! tip217- Again, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎227+So, a colored text is still just a `str`.228 229+///230+231+/// tip232+233+Again, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎234+235+///236+237 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()`:238 239 ```Python hl_lines="7 9"240 {!../docs_src/printing/tutorial005.py!}241 ```242 225-!!! tip226- 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"`.243+/// tip244 228- But **Typer** provides them all as variables like `typer.colors.GREEN` just so you can use autocompletion while selecting them.245+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"`.246 247+But **Typer** provides them all as variables like `typer.colors.GREEN` just so you can use autocompletion while selecting them.248+249+///250+251 Check it:252 253 <div class="use-termynal" data-termynal>254 <span data-ty="input">python main.py</span>12 unmodified lines267 * `blink`: enable or disable blinking.268 * `reverse`: enable or disable inverse rendering (foreground becomes background and the other way round).269 * `reset`: by default a reset-all code is added at the end of the string which means that styles do not carry over. This can be disabled to compose styles.270 250-!!! info251- You can read more about it in <a href="https://click.palletsprojects.com/en/7.x/api/#click.style" class="external-link" target="_blank">Click's docs about `style()`</a>271+/// info272 273+You can read more about it in <a href="https://click.palletsprojects.com/en/7.x/api/#click.style" class="external-link" target="_blank">Click's docs about `style()`</a>274+275+///276+277 ### `typer.secho()` - style and print278 255-!!! tip256- In case you didn't see above, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎279+/// tip280 281+In case you didn't see above, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎282+283+///284+285 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()`:286 287 ```Python hl_lines="5"288 {!../docs_src/printing/tutorial006.py!}8 unmodified linesmodified:docs/tutorial/progressbar.md+35 added-15 removed
69 unmodified lines70 ## Typer `progressbar`71 72 If you can, you should use **Rich** as explained above, it has more features, it's more advanced, and can display information more beautifully. ✨73 74-!!! tip75- If you can use Rich, use the information above, the Rich docs, and skip the rest of this page. 😎74+/// tip75 76+If you can use Rich, use the information above, the Rich docs, and skip the rest of this page. 😎77+78+///79+80 But if you can't use Rich, Typer (actually Click) comes with a simple utility to show progress bars.81 79-!!! info80- `typer.progressbar()` comes directly from Click, you can read more about it in <a href="https://click.palletsprojects.com/en/8.1.x/utils/#showing-progress-bars" class="external-link" target="_blank">Click's docs</a>.82+/// info83 84+`typer.progressbar()` comes directly from Click, you can read more about it in <a href="https://click.palletsprojects.com/en/8.1.x/utils/#showing-progress-bars" class="external-link" target="_blank">Click's docs</a>.85 86+///87+88 ### Use `typer.progressbar`89 85-!!! tip86- Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎90+/// tip91 92+Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎93+94+///95+96 You can use `typer.progressbar()` with a `with` statement, as in:97 98 ```Python99 with typer.progressbar(something) as progress:26 unmodified lines126 for user in progress:127 typer.echo(user)128 ```129 122-!!! tip123- Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement.130+/// tip131 125-!!! info126- This is mostly useful for operations that take some time.132+Notice that there are 2 levels of code blocks. One for the `with` statement and one for the `for` statement.133 128- In the example above we are faking it with `time.sleep()`.134+///135 136+/// info137+138+This is mostly useful for operations that take some time.139+140+In the example above we are faking it with `time.sleep()`.141+142+///143+144 Check it:145 146 <div class="termy">147 8 unmodified lines156 </div>157 158 ### Setting a Progress Bar `length`159 146-!!! tip147- Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎160+/// tip161 162+Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎163+164+///165+166 The progress bar is generated from the length of the iterable (e.g. the list of users).167 168 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()`.169 38 unmodified lines208 would print each of the "user IDs" (here it's just the numbers from `0` to `99`).209 210 ### Add a `label`211 195-!!! tip196- Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎212+/// tip213 214+Remember, you are much better off using <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a> for this. 😎215+216+///217+218 You can also set a `label`:219 220 ```Python hl_lines="8"221 {!../docs_src/progressbar/tutorial005.py!}27 unmodified linesmodified:docs/tutorial/terminating.md+12 added-6 removed
38 unmodified lines39 ```40 41 </div>42 43-!!! tip44- Even though you are raising an exception, it doesn't necessarily mean there's an error.43+/// tip44 46- This is done with an exception because it works as an "error" and stops all execution.45+Even though you are raising an exception, it doesn't necessarily mean there's an error.46 48- But then **Typer** (actually Click) catches it and just terminates the program normally.47+This is done with an exception because it works as an "error" and stops all execution.48 49+But then **Typer** (actually Click) catches it and just terminates the program normally.50+51+///52+53 ## Exit with an error54 55 `typer.Exit()` takes an optional `code` parameter. By default, `code` is `0`, meaning there was no error.56 31 unmodified lines88 ```89 90 </div>91 89-!!! tip90- The error code might be used by other programs (for example a Bash script) that execute your CLI program.92+/// tip93 94+The error code might be used by other programs (for example a Bash script) that execute your CLI program.95+96+///97+98 ## Abort99 100 There's a special exception that you can use to "abort" a program.101 23 unmodified linesmodified:docs/tutorial/testing.md+42 added-20 removed
35 unmodified lines36 ```Python hl_lines="1 5"37 {!../docs_src/testing/app01/test_main.py!}38 ```39 40-!!! tip41- 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.40+/// tip41 42+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.43+44+///45+46 ### Call the app47 48 Then create a function `test_app()`.49 6 unmodified lines56 ```Python hl_lines="8 9"57 {!../docs_src/testing/app01/test_main.py!}58 ```59 57-!!! tip58- The name of the function has to start with `test_`, that way pytest can detect it and use it automatically.60+/// tip61 62+The name of the function has to start with `test_`, that way pytest can detect it and use it automatically.63+64+///65+66 ### Check the result67 68 Then, inside of the test function, add `assert` statements to ensure that everything in the result of the call is as it should be.69 4 unmodified lines74 Here we are checking that the exit code is 0, as it is for programs that exit without errors.75 76 Then we check that the text printed to "standard output" contains the text that our CLI program prints.77 72-!!! tip73- You could also check `result.stderr` for "standard error" independently from "standard output" if your `CliRunner` instance is created with the `mix_stderr=False` argument.78+/// tip79 75-!!! info76- If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.80+You could also check `result.stderr` for "standard error" independently from "standard output" if your `CliRunner` instance is created with the `mix_stderr=False` argument.81 82+///83+84+/// info85+86+If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.87+88+///89+90 ### Call `pytest`91 92 Then you can call `pytest` in your directory and it will run your tests:93 20 unmodified lines114 ## Testing input115 116 If you have a CLI with prompts, like:117 106-=== "Python 3.7+"118+//// tab | Python 3.7+119 108- ```Python hl_lines="8"109- {!> ../docs_src/testing/app02_an/main.py!}110- ```120+```Python hl_lines="8"121+{!> ../docs_src/testing/app02_an/main.py!}122+```123 112-=== "Python 3.7+ non-Annotated"124+////125 114- !!! tip115- Prefer to use the `Annotated` version if possible.126+//// tab | Python 3.7+ non-Annotated127 117- ```Python hl_lines="7"118- {!> ../docs_src/testing/app02/main.py!}119- ```128+/// tip129 130+Prefer to use the `Annotated` version if possible.131+132+///133+134+```Python hl_lines="7"135+{!> ../docs_src/testing/app02/main.py!}136+```137+138+////139+140 That you would use like:141 142 <div class="termy">143 10 unmodified lines154 You can test the input typed in the terminal using `input="camila@example.com\n"`.155 156 This is because what you type in the terminal goes to "**standard input**" and is handled by the operating system as if it was a "virtual file".157 139-!!! info140- If you need a refresher about what is "standard output", "standard error", and "standard input" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.158+/// info159 160+If you need a refresher about what is "standard output", "standard error", and "standard input" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](printing.md#standard-output-and-standard-error){.internal-link target=_blank}.161+162+///163+164 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"`.165 166 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":167 79 unmodified linesmodified:docs/tutorial/typer-command.md+8 added-5 removed
259 unmodified lines260 ```261 262 </div>263 264-!!! tip265- If you installed only `typer-slim` and you don't have the `typer` command, you can still generate docs with:264+/// tip265 267- ```console268- $ python -m typer some_script.py utils docs269- ```266+If you installed only `typer-slim` and you don't have the `typer` command, you can still generate docs with:267 268+```console269+$ python -m typer some_script.py utils docs270+```271+272+///273+274 **Options**:275 276 * `--name TEXT`: The name of the CLI program to use in docs.277 * `--output FILE`: An output file to write docs to, like README.md.104 unmodified linesmodified:docs/tutorial/using-click.md+21 added-9 removed
1-!!! warning2- This is a more advanced topic, if you are starting with **Typer**, feel free to skip it.1+/// warning2 4- It will be mostly useful for people that already work with Click and have questions around it.3+This is a more advanced topic, if you are starting with **Typer**, feel free to skip it.4 5+It will be mostly useful for people that already work with Click and have questions around it.6+7+///8+9 **Typer** is powered by <a href="https://click.palletsprojects.com" class="external-link" target="_blank">Click</a>. It does all the work underneath.10 11 Here is some more information related to using both together.12 36 unmodified lines49 ```50 51 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.52 50-!!! tip51- The original `cli` function would be the equivalent of a [Typer Callback](./commands/callback.md){.internal-link target=_blank}.53+/// tip54 55+The original `cli` function would be the equivalent of a [Typer Callback](./commands/callback.md){.internal-link target=_blank}.56+57+///58+59 Then the `cli` variable, that now is a `Group` object, is used to add sub-commands.60 61 ### How **Typer** works62 8 unmodified lines71 * More than one command.72 * A callback.73 * Sub-Typer apps (sub commands).74 69-!!! tip70- If you want to learn more about this check the section [One or Multiple Commands](./commands/one-or-multiple.md){.internal-link target=_blank}.75+/// tip76 77+If you want to learn more about this check the section [One or Multiple Commands](./commands/one-or-multiple.md){.internal-link target=_blank}.78+79+///80+81 ### Combine Click and **Typer**82 83 **Typer** uses an internal function `typer.main.get_command()` to generate a Click `Command` (or `Group`) from a `typer.Typer` object.84 104 unmodified lines189 Most of the functionality provided by decorators in Click has an alternative way of doing it in **Typer**.190 191 For example, to access the context, you can just declare a function parameter of type `typer.Context`.192 184-!!! tip185- You can read more about using the context in the docs: [Commands: Using the Context](commands/context.md){.internal-link target=_blank}193+/// tip194 195+You can read more about using the context in the docs: [Commands: Using the Context](commands/context.md){.internal-link target=_blank}196+197+///198+199 But if you need to use something based on Click decorators, you can always generate a Click object using the methods described above, and use it as you would normally use Click.modified:docs/alternatives.md+32 added-14 removed
12 unmodified lines13 `argparse` is the Python standard library's module to write CLIs.14 15 It provides a better alternative than reading the *CLI Parameters* as a `list` of `str` and parsing everything by hand.16 17-!!! check "Inspired **Typer** to"18- Provide a better development experience than just reading *CLI Parameters* by hand.17+/// check | Inspired **Typer** to18 19+Provide a better development experience than just reading *CLI Parameters* by hand.20+21+///22+23 ### <a href="https://www.hug.rest/" class="external-link" target="_blank">Hug</a>24 25 Hug is a library to create APIs and CLIs, it uses parameters in functions to declare the required data.26 27 It inspired a lot of the ideas in **FastAPI** and **Typer**.28 26-!!! check "Inspired **Typer** to"27- Use function parameters to declare *CLI arguments* and *CLI options* as it simplifies a lot the development experience.29+/// check | Inspired **Typer** to30 31+Use function parameters to declare *CLI arguments* and *CLI options* as it simplifies a lot the development experience.32+33+///34+35 ### <a href="https://plac.readthedocs.io/en/latest/" class="external-link" target="_blank">Plac</a>36 37 Plac is another library to create CLIs using parameters in functions, similar to Hug.38 33-!!! check "Inspired **Typer** to"34- Provide a simple way to use a function as a command line app, without having to create a complete app, with `typer.run(some_function)`.39+/// check | Inspired **Typer** to40 41+Provide a simple way to use a function as a command line app, without having to create a complete app, with `typer.run(some_function)`.42+43+///44+45 ### <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a>46 47 Pydantic is a library to handle data validation using standard modern Python type annotations.48 49 It powers **FastAPI** underneath.50 51 It is not used by **Typer**, but it inspired a lot of the design (through **FastAPI**).52 44-!!! check "Inspired **Typer** to"45- Use standard Python type annotations to declare types instead of library-specific types or classes and use them for data validation and documentation.53+/// check | Inspired **Typer** to54 55+Use standard Python type annotations to declare types instead of library-specific types or classes and use them for data validation and documentation.56+57+///58+59 ### <a href="https://click.palletsprojects.com" class="external-link" target="_blank">Click</a>60 61 Click is one of the most widely used libraries to create CLIs in Python.62 4 unmodified lines67 It uses decorators on top of functions to modify the actual value of those functions, converting them to instances of a specific class. This is a clever trick, but code editors can't provide great support for autocompletion that way.68 69 It was built with some great ideas and design using the features available in the language at the time (Python 2.x).70 59-!!! check "**Typer** uses it for"60- Everything. 🚀71+/// check | **Typer** uses it for72 62- **Typer** mainly adds a layer on top of Click, making the code simpler and easier to use, with autocompletion everywhere, etc, but providing all the powerful features of Click underneath.73+Everything. 🚀74 64- As someone pointed out: <em><a href="https://twitter.com/fishnets88/status/1210126833745838080" class="external-link" target="_blank">"Nice to see it is built on Click but adds the type stuff. Me gusta!"</a></em>75+**Typer** mainly adds a layer on top of Click, making the code simpler and easier to use, with autocompletion everywhere, etc, but providing all the powerful features of Click underneath.76 77+As someone pointed out: <em><a href="https://twitter.com/fishnets88/status/1210126833745838080" class="external-link" target="_blank">"Nice to see it is built on Click but adds the type stuff. Me gusta!"</a></em>78+79+///80+81 ### <a href="https://github.com/click-contrib/click-completion" class="external-link" target="_blank">`click-completion`</a>82 83 `click-completion` is a plug-in for Click. It was created to extend completion support for shells when Click only had support for Bash completion.84 85 Previous versions of **Typer** had deep integrations with `click-completion` and used it as an optional dependency. But now all the completion logic is implemented internally in **Typer** itself, the internal logic was heavily inspired and using some parts of `click-completion`.86 87 And now **Typer** improved it to have new features, tests, some bug fixes (for issues in plain `click-completion` and Click), and better support for shells, including modern versions of PowerShell (e.g. the default versions that come with Windows 10).88 74-!!! check "Inspired **Typer** to"75- Provide auto completion for all the shells.89+/// check | Inspired **Typer** to90 91+Provide auto completion for all the shells.92+93+///94+95 ### <a href="https://fastapi.tiangolo.com/" class="external-link" target="_blank">FastAPI</a>96 97 I created **FastAPI** to provide an easy way to build APIs with autocompletion for everything in the code (and some other <a href="https://fastapi.tiangolo.com/features/" class="external-link" target="_blank">features</a>).98 3 unmodified linesmodified:docs/contributing.md+68 added-51 removed
20 unmodified lines21 ### Activate the environment22 23 Activate the new environment with:24 25-=== "Linux, macOS"25+//// tab | Linux, macOS26 27- <div class="termy">27+<div class="termy">28 29- ```console30- $ source ./env/bin/activate31- ```29+```console30+$ source ./env/bin/activate31+```32 33- </div>33+</div>34 35-=== "Windows PowerShell"35+////36 37- <div class="termy">37+//// tab | Windows PowerShell38 39- ```console40- $ .\env\Scripts\Activate.ps141- ```39+<div class="termy">40 43- </div>41+```console42+$ .\env\Scripts\Activate.ps143+```44 45-=== "Windows Bash"45+</div>46 47- Or if you use Bash for Windows (e.g. <a href="https://gitforwindows.org/" class="external-link" target="_blank">Git Bash</a>):47+////48 49- <div class="termy">49+//// tab | Windows Bash50 51- ```console52- $ source ./env/Scripts/activate53- ```51+Or if you use Bash for Windows (e.g. <a href="https://gitforwindows.org/" class="external-link" target="_blank">Git Bash</a>):52 55- </div>53+<div class="termy">54 55+```console56+$ source ./env/Scripts/activate57+```58+59+</div>60+61+////62+63 To check it worked, use:64 59-=== "Linux, macOS, Windows Bash"65+//// tab | Linux, macOS, Windows Bash66 61- <div class="termy">67+<div class="termy">68 63- ```console64- $ which pip69+```console70+$ which pip71 66- some/directory/typer/env/bin/pip67- ```72+some/directory/typer/env/bin/pip73+```74 69- </div>75+</div>76 71-=== "Windows PowerShell"77+////78 73- <div class="termy">79+//// tab | Windows PowerShell80 75- ```console76- $ Get-Command pip81+<div class="termy">82 78- some/directory/typer/env/bin/pip79- ```83+```console84+$ Get-Command pip85 81- </div>86+some/directory/typer/env/bin/pip87+```88 89+</div>90+91+////92+93 If it shows the `pip` binary at `env/bin/pip` then it worked. 🎉94 85-!!! tip86- Every time you install a new package with `pip` under that environment, activate the environment again.95+/// tip96 88- This makes sure that if you use a terminal program installed by that package (like `flit`), you use the one from your local environment and not any other that could be installed globally.97+Every time you install a new package with `pip` under that environment, activate the environment again.98 99+This makes sure that if you use a terminal program installed by that package (like `flit`), you use the one from your local environment and not any other that could be installed globally.100+101+///102+103 ### Flit104 105 **Typer** uses <a href="https://flit.readthedocs.io/en/latest/index.html" class="external-link" target="_blank">Flit</a> to build, package and publish the project.106 12 unmodified lines119 Now re-activate the environment to make sure you are using the `flit` you just installed (and not a global one).120 121 And now use `flit` to install the development dependencies:122 110-=== "Linux, macOS"123+//// tab | Linux, macOS124 112- <div class="termy">125+<div class="termy">126 114- ```console115- $ flit install --deps develop --symlink127+```console128+$ flit install --deps develop --symlink129 117- ---> 100%118- ```130+---> 100%131+```132 120- </div>133+</div>134 122-=== "Windows"135+////136 124- If you are on Windows, use `--pth-file` instead of `--symlink`:137+//// tab | Windows138 126- <div class="termy">139+If you are on Windows, use `--pth-file` instead of `--symlink`:140 128- ```console129- $ flit install --deps develop --pth-file141+<div class="termy">142 131- ---> 100%132- ```143+```console144+$ flit install --deps develop --pth-file145 134- </div>146+---> 100%147+```148 149+</div>150+151+////152+153 It will install all the dependencies and your local Typer in your local environment.154 155 #### Using your local Typer156 92 unmodified linesmodified:docs/features.md+19 added-13 removed
46 unmodified lines47 * Automatic help options for the main CLI program and all its subcommands.48 * Automatic command and subcommand structure handling (you will see more about subcommands in the Tutorial - User Guide).49 * Automatic completion for the CLI app in all operating systems, in all the shells (Bash, Zsh, Fish, PowerShell), so that the final user of your app can just hit <kbd>TAB</kbd> and get the available options or subcommands. *50 51-!!! note "* Auto completion"52- Auto completion works when you create a package (installable with `pip`). Or when using the `typer` command.51+/// note | * Auto completion52 54- **Typer** uses `shellingham` to auto-detect the current shell when installing completion. If you don't want to include `shellingham`, install `typer-slim`.53+Auto completion works when you create a package (installable with `pip`). Or when using the `typer` command.54 56- **Typer** will automatically create 2 *CLI options*:55+**Typer** uses `shellingham` to auto-detect the current shell when installing completion. If you don't want to include `shellingham`, install `typer-slim`.56 58- * `--install-completion`: Install completion for the current shell.59- * `--show-completion`: Show completion for the current shell, to copy it or customize the installation.57+**Typer** will automatically create 2 *CLI options*:58 61- If you didn't add `shellingham` (if you installed `pip install typer-slim`) those *CLI options* take a value with the name of the shell to install completion for, e.g.:59+* `--install-completion`: Install completion for the current shell.60+* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.61 63- * `--install-completion bash`.64- * `--show-completion powershell`.62+If you didn't add `shellingham` (if you installed `pip install typer-slim`) those *CLI options* take a value with the name of the shell to install completion for, e.g.:63 66- Then you can tell the user to install completion after installing your CLI program and the rest will just work.64+* `--install-completion bash`.65+* `--show-completion powershell`.66 68-!!! tip69- **Typer**'s completion is implemented internally, it uses ideas and components from Click and ideas from `click-completion`, but it doesn't use `click-completion` and re-implements some of the relevant parts of Click.67+Then you can tell the user to install completion after installing your CLI program and the rest will just work.68 71- Then it extends those ideas with features and bug fixes. For example, **Typer** programs also support modern versions of PowerShell (e.g. in Windows 10) among all the other shells.69+///70 71+/// tip72+73+**Typer**'s completion is implemented internally, it uses ideas and components from Click and ideas from `click-completion`, but it doesn't use `click-completion` and re-implements some of the relevant parts of Click.74+75+Then it extends those ideas with features and bug fixes. For example, **Typer** programs also support modern versions of PowerShell (e.g. in Windows 10) among all the other shells.76+77+///78+79 ## The power of Click80 81 <a href="https://click.palletsprojects.com" class="external-link" target="_blank">Click</a> is one of the most popular tools for building CLIs in Python.82 9 unmodified linesmodified:docs/help-typer.md+13 added-7 removed
153 unmodified lines154 * Check and read the code, see if it makes sense, **run it locally** and see if it actually solves the problem.155 156 * Then **comment** saying that you did that, that's how I will know you really checked it.157 158-!!! info159- Unfortunately, I can't simply trust PRs that just have several approvals.158+/// info159 161- Several times it has happened that there are PRs with 3, 5 or more approvals, probably because the description is appealing, but when I check the PRs, they are actually broken, have a bug, or don't solve the problem they claim to solve. 😅160+Unfortunately, I can't simply trust PRs that just have several approvals.161 163- So, it's really important that you actually read and run the code, and let me know in the comments that you did. 🤓162+Several times it has happened that there are PRs with 3, 5 or more approvals, probably because the description is appealing, but when I check the PRs, they are actually broken, have a bug, or don't solve the problem they claim to solve. 😅163 164+So, it's really important that you actually read and run the code, and let me know in the comments that you did. 🤓165+166+///167+168 * If the PR can be simplified in a way, you can ask for that, but there's no need to be too picky, there might be a lot of subjective points of view (and I will have my own as well 🙈), so it's better if you can focus on the fundamental things.169 170 ### Tests171 37 unmodified lines209 ## Join the chat210 211 Join the 👥 <a href="https://discord.gg/VQjSZaeJmf" class="external-link" target="_blank">FastAPI and Friends Discord chat server</a> 👥 and hang out with others in the community. There's a `#typer` channel.212 210-!!! tip211- For questions, ask them in <a href="https://github.com/tiangolo/typer/discussions/new?category=questions" class="external-link" target="_blank">GitHub Discussions</a>, there's a much better chance you will receive help there.213+/// tip214 213- Use the chat only for other general conversations.215+For questions, ask them in <a href="https://github.com/tiangolo/typer/discussions/new?category=questions" class="external-link" target="_blank">GitHub Discussions</a>, there's a much better chance you will receive help there.216 217+Use the chat only for other general conversations.218+219+///220+221 ### Don't use the chat for questions222 223 Have in mind that as chats allow more "free conversation", it's easy to ask questions that are too general and more difficult to answer, so, you might not receive answers.224 23 unmodified linesmodified:mkdocs.yml+25 added-19 removed
113 unmodified lines114 - Development - Contributing: contributing.md115 - Release Notes: release-notes.md116 117 markdown_extensions:118- - toc:119- permalink: true120- - markdown.extensions.codehilite:121- guess_lang: false122- - admonition123- - codehilite124- - extra125- - pymdownx.superfences:126- custom_fences:127- - name: mermaid128- class: mermaid129- format: !!python/name:pymdownx.superfences.fence_code_format ''130- - pymdownx.tabbed:131- alternate_style: true132- - mdx_include:133- base_path: docs134- # Enable while writing docs to simplify highlighting135- # - pymdownx.highlight:136- # linenums: true118+ toc:119+ permalink: true120+ markdown.extensions.attr_list:121+ markdown.extensions.tables:122+ markdown.extensions.md_in_html:123 124+ pymdownx.superfences:125+ custom_fences:126+ - name: mermaid127+ class: mermaid128+ format: !!python/name:pymdownx.superfences.fence_code_format ''129+ pymdownx.betterem:130+ pymdownx.blocks.details:131+ pymdownx.blocks.admonition:132+ types:133+ - note134+ - info135+ - tip136+ - warning137+ - danger138+ - check139+ pymdownx.blocks.tab:140+ alternate_style: True141+ mdx_include:142+ base_path: docs143+144 extra:145 analytics:146 provider: google147 property: G-T78C5GNRXK21 unmodified lines