Skip to content
Myr

README.md

This file

Path

README.md

305 lines · 7.7 KiB · Markdown

Last changed by

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

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–7why, one step back
3</p>
4<p align="center">5 <em>Typer: CLIs with autocompletion. While developing and using.</em>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–36why, 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.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 back
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.

: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–114why, one step back
44
45## 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

: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–282why, one step back
116
117Now 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

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

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

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

line 284why, one step back
284

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

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

lines 287–289why, one step back
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.289

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

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

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

lines 291–298why, one step back
291
292## 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.

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

line 299why, one step back
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.

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

lines 300–301why, one step back
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.

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

lines 302–305
302
303## License304305This project is licensed under the terms of the MIT license.