Skip to content
Myr

index.md

This file

Path

docs/index.md

285 lines · 9.2 KiB · Markdown

Last changed by

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

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">

:art: Fix types and formatSebastián Ramírezbefore adoptionb52fc16

lines 8–9why, one step back
8<a href="https://travis-ci.com/tiangolo/typer" target="_blank">
9 <img src="https://travis-ci.com/tiangolo/typer.svg?branch=master" alt="Build Status">

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

lines 10–26why, one step back
10</a>
11<a href="https://codecov.io/gh/tiangolo/typer" target="_blank">12 <img src="https://codecov.io/gh/tiangolo/typer/branch/master/graph/badge.svg" alt="Coverage">13</a>14<a href="https://pypi.org/project/typer" target="_blank">15 <img src="https://badge.fury.io/py/typer.svg" alt="Package version">16</a>17</p>1819---2021**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>2223**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>2425---26

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

line 27why, one step back
27Typer is 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 28–31why, one step back
28
29The key features are:3031* **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 32why, one step back
32* **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 33–34why, one step back
33* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
34* **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 35why, one step back
35* **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 36why, one step back
36

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

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

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

lines 45–48why, one step back
45## Requirements
4647Python 3.6+48

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

line 49why, one step back
49**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 50–52why, one step back
50
51## Installation52

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

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

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

line 54why, one step back
54

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

lines 55–58why, one step back
55```console
56$ pip install typer57---> 100%58Successfully installed typer

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

line 59why, one step back
59```

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

line 60why, one step back
60

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

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

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

lines 62–84why, one step back
62
63## Example6465### The absolute minimum6667* Create a file `main.py` with:6869```Python70import typer717273def main(name: str):74 typer.echo(f"Hello {name}")757677if __name__ == "__main__":78 typer.run(main)79```8081### Run it8283Run your application:84

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

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

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

line 86why, one step back
86

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

lines 87–89why, one step back
87```console
88// Run your application89$ python main.py

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

line 90why, one step back
90

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

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

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

lines 92–94why, one step back
92Usage: main.py [OPTIONS] NAME
93Try "main.py --help" for help.94

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

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

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

line 96why, one step back
96

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

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

: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

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

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

line 101why, one step back
101

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

line 102why, one step back
102Options:

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

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

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

line 106why, one step back
106

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

line 107why, one step back
107// 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 108why, one step back
108

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

lines 109–111why, one step back
109// Now pass the NAME argument
110$ python main.py Camila111

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

lines 112–113why, one step back
112Hello Camila
113

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

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

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

line 115why, one step back
115```

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

line 116why, one step back
116

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

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

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

line 118why, one step back
118

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

lines 119–120why, one step back
119**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>.
120

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

lines 121–122why, one step back
121## Example upgrade
122

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

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

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

lines 124–126why, one step back
124
125Now let's see one a bit more complex.126

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

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

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

lines 128–130why, one step back
128
129Modify the file `main.py`.130

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

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

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

lines 132–159why, one step back
132
133```Python hl_lines="3 6 11 20"134import typer135136app = typer.Typer()137138139@app.command()140def hello(name: str):141 typer.echo(f"Hello {name}")142143144@app.command()145def goodbye(name: str, formal: bool = False):146 if formal:147 typer.echo(f"Goodbye Ms. {name}. Have a good day.")148 else:149 typer.echo(f"Bye {name}!")150151152if __name__ == "__main__":153 app()154```155156And that will:157158* Explicitly create a `typer.Typer` app.159 * The previous `typer.run` actually creates one implicitly for you.

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

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

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

lines 161–164why, one step back
161* Execute the `app()` itself, as if it was a function (instead of `typer.run`).
162163### Run the upgraded example164

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

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

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

line 166why, one step back
166

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

lines 167–169why, one step back
167```console
168// Check the --help169$ python main.py --help

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

lines 170–173why, one step back
170
171Usage: main.py [OPTIONS] COMMAND [ARGS]...172173Options:

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

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

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

lines 177–181why, one step back
177
178Commands:179 goodbye180 hello181

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

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

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

line 183why, one step back
183

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

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

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

line 185why, one step back
185

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

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

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

lines 187–192why, one step back
187
188Usage: main.py hello [OPTIONS] NAME189190Options:191 --help Show this message and exit.192

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

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

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

line 194why, one step back
194

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

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

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

lines 196–202why, one step back
196
197Usage: main.py goodbye [OPTIONS] NAME198199Options:200 --formal / --no-formal201 --help Show this message and exit.202

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

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

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

line 204why, one step back
204

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

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

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

line 206why, one step back
206

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

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

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

lines 208–210why, one step back
208
209Hello Camila210

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

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

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

line 212why, one step back
212

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

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

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

lines 214–216why, one step back
214
215Bye Camila!216

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

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

: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 goodbye --formal Camila

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

lines 220–222why, one step back
220
221Goodbye Ms. Camila. Have a good day.222```

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

line 223why, one step back
223

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

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

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

lines 225–227why, one step back
225
226### Recap227

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

line 228why, one step back
228In 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 229–247why, one step back
229
230You do that with standard modern Python types.231232You don't have to learn a new syntax, the methods or classes of a specific library, etc.233234Just standard **Python 3.6+**.235236For example, for an `int`:237238```Python239total: int240```241242or for a `bool` flag:243244```Python245force: bool246```247

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

line 248why, one step back
248And 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 249–251why, one step back
249
250**You get**: great editor support, including **completion** and **type checks** everywhere.251

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

line 252why, one step back
252**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 253why, one step back
253

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

line 254why, one step back
254For 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 255–257why, one step back
255
256## Optional Dependencies257

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

line 258why, one step back
258Typer 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 259why, one step back
259

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

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

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

line 261why, one step back
261

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

line 262why, one step back
262* <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 263why, one step back
263 * 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 264why, one step back
264 * 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 265–266why, one step back
265* <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.
266 * 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 267why, one step back
267 * 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 268why, one step back
268

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

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

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

lines 270–276why, one step back
270
271## Other tools and plug-ins272273Click 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.274275For example:276

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

lines 277–278why, one step back
277* <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.
278 * 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 279why, one step back
279* <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 280why, one step back
280* <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 281–285why, one step back
281* etc... you can re-use many of the great available tools for building CLIs.
282283## License284285This project is licensed under the terms of the MIT license.