Skip to content
Myr

index.md

This file

Path

docs/index.md

268 lines · 7.6 KiB · Markdown

Last changed by

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

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

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

line 27why, one step back
27Typer is library to build <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

line 28why, one step back
28

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

line 29why, one step back
29**Typer** is <a href="https://fastapi.tiangolo.com" target="_blank">FastAPI</a>'s little sibling. And it's intended to be the FastAPI of CLIs.

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

lines 30–36why, one step back
30
31The key features are:3233* **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.34* **Easy to use**: It's easy to use for the final users. Automatic help commands, and (optional) automatic completion for all shells.35* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.36* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.

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

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

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

lines 38–42why, one step back
38
39## Requirements4041Python 3.6+42

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

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

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

lines 44–46why, one step back
44
45## Installation46

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

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

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

line 48why, one step back
48

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

lines 49–52why, one step back
49```console
50$ pip install typer51---> 100%52Successfully installed typer

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

line 53why, one step back
53```

: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

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

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

lines 56–78why, one step back
56
57## Example5859### The absolute minimum6061* Create a file `main.py` with:6263```Python64import typer656667def main(name: str):68 typer.echo(f"Hello {name}")697071if __name__ == "__main__":72 typer.run(main)73```7475### Run it7677Run your application:78

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

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

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

line 80why, one step back
80

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

lines 81–83why, one step back
81```console
82// Run your application83$ python main.py

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

line 84why, one step back
84

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

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

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

lines 86–90why, one step back
86Usage: main.py [OPTIONS] NAME
87Try "main.py --help" for help.8889Error: Missing argument "NAME".90

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

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

: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
94Usage: main.py [OPTIONS] NAME

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

line 95why, one step back
95

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

lines 96–97why, one step back
96Options:
97 --help Show this message and exit.

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

line 98why, one step back
98

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

lines 99–101why, one step back
99// Now pass the NAME argument
100$ python main.py Camila101

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

lines 102–103why, one step back
102Hello Camila
103

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

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

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

line 105why, one step back
105```

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

line 106why, one step back
106

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

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

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

lines 108–110why, one step back
108
109## Example upgrade110

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

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

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

lines 112–152why, one step back
112
113Now let's see one a bit more complex.114115### An example with two sub-commands116117Modify the file `main.py`.118119Create a `typer.Typer()` app, and create two sub-commands with their parameters.120121```Python hl_lines="3 6 11 20"122import typer123124app = typer.Typer()125126127@app.command()128def hello(name: str):129 typer.echo(f"Hello {name}")130131132@app.command()133def goodbye(name: str, formal: bool = False):134 if formal:135 typer.echo(f"Goodbye Ms. {name}. Have a good day.")136 else:137 typer.echo(f"Bye {name}!")138139140if __name__ == "__main__":141 app()142```143144And that will:145146* Explicitly create a `typer.Typer` app.147 * The previous `typer.run` actually creates one implicitly for you.148* Add two sub-commands with `@app.command()`.149* Execute the `app()` itself, as if it was a function (instead of `typer.run`).150151### Run the upgraded example152

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

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

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

line 154why, one step back
154

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

lines 155–157why, one step back
155```console
156// Check the --help157$ python main.py --help

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

lines 158–167why, one step back
158
159Usage: main.py [OPTIONS] COMMAND [ARGS]...160161Options:162 --help Show this message and exit.163164Commands:165 goodbye166 hello167

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

line 168why, one step back
168// You have 2 sub-commands (the 2 functions): goodbye and hello

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

line 169why, one step back
169

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

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

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

line 171why, one step back
171

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

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

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

lines 173–178why, one step back
173
174Usage: main.py hello [OPTIONS] NAME175176Options:177 --help Show this message and exit.178

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

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

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

line 180why, one step back
180

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

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

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

lines 182–188why, one step back
182
183Usage: main.py goodbye [OPTIONS] NAME184185Options:186 --formal / --no-formal187 --help Show this message and exit.188

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

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

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

line 190why, one step back
190

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

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

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

line 192why, one step back
192

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

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

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

lines 194–196why, one step back
194
195Hello Camila196

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

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

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

line 198why, one step back
198

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

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

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

lines 200–202why, one step back
200
201Bye Camila!202

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

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

: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$ python main.py goodbye --formal Camila

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

lines 206–208why, one step back
206
207Goodbye Ms. Camila. Have a good day.208```

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

line 209why, one step back
209

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

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

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

lines 211–245why, one step back
211
212### Recap213214In summary, you declare **once** the types of parameters (*arguments* and *options*) as function parameters.215216You do that with standard modern Python types.217218You don't have to learn a new syntax, the methods or classes of a specific library, etc.219220Just standard **Python 3.6+**.221222For example, for an `int`:223224```Python225total: int226```227228or for a `bool` flag:229230```Python231force: bool232```233234And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of sub-commands**, add metadata, extra **validation**, etc.235236**You get**: great editor support, including **completion** and **type checks** everywhere.237238**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).239240For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/intro/">Tutorial - User Guide</a>.241242## Optional Dependencies243244Typer uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a> internally. That's the only dependency.245

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

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

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

line 247why, one step back
247

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

lines 248–249why, one step back
248* <a href="https://pypi.org/project/colorama/" 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.
249 * 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).

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

lines 250–252why, one step back
250 * Or any other tool, e.g. <a href="https://pypi.org/project/wasabi/" target="_blank"><code>wasabi</code></a>, <a href="https://github.com/erikrose/blessings" target="_blank"><code>blessings</code></a>.
251* <a href="https://github.com/click-contrib/click-completion" target="_blank"><code>click-completion</code></a>: and Typer will automatically configure it to provide completion for all the shells, including installation commands.252

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

line 253why, one step back
253You can install `typer` with `colorama` and `click-completion` with `pip install typer[all]`.

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

lines 254–261why, one step back
254
255## Other tools and plug-ins256257Click 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.258259For example:260261* <a href="https://github.com/click-contrib/click-spinner" target="_blank"><code>click-spinner</code></a>: to show the user that you are loading data. A Click plug-in.

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

line 262why, one step back
262 * There are several other Click plug-ins at <a href="https://github.com/click-contrib" target="_blank">click-contrib</a> that you can explore.

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

lines 263–268why, one step back
263* <a href="https://pypi.org/project/tabulate/" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or typer.
264* etc... you can re-use many of the great available tools for building CLIs.265266## License267268This project is licensed under the terms of the MIT license.