Skip to content
Myr

README.md

This file

Path

README.md

326 lines · 14.9 KiB · Markdown

Last changed by

👷 Move from pytest-cov to coverage and Codecov to Smokeshow (#483)Sebastián Ramírezbefore adoptionb988f6f

History of this file

Take it

The walk stopped after 200 commits: lines older than e4d6047 point at it.

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 1–7why, one step back
1<p align="center">
2 <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" alt="Typer"></a>3</p>4<p align="center">5 <em>Typer, build great CLIs. Easy to code. Based on Python type hints.</em>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 docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

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>

👷 Move from pytest-cov to coverage and Codecov to Smokeshow (#483)Sebastián Ramírezbefore adoptionb988f6f

lines 14–15why, one step back
14<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/tiangolo/typer" target="_blank">
15 <img src="https://coverage-badge.samuelcolvin.workers.dev/tiangolo/typer.svg" alt="Coverage">

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

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

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 18–28why, one step back
18</a>
19</p>2021---2223**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>2425**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>2627---28

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

line 29why, one step back
29Typer 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 docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

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.

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

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**.

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

line 37why, one step back
37* **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 docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 38why, one step back
38

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 47–50why, one step back
47## Requirements
4849Python 3.6+50

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

line 51why, one step back
51**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 docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 52–57why, one step back
52
53## Installation5455<div class="termy">5657```console

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 58why, one step back
58$ pip install "typer[all]"

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 59–64why, one step back
59---> 100%
60Successfully installed typer61```6263</div>64

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 65–66why, one step back
65**Note**: that will include <a href="https://rich.readthedocs.io/" class="external-link" target="_blank">Rich</a>. Rich is the recommended library to *display* information on the terminal, it is optional, but when installed, it's deeply integrated into **Typer** to display beautiful output.
66

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 67–77why, one step back
67## Example
6869### The absolute minimum7071* Create a file `main.py` with:7273```Python74import typer757677def main(name: str):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 78why, one step back
78 print(f"Hello {name}")

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 79–95why, one step back
79
8081if __name__ == "__main__":82 typer.run(main)83```8485### Run it8687Run your application:8889<div class="termy">9091```console92// Run your application93$ python main.py9495// You get a nice error, you are missing NAME

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 96–100why, one step back
96<font color="#F4BF75">Usage: </font>main.py [OPTIONS] NAME
97<font color="#A5A5A1">Try </font><font color="#44919F">&apos;main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">&apos;</font><font color="#A5A5A1"> for help.</font>98<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>99<font color="#F92672">│</font> Missing argument &apos;NAME&apos;. <font color="#F92672">│</font>100<font color="#F92672">╰───────────────────────────────────────────────────╯</font>

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 101–105why, one step back
101
102103// You get a --help for free104$ python main.py --help105

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 106–122why, one step back
106<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] NAME </b>
107<b> </b>108<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>109<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │110<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>111<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>112<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │113<font color="#A5A5A1">│ for the current │</font>114<font color="#A5A5A1">│ shell. │</font>115<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │116<font color="#A5A5A1">│ the current shell, │</font>117<font color="#A5A5A1">│ to copy it or │</font>118<font color="#A5A5A1">│ customize the │</font>119<font color="#A5A5A1">│ installation. │</font>120<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │121<font color="#A5A5A1">│ and exit. │</font>122<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 123why, one step back
123

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 125–135why, one step back
125
126// Now pass the NAME argument127$ python main.py Camila128129Hello Camila130131// It works! 🎉132```133134</div>135

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

line 136why, one step back
136**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 137why, one step back
137

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 138–157why, one step back
138## Example upgrade
139140This was the simplest example possible.141142Now let's see one a bit more complex.143144### An example with two subcommands145146Modify the file `main.py`.147148Create a `typer.Typer()` app, and create two subcommands with their parameters.149150```Python hl_lines="3 6 11 20"151import typer152153app = typer.Typer()154155156@app.command()157def hello(name: str):

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 158why, one step back
158 print(f"Hello {name}")

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 159–163why, one step back
159
160161@app.command()162def goodbye(name: str, formal: bool = False):163 if formal:

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 164why, one step back
164 print(f"Goodbye Ms. {name}. Have a good day.")

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 165why, one step back
165 else:

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 166why, one step back
166 print(f"Bye {name}!")

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 167–181why, one step back
167
168169if __name__ == "__main__":170 app()171```172173And that will:174175* Explicitly create a `typer.Typer` app.176 * The previous `typer.run` actually creates one implicitly for you.177* Add two subcommands with `@app.command()`.178* Execute the `app()` itself, as if it was a function (instead of `typer.run`).179180### Run the upgraded example181

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 182–183why, one step back
182Check the new help:
183

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 184–188why, one step back
184<div class="termy">
185186```console187$ python main.py --help188

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 189–206why, one step back
189<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>
190<b> </b>191<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>192<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │193<font color="#A5A5A1">│ for the current │</font>194<font color="#A5A5A1">│ shell. │</font>195<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │196<font color="#A5A5A1">│ the current shell, │</font>197<font color="#A5A5A1">│ to copy it or │</font>198<font color="#A5A5A1">│ customize the │</font>199<font color="#A5A5A1">│ installation. │</font>200<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │201<font color="#A5A5A1">│ and exit. │</font>202<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>203<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────╮</font>204<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>goodbye </b></font> │205<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>hello </b></font> │206<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 207why, one step back
207

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 208–209why, one step back
208// You have 2 subcommands (the 2 functions): goodbye and hello
209```

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 210why, one step back
210

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 212why, one step back
212

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 213why, one step back
213Now check the help for the `hello` command:

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 214why, one step back
214

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 216why, one step back
216

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 217why, one step back
217```console

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 218–219why, one step back
218$ python main.py hello --help
219

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 220–228why, one step back
220<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py hello [OPTIONS] NAME </b>
221<b> </b>222<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>223<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │224<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>225<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>226<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │227<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>228```

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 229why, one step back
229

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

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

line 231why, one step back
231

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 232why, one step back
232And now check the help for the `goodbye` command:

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 233why, one step back
233

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 235why, one step back
235

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 236why, one step back
236```console

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 237–238why, one step back
237$ python main.py goodbye --help
238

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 239–248why, one step back
239<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py goodbye [OPTIONS] NAME </b>
240<b> </b>241<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>242<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │243<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>244<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>245<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--formal</b></font> <font color="#AE81FF"><b>--no-formal</b></font> [default: no-formal] │246<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │247<font color="#A5A5A1">│ and exit. │</font>248<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 249why, one step back
249

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 250–251why, one step back
250// Automatic --formal and --no-formal for the bool option 🎉
251```

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

line 252why, one step back
252

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 254why, one step back
254

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 255why, one step back
255Now you can try out the new command line application:

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 256why, one step back
256

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 258why, one step back
258

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

lines 259–261why, one step back
259```console
260// Use it with the hello command261

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 262–282why, one step back
262$ python main.py hello Camila
263264Hello Camila265266// And with the goodbye command267268$ python main.py goodbye Camila269270Bye Camila!271272// And with --formal273274$ python main.py goodbye --formal Camila275276Goodbye Ms. Camila. Have a good day.277```278279</div>280281### Recap282

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 284–306why, one step back
284
285You do that with standard modern Python types.286287You don't have to learn a new syntax, the methods or classes of a specific library, etc.288289Just standard **Python 3.6+**.290291For example, for an `int`:292293```Python294total: int295```296297or for a `bool` flag:298299```Python300force: bool301```302303And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.304305**You get**: great editor support, including **completion** and **type checks** everywhere.306

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

line 307why, one step back
307**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 docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 308why, one step back
308

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 310–312why, one step back
310
311## Optional Dependencies312

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

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 314–316why, one step back
314
315But you can also install extras:316

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

line 317why, one step back
317* <a href="https://rich.readthedocs.io/en/stable/index.html" class="external-link" target="_blank"><code>rich</code></a>: and Typer will show nicely formatted errors automatically.

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

lines 318–319why, one step back
318* <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.
319 * 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 320why, one step back
320 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

line 321why, one step back
321

✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b

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

:memo: Add docs for get_app_dir()Sebastián Ramírezbefore adoptione4d6047

lines 323–326why, one step back
323
324## License325326This project is licensed under the terms of the MIT license.