Skip to content
Myr

index.md

This file

Path

docs/index.md

305 lines · 7.7 KiB · Markdown

Last changed by

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

History of this file

Take it

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

lines 1–305why, one step back
1<p align="center">
2 <a href="https://typer.tiangolo.com"><img src="/img/logo-margin/logo-margin-vector.svg" alt="Typer"></a>3</p>4<p align="center">5 <em>Typer: CLIs with autocompletion. While developing and using.</em>6</p>7<p align="center">8<a href="https://travis-ci.org/tiangolo/typer" target="_blank">9 <img src="https://travis-ci.org/tiangolo/typer.svg?branch=master" alt="Build Status">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---2627Typer is library to build <abbr title="command line interface, programs executed from a terminal">CLI</abbr> applications that users love using and developers love creating. Based on Python 3.6+ type hints.2829**Typer** is the little sibling of <a href="https://fastapi.tiangolo.com" target="_blank">FastAPI</a>. And it's intended to be the FastAPI of CLIs.3031The 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**.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.3839## Requirements4041Python 3.6+4243Typer stands on the shoulders of a giant. Internally it uses <a href="https://click.palletsprojects.com/" target="_blank">Click</a>, that's the only dependency.4445## 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 upgrade114115The previous example was the extreme in terms of simplicity.116117Now 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.282283But you can install extras:284285* <a href="https://pypi.org/project/colorama/" target="_blank"><code>colorama</code></a>: and Click will automatically use it to make sure colors always work correctly, even in Windows.286 * Then you can use any tool you want to output colors in all the systems, including the integrated `typer.style()` and `typer.secho()` (provided by Click).287 * 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.289290You can install `typer` with `colorama` and `click-completion` with `pip3 install typer[all]`.291292## 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.299 * * There are several other Click plug-ins at <a href="https://github.com/click-contrib" target="_blank">click-contrib</a> that you can explore.300* <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.