Skip to content
Myr

index.md

This file

Path

docs/index.md

297 lines · 9.5 KiB · Markdown

Last changed by

💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e

History of this file

Take it

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 1why, one step back
1<p align="center">

:bug: Fix GitHub imageSebastián Ramírezbefore adoption4792de3

line 2why, one step back
2 <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" alt="Typer"></a>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 3–4why, one step back
3</p>
4<p align="center">

:memo: Update sub-header, short descriptionSebastián Ramírezbefore adoptionbc931c1

line 5why, one step back
5 <em>Typer, build great CLIs. Easy to code. Based on Python type hints.</em>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 6–7why, one step back
6</p>
7<p align="center">

💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e

lines 8–9why, one step back
8<a href="https://github.com/tiangolo/typer/actions?query=workflow%3ATest" target="_blank">
9 <img src="https://github.com/tiangolo/typer/workflows/Test/badge.svg" alt="Test">

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 10why, one step back
10</a>

💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e

lines 11–13why, one step back
11<a href="https://github.com/tiangolo/typer/actions?query=workflow%3APublish" target="_blank">
12 <img src="https://github.com/tiangolo/typer/workflows/Publish/badge.svg" alt="Publish">13</a>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 14why, one step back
14<a href="https://codecov.io/gh/tiangolo/typer" target="_blank">

💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e

line 15why, one step back
15 <img src="https://img.shields.io/codecov/c/github/tiangolo/typer?color=%2334D058" alt="Coverage">

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 16–17why, one step back
16</a>
17<a href="https://pypi.org/project/typer" target="_blank">

💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e

line 18why, one step back
18 <img src="https://img.shields.io/pypi/v/typer?color=%2334D058&label=pypi%20package" alt="Package version">

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 19–29why, one step back
19</a>
20</p>2122---2324**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>2526**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>2728---29

✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a

line 30why, one step back
30Typer is a library for building <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users will **love using** and developers will **love creating**. Based on Python 3.6+ type hints.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 31–34why, one step back
31
32The key features are:3334* **Intuitive to write**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

line 35why, one step back
35* **Easy to use**: It's easy to use for the final users. Automatic help, and automatic completion for all shells.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 36–37why, one step back
36* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
37* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

line 38why, one step back
38* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 39why, one step back
39

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

lines 40–47why, one step back
40## FastAPI of CLIs
4142<a href="https://fastapi.tiangolo.com" target="_blank"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" style="width: 20%;"></a>4344**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling.4546And it's intended to be the FastAPI of CLIs.47

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 48–51why, one step back
48## Requirements
4950Python 3.6+51

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

line 52why, one step back
52**Typer** stands on the shoulders of a giant. Its only internal dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 53–55why, one step back
53
54## Installation55

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 56why, one step back
56<div class="termy">

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 57why, one step back
57

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

lines 58–61why, one step back
58```console
59$ pip install typer60---> 100%61Successfully installed typer

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 62why, one step back
62```

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 63why, one step back
63

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 64why, one step back
64</div>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 65–87why, one step back
65
66## Example6768### The absolute minimum6970* Create a file `main.py` with:7172```Python73import typer747576def main(name: str):77 typer.echo(f"Hello {name}")787980if __name__ == "__main__":81 typer.run(main)82```8384### Run it8586Run your application:87

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 88why, one step back
88<div class="termy">

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 89why, one step back
89

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

