Skip to content
Myr

README.md

This file

Path

README.md

274 lines · 8.1 KiB · Markdown

Last changed by

:memo: :sparkles: :bug: Commands: docs, examples, tests, and fixSebastián Ramírezbefore adoptioned6701b

History of this file

Take it

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:bug: Fix link in README for GitHubSebastián Ramírezbefore adoptionc820ba0

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:memo: Update READMESebastián Ramírezbefore adoption08d36b1

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 10–17why, 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>

:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112

line 18
18

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 19why, one step back
19---

:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112

line 20
20

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 21why, one step back
21**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>

:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112

line 22
22

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 23–26why, one step back
23**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>
2425---26

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 28why, one step back
28

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 30–33why, 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.

:memo: :sparkles: :bug: Commands: docs, examples, tests, and fixSebastián Ramírezbefore adoptioned6701b

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 35–36why, one step back
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: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

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

line 48why, one step back
48

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 53why, one step back
53```

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

line 54why, one step back
54

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

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

line 80why, one step back
80

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 84why, one step back
84

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

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

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 93why, one step back
93

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 95why, one step back
95

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

line 96why, one step back
96Options:

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 100why, one step back
100

:sparkles: Add docs for CLI Options with testsSebastián Ramírezbefore adoption9fb09d4

line 101why, one step back
101// You get ✨ auto completion ✨ for free, installed with --install-completion

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

line 102why, one step back
102

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

lines 103–105why, one step back
103// Now pass the NAME argument
104$ python main.py Camila105

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 106–107why, one step back
106Hello Camila
107

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 109why, one step back
109```

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

line 110why, one step back
110

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 112–114why, one step back
112
113## Example upgrade114

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 116–118why, one step back
116
117Now let's see one a bit more complex.118

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 120–122why, one step back
120
121Modify the file `main.py`.122

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 124–151why, one step back
124
125```Python hl_lines="3 6 11 20"126import typer127128app = typer.Typer()129130131@app.command()132def hello(name: str):133 typer.echo(f"Hello {name}")134135136@app.command()137def goodbye(name: str, formal: bool = False):138 if formal:139 typer.echo(f"Goodbye Ms. {name}. Have a good day.")140 else:141 typer.echo(f"Bye {name}!")142143144if __name__ == "__main__":145 app()146```147148And that will:149150* Explicitly create a `typer.Typer` app.151 * The previous `typer.run` actually creates one implicitly for you.

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 153–156why, one step back
153* Execute the `app()` itself, as if it was a function (instead of `typer.run`).
154155### Run the upgraded example156

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

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

line 158why, one step back
158

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

lines 159–161why, one step back
159```console
160// Check the --help161$ python main.py --help

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 162–165why, one step back
162
163Usage: main.py [OPTIONS] COMMAND [ARGS]...164165Options:

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 169–173why, one step back
169
170Commands:171 goodbye172 hello173

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 175why, one step back
175

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 177why, one step back
177

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 179–184why, one step back
179
180Usage: main.py hello [OPTIONS] NAME181182Options:183 --help Show this message and exit.184

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 186why, one step back
186

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 188–194why, one step back
188
189Usage: main.py goodbye [OPTIONS] NAME190191Options:192 --formal / --no-formal193 --help Show this message and exit.194

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 196why, one step back
196

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 198why, one step back
198

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 200–202why, one step back
200
201Hello Camila202

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 204why, one step back
204

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 206–208why, one step back
206
207Bye Camila!208

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 210why, one step back
210

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 212–214why, one step back
212
213Goodbye Ms. Camila. Have a good day.214```

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

line 215why, one step back
215

:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 217–239why, one step back
217
218### Recap219220In summary, you declare **once** the types of parameters (*arguments* and *options*) as function parameters.221222You do that with standard modern Python types.223224You don't have to learn a new syntax, the methods or classes of a specific library, etc.225226Just standard **Python 3.6+**.227228For example, for an `int`:229230```Python231total: int232```233234or for a `bool` flag:235236```Python237force: bool238```239

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

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 241–251why, one step back
241
242**You get**: great editor support, including **completion** and **type checks** everywhere.243244**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).245246For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/intro/">Tutorial - User Guide</a>.247248## Optional Dependencies249250Typer uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a> internally. That's the only dependency.251

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

line 253why, one step back
253

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

lines 254–255why, one step back
254* <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.
255 * 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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 256–258why, one step back
256 * 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>.
257* <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.258

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

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

:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 260–267why, one step back
260
261## Other tools and plug-ins262263Click 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.264265For example:266267* <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.

:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d

line 268why, one step back
268 * 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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033

lines 269–270why, one step back
269* <a href="https://pypi.org/project/tabulate/" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or typer.
270* etc... you can re-use many of the great available tools for building CLIs.

:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112

lines 271–274
271
272## License273274This project is licensed under the terms of the MIT license.