index.md
This file
Path
docs/index.md
305 lines · 7.7 KiB · Markdown
Last changed by
:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
History of this fileTake it
:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f
line 1why, one step back1<p align="center">:bug: Fix GitHub imageSebastián Ramírezbefore adoption4792de3
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 first version of docsSebastián Ramírezbefore adoptionc24269f
lines 3–7why, one step back3</p>4<p align="center">5 <em>Typer: CLIs with autocompletion. While developing and using.</em>6</p>7<p align="center">:art: Fix types and formatSebastián Ramírezbefore adoptionb52fc16
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 first version of docsSebastián Ramírezbefore adoptionc24269f
lines 10–26why, 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>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 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 first version of docsSebastián Ramírezbefore adoptionc24269f
line 28why, one step back28:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
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 first version of docsSebastián Ramírezbefore adoptionc24269f
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**.:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
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 first version of docsSebastián Ramírezbefore adoptionc24269f
lines 38–42why, one step back3839## Requirements4041Python 3.6+42:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
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 first version of docsSebastián Ramírezbefore adoptionc24269f
lines 44–114why, one step back4445## Installation4647```bash48pip install typer49```5051## Example5253### The absolute minimum5455* Create a file `main.py` with:5657```Python58import typer596061def main(name: str):62 typer.echo(f"Hello {name}")636465if __name__ == "__main__":66 typer.run(main)67```6869### Run it7071Run your application:7273```bash74python main.py75```7677you will get a response like:7879```80Usage: main.py [OPTIONS] NAME81Try "main.py --help" for help.8283Error: Missing argument "NAME".84```8586Now pass the `NAME` *argument*:8788```bash89python main.py Camila90```9192You will get a response like:9394```95Hello Camila96```9798And you automatically get a `--help` command:99100```bash101python main.py --help102```103104shows:105106```107Usage: main.py [OPTIONS] NAME108109Options:110 --help Show this message and exit.111```112113## Example upgrade114:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
line 115why, one step back115This was the simplest example possible.:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f
lines 116–282why, one step back116117Now let's see one a bit more complex.118119### An example with two sub-commands120121Modify the file `main.py`.122123Create a `typer.Typer()` app, and create two sub-commands with their parameters.124125```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.152* Add two sub-commands with `@app.command()`.153* Execute the `app()` itself, as if it was a function (instead of `typer.run`).154155### Run the upgraded example156157Get the main `--help`:158159```bash160python main.py --help161```162163shows:164165```166Usage: main.py [OPTIONS] COMMAND [ARGS]...167168Options:169 --help Show this message and exit.170171Commands:172 goodbye173 hello174```175176You have 2 sub-commands (the 2 functions), `goodbye` and `hello`.177178Now get the help for `hello`:179180```bash181python main.py hello --help182```183184shows:185186```187Usage: main.py hello [OPTIONS] NAME188189Options:190 --help Show this message and exit.191```192193And now get the help for `goodbye`:194195```bash196python main.py goodbye --help197```198199shows:200201```202Usage: main.py goodbye [OPTIONS] NAME203204Options:205 --formal / --no-formal206 --help Show this message and exit.207```208209Notice how it automatically creates a `--formal` and `--no-formal` for your `bool` *option*.210211---212213And of course, if you use it, it does what you expect:214215```bash216python main.py hello Camila217```218219shows:220221```222Hello Camila223```224225Then:226227```bash228python main.py goodbye Camila229```230231shows:232233```234Bye Camila!235```236237And:238239```bash240python main.py goodbye --formal Camila241```242243shows:244245```246Goodbye Ms. Camila. Have a good day.247```248249### Recap250251In summary, you declare **once** the types of parameters (*arguments* and *options*) as function parameters.252253You do that with standard modern Python types.254255You don't have to learn a new syntax, the methods or classes of a specific library, etc.256257Just standard **Python 3.6+**.258259For example, for an `int`:260261```Python262total: int263```264265or for a `bool` flag:266267```Python268force: bool269```270271And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of sub-commands**, add metadata, extra **validation**, etc.272273**You get**: great editor support, including **completion** and **type checks** everywhere.274275**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).276277For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/intro/">Tutorial - User Guide</a>.278279## Optional Dependencies280281Typer uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a> internally. That's the only dependency.282:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
line 283why, one step back283But you can also install extras::memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f
line 284why, one step back284:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
lines 285–286why, one step back285* <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.286 * 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 287–289why, one step back287 * 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>.288* <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.289:pencil: Rewording in docs and fix typosSebastián Ramírezbefore adoption7b115c3
line 290why, one step back290You can install `typer` with `colorama` and `click-completion` with `pip install typer[all]`.:memo: Add first version of docsSebastián Ramírezbefore adoptionc24269f
lines 291–298why, one step back291292## Other tools and plug-ins293294Click 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.295296For example:297298* <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 299why, one step back299 * 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 300–305why, one step back300* <a href="https://pypi.org/project/tabulate/" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or typer.301* etc... you can re-use many of the great available tools for building CLIs.302303## License304305This project is licensed under the terms of the MIT license.