lines 90–92why, one step back
90```console
91// Run your application92$ python main.py

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 93why, one step back
93

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 94why, one step back
94// You get a nice error, you are missing NAME

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 95–97why, one step back
95Usage: main.py [OPTIONS] NAME
96Try "main.py --help" for help.97

:sparkles: Add support for Click 7.1.1 (#60)Sebastián Ramírezbefore adoption96b29e9

line 98why, one step back
98Error: Missing argument 'NAME'.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 99why, one step back
99

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

lines 100–101why, one step back
100// You get a --help for free
101$ python main.py --help

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 102why, one step back
102

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 103why, one step back
103Usage: main.py [OPTIONS] NAME

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 104why, one step back
104

✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd

lines 105–107why, one step back
105Arguments:
106 NAME [required]107

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 108why, one step back
108Options:

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

lines 109–111why, one step back
109 --install-completion Install completion for the current shell.
110 --show-completion Show completion for the current shell, to copy it or customize the installation.111 --help Show this message and exit.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 112why, one step back
112

✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a

line 113why, one step back
113// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 114why, one step back
114

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

lines 115–117why, one step back
115// Now pass the NAME argument
116$ python main.py Camila117

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 118–119why, one step back
118Hello Camila
119

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 120why, one step back
120// It works! 🎉

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 121why, one step back
121```

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 122why, one step back
122

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 123why, one step back
123</div>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 124why, one step back
124

✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a

line 125why, one step back
125**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use <a href="https://typer.tiangolo.com/typer-cli/" class="internal-link" target="_blank">Typer CLI</a>.

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

line 126why, one step back
126

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 127–128why, one step back
127## Example upgrade
128

:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3

line 129why, one step back
129This was the simplest example possible.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 130–132why, one step back
130
131Now let's see one a bit more complex.132

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 133why, one step back
133### An example with two subcommands

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 134–136why, one step back
134
135Modify the file `main.py`.136

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 137why, one step back
137Create a `typer.Typer()` app, and create two subcommands with their parameters.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 138–165why, one step back
138
139```Python hl_lines="3 6 11 20"140import typer141142app = typer.Typer()143144145@app.command()146def hello(name: str):147 typer.echo(f"Hello {name}")148149150@app.command()151def goodbye(name: str, formal: bool = False):152 if formal:153 typer.echo(f"Goodbye Ms. {name}. Have a good day.")154 else:155 typer.echo(f"Bye {name}!")156157158if __name__ == "__main__":159 app()160```161162And that will:163164* Explicitly create a `typer.Typer` app.165 * The previous `typer.run` actually creates one implicitly for you.

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 166why, one step back
166* Add two subcommands with `@app.command()`.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 167–170why, one step back
167* Execute the `app()` itself, as if it was a function (instead of `typer.run`).
168169### Run the upgraded example170

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 171why, one step back
171<div class="termy">

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 172why, one step back
172

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

lines 173–175why, one step back
173```console
174// Check the --help175$ python main.py --help

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 176–179why, one step back
176
177Usage: main.py [OPTIONS] COMMAND [ARGS]...178179Options:

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

lines 180–182why, one step back
180 --install-completion Install completion for the current shell.
181 --show-completion Show completion for the current shell, to copy it or customize the installation.182 --help Show this message and exit.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 183–187why, one step back
183
184Commands:185 goodbye186 hello187

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 188why, one step back
188// You have 2 subcommands (the 2 functions): goodbye and hello

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 189why, one step back
189

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 190why, one step back
190// Now get the --help for hello

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 191why, one step back
191

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 192why, one step back
192$ python main.py hello --help

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 193–195why, one step back
193
194Usage: main.py hello [OPTIONS] NAME195

✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd

lines 196–198why, one step back
196Arguments:
197 NAME [required]198

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 199–201why, one step back
199Options:
200 --help Show this message and exit.201

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 202why, one step back
202// And now get the --help for goodbye

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 203why, one step back
203

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 204why, one step back
204$ python main.py goodbye --help

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 205–207why, one step back
205
206Usage: main.py goodbye [OPTIONS] NAME207

✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd

lines 208–210why, one step back
208Arguments:
209 NAME [required]210

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 211why, one step back
211Options:

✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd

line 212why, one step back
212 --formal / --no-formal [default: False]

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 213–214why, one step back
213 --help Show this message and exit.
214

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 215why, one step back
215// Automatic --formal and --no-formal for the bool option 🎉

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 216why, one step back
216

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 217why, one step back
217// And if you use it with the hello command

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 218why, one step back
218

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 219why, one step back
219$ python main.py hello Camila

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 220–222why, one step back
220
221Hello Camila222

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 223why, one step back
223// And with the goodbye command

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 224why, one step back
224

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 225why, one step back
225$ python main.py goodbye Camila

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 226–228why, one step back
226
227Bye Camila!228

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 229why, one step back
229// And with --formal

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 230why, one step back
230

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 231why, one step back
231$ python main.py goodbye --formal Camila

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 232–234why, one step back
232
233Goodbye Ms. Camila. Have a good day.234```

:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4

line 235why, one step back
235

:sparkles: Add termynal for awesome docsSebastián Ramírezbefore adoptionc004fb6

line 236why, one step back
236</div>

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 237–239why, one step back
237
238### Recap239

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

line 240why, one step back
240In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 241–259why, one step back
241
242You do that with standard modern Python types.243244You don't have to learn a new syntax, the methods or classes of a specific library, etc.245246Just standard **Python 3.6+**.247248For example, for an `int`:249250```Python251total: int252```253254or for a `bool` flag:255256```Python257force: bool258```259

:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

line 260why, one step back
260And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 261–263why, one step back
261
262**You get**: great editor support, including **completion** and **type checks** everywhere.263

✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a

line 264why, one step back
264**Your users get**: automatic **`--help`**, **auto-completion** in their terminal (Bash, Zsh, Fish, PowerShell) when they install your package or when using <a href="https://typer.tiangolo.com/typer-cli/" class="internal-link" target="_blank">Typer CLI</a>.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 265why, one step back
265

:memo: Refactor docs with internal links #38Sebastián Ramírezbefore adoption8d6b201

line 266why, one step back
266For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 267–269why, one step back
267
268## Optional Dependencies269

:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0

line 270why, one step back
270Typer uses <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a> internally. That's the only dependency.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 271why, one step back
271

:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3

line 272why, one step back
272But you can also install extras:

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 273why, one step back
273

:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0

line 274why, one step back
274* <a href="https://pypi.org/project/colorama/" class="external-link" target="_blank"><code>colorama</code></a>: and Click will automatically use it to make sure your terminal's colors always work correctly, even in Windows.

:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3

line 275why, one step back
275 * Then you can use any tool you want to output your terminal's colors in all the systems, including the integrated `typer.style()` and `typer.secho()` (provided by Click).

:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0

line 276why, one step back
276 * Or any other tool, e.g. <a href="https://pypi.org/project/wasabi/" class="external-link" target="_blank"><code>wasabi</code></a>, <a href="https://github.com/erikrose/blessings" class="external-link" target="_blank"><code>blessings</code></a>.

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

lines 277–278why, one step back
277* <a href="https://github.com/sarugaku/shellingham" class="external-link" target="_blank"><code>shellingham</code></a>: and Typer will automatically detect the current shell when installing completion.
278 * With `shellingham` you can just use `--install-completion`.

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

line 279why, one step back
279 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

line 280why, one step back
280

:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a

line 281why, one step back
281You can install `typer` with `colorama` and `shellingham` with `pip install typer[all]`.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 282–288why, one step back
282
283## Other tools and plug-ins284285Click has many plug-ins available that you can use. And there are many tools that help with command line applications that you can use as well, even if they are not related to Typer or Click.286287For example:288

:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0

lines 289–290why, one step back
289* <a href="https://github.com/click-contrib/click-spinner" class="external-link" target="_blank"><code>click-spinner</code></a>: to show the user that you are loading data. A Click plug-in.
290 * There are several other Click plug-ins at <a href="https://github.com/click-contrib" class="external-link" target="_blank">click-contrib</a> that you can explore.

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

line 291why, one step back
291* <a href="https://pypi.org/project/tabulate/" class="external-link" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or Typer.

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

line 292why, one step back
292* <a href="https://github.com/tqdm/tqdm" class="external-link" target="_blank"><code>tqdm</code></a>: a fast, extensible progress bar, alternative to Typer's own `typer.progressbar()`.

:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f

lines 293–297why, one step back
293* etc... you can re-use many of the great available tools for building CLIs.
294295## License296297This project is licensed under the terms of the MIT license.