README.md
This file
Path
README.md
268 lines · 7.6 KiB · Markdown
Last changed by
:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
History of this fileTake it
:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 1why, one step back1<p align="center">:bug: Fix link in README for GitHubSebastián Ramírezbefore adoptionc820ba0
line 2why, one step back2 <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 back3</p>4<p align="center">:memo: Update sub-header, short descriptionSebastián Ramírezbefore adoptionbc931c1
line 5why, one step back5 <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 back6</p>7<p align="center">:memo: Update READMESebastián Ramírezbefore adoption08d36b1
lines 8–9why, one step back8<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 back10</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>18:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 19why, one step back19---20:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 21why, one step back21**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>22:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 23–26why, one step back23**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 back27Typer 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 back28:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 29why, one step back29**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–36why, one step back3031The 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**.:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 37why, one step back37* **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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 38–42why, one step back3839## Requirements4041Python 3.6+42:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 43why, one step back43Typer 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 back4445## Installation46:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 47why, one step back47<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 48why, one step back48:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 49–52why, one step back49```console50$ pip install typer51---> 100%52Successfully installed typer:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 53why, one step back53```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 54why, one step back54:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 55why, one step back55</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 56–78why, one step back5657## 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 back79<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 80why, one step back80:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 81–83why, one step back81```console82// Run your application83$ python main.py:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 84why, one step back84:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 85why, one step back85// You get a nice error, you are missing NAME:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 86–90why, one step back86Usage: main.py [OPTIONS] NAME87Try "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 back91// You get a --help for free92$ python main.py --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 93why, one step back93:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 94why, one step back94Usage: main.py [OPTIONS] NAME:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 95why, one step back95:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 96–97why, one step back96Options:97 --help Show this message and exit.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 98why, one step back98:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 99–101why, one step back99// Now pass the NAME argument100$ python main.py Camila101:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 102–103why, one step back102Hello Camila103:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 104why, one step back104// It works! 🎉:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 105why, one step back105```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 106why, one step back106:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 107why, one step back107</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 108–110why, one step back108109## Example upgrade110:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 111why, one step back111This was the simplest example possible.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 112–152why, one step back112113Now 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:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 153why, one step back153<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 154why, one step back154:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 155–157why, one step back155```console156// Check the --help157$ python main.py --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 158–167why, one step back158159Usage: main.py [OPTIONS] COMMAND [ARGS]...160161Options:162 --help Show this message and exit.163164Commands:165 goodbye166 hello167:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 168why, one step back168// You have 2 sub-commands (the 2 functions): goodbye and hello:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 169why, one step back169:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 170why, one step back170// Now get the --help for hello:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 171why, one step back171:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 172why, one step back172$ python main.py hello --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 173–178why, one step back173174Usage: main.py hello [OPTIONS] NAME175176Options:177 --help Show this message and exit.178:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 179why, one step back179// And now get the --help for goodbye:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 180why, one step back180:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 181why, one step back181$ python main.py goodbye --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 182–188why, one step back182183Usage: main.py goodbye [OPTIONS] NAME184185Options:186 --formal / --no-formal187 --help Show this message and exit.188:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 189why, one step back189// Automatic --formal and --no-formal for the bool option 🎉:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 190why, one step back190:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 191why, one step back191// And if you use it with the hello command:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 192why, one step back192:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 193why, one step back193$ python main.py hello Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 194–196why, one step back194195Hello Camila196:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 197why, one step back197// And with the goodbye command:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 198why, one step back198:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 199why, one step back199$ python main.py goodbye Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 200–202why, one step back200201Bye Camila!202:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 203why, one step back203// And with --formal:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 204why, one step back204:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 205why, one step back205$ python main.py goodbye --formal Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 206–208why, one step back206207Goodbye Ms. Camila. Have a good day.208```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 209why, one step back209:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 210why, one step back210</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 211–245why, one step back211212### 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:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 246why, one step back246But you can also install extras::memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 247why, one step back247:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
lines 248–249why, one step back248* <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 README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 250–252why, one step back250 * 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:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 253why, one step back253You can install `typer` with `colorama` and `click-completion` with `pip install typer[all]`.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 254–261why, one step back254255## 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.:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 262why, one step back262 * 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 263–264why, one step back263* <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.:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112
lines 265–268265266## License267268This project is licensed under the terms of the MIT license.