README.md
This file
Path
README.md
317 lines · 11.6 KiB · Markdown
Last changed by
📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
History of this fileTake it
The walk stopped after 200 commits: lines older than 7f65b64 point at it.
🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 1–13why, one step back1<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">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">10</a>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 back14<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">🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 16–57why, one step back16<a href="https://pypi.org/project/typer" target="_blank">17 <img src="https://img.shields.io/pypi/v/typer?color=%2334D058&label=pypi%20package" alt="Package version">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---2829Typer 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.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, and 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 of subcommands, with options and arguments.3839## FastAPI of CLIs4041<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.4647## Requirements4849Python 3.6+5051**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>.5253## Installation5455<div class="termy">5657```console✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 58why, one step back58$ pip install "typer[all]"🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 59–64why, one step back59---> 100%60Successfully installed typer61```6263</div>64✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
lines 65–66why, one step back65**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🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 67–77why, one step back67## Example6869### 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 back78 print(f"Hello {name}")🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 79–95why, one step back798081if __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📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
lines 96–100why, one step back96Usage: main.py [OPTIONS] NAME97Try 'main.py --help' for help.98╭─ Error ───────────────────────────────────────────╮99│ Missing argument 'NAME'. │100╰───────────────────────────────────────────────────╯🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 101–105why, one step back101102103// You get a --help for free104$ python main.py --help105📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
line 106why, one step back106Usage: main.py [OPTIONS] NAME🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 107why, one step back107📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
lines 108–113why, one step back108╭─ Arguments ───────────────────────────────────────╮109│ * name TEXT [default: None] [required] |110╰───────────────────────────────────────────────────╯111╭─ Options ─────────────────────────────────────────╮112│ --help Show this message and exit. │113╰───────────────────────────────────────────────────╯🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 114–146why, one step back114115// Now pass the NAME argument116$ python main.py Camila117118Hello Camila119120// It works! 🎉121```122123</div>124125**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>.126127## Example upgrade128129This was the simplest example possible.130131Now let's see one a bit more complex.132133### An example with two subcommands134135Modify the file `main.py`.136137Create a `typer.Typer()` app, and create two subcommands with their parameters.138139```Python hl_lines="3 6 11 20"140import typer141142app = typer.Typer()143144145@app.command()146def hello(name: str):✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 147why, one step back147 print(f"Hello {name}")🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 148–152why, one step back148149150@app.command()151def goodbye(name: str, formal: bool = False):152 if formal:✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 153why, one step back153 print(f"Goodbye Ms. {name}. Have a good day.")🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 154why, one step back154 else:✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 155why, one step back155 print(f"Bye {name}!")🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 156–170why, one step back156157158if __name__ == "__main__":159 app()160```161162And that will:163164* Explicitly create a `typer.Typer` app.165 * The previous `typer.run` actually creates one implicitly for you.166* Add two subcommands with `@app.command()`.167* Execute the `app()` itself, as if it was a function (instead of `typer.run`).168169### Run the upgraded example170✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
lines 171–172why, one step back171Check the new help:172🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 173–177why, one step back173<div class="termy">174175```console176$ python main.py --help177📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
line 178why, one step back178 Usage: main.py [OPTIONS] COMMAND [ARGS]...🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 179why, one step back179📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
lines 180–198why, one step back180╭─ Options ─────────────────────────────────────────╮181│ --install-completion Install completion │182│ for the current │183│ shell. │184│ --show-completion Show completion for │185│ the current shell, │186│ to copy it or │187│ customize the │188│ installation. │189│ --help Show this message │190│ and exit. │191╰───────────────────────────────────────────────────╯192╭─ Commands ────────────────────────────────────────╮193│ goodbye │194│ hello │195╰───────────────────────────────────────────────────╯196197// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion198✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
lines 199–200why, one step back199// You have 2 subcommands (the 2 functions): goodbye and hello200```🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 201why, one step back201✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 202why, one step back202</div>🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 203why, one step back203✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 204why, one step back204Now check the help for the `hello` command:🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 205why, one step back205✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 206why, one step back206<div class="termy">🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 207why, one step back207✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 208why, one step back208```console🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 209–210why, one step back209$ python main.py hello --help210📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
lines 211–218why, one step back211 Usage: main.py hello [OPTIONS] NAME212213╭─ Arguments ───────────────────────────────────────╮214│ * name TEXT [default: None] [required] │215╰───────────────────────────────────────────────────╯216╭─ Options ─────────────────────────────────────────╮217│ --help Show this message and exit. │218╰───────────────────────────────────────────────────╯✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 219why, one step back219```🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 220why, one step back220✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 221why, one step back221</div>🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 222why, one step back222✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 223why, one step back223And now check the help for the `goodbye` command:🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 224why, one step back224✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 225why, one step back225<div class="termy">🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 226why, one step back226✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 227why, one step back227```console🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 228–229why, one step back228$ python main.py goodbye --help229📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467
lines 230–239why, one step back230 Usage: main.py goodbye [OPTIONS] NAME231232╭─ Arguments ───────────────────────────────────────╮233│ * name TEXT [default: None] [required] │234╰───────────────────────────────────────────────────╯235╭─ Options ─────────────────────────────────────────╮236│ --formal --no-formal [default: no-formal] │237│ --help Show this message │238│ and exit. │239╰───────────────────────────────────────────────────╯🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 240why, one step back240✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
lines 241–242why, one step back241// Automatic --formal and --no-formal for the bool option 🎉242```🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 243why, one step back243✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 244why, one step back244</div>🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 245why, one step back245✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 246why, one step back246Now you can try out the new command line application:🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 247why, one step back247✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 248why, one step back248<div class="termy">🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
line 249why, one step back249✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
lines 250–252why, one step back250```console251// Use it with the hello command252🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 253–307why, one step back253$ python main.py hello Camila254255Hello Camila256257// And with the goodbye command258259$ python main.py goodbye Camila260261Bye Camila!262263// And with --formal264265$ python main.py goodbye --formal Camila266267Goodbye Ms. Camila. Have a good day.268```269270</div>271272### Recap273274In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.275276You do that with standard modern Python types.277278You don't have to learn a new syntax, the methods or classes of a specific library, etc.279280Just standard **Python 3.6+**.281282For example, for an `int`:283284```Python285total: int286```287288or for a `bool` flag:289290```Python291force: bool292```293294And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.295296**You get**: great editor support, including **completion** and **type checks** everywhere.297298**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>.299300For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.301302## Optional Dependencies303304Typer uses <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a> internally. That's the only dependency.305306But you can also install extras:307✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 308why, one step back308* <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.🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 309–312why, one step back309* <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.310 * With `shellingham` you can just use `--install-completion`.311 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.312✨ Richify, add integrations with Rich everywhere (#419)Sebastián Ramírezbefore adoption6208b5b
line 313why, one step back313You can install `typer` with `rich` and `shellingham` with `pip install typer[all]`.🎨 Fix header format for "Standard Input" in `docs/tutorial/printing.md` (#386)Brian Cohanbefore adoption7f65b64
lines 314–317why, one step back314315## License316317This project is licensed under the terms of the MIT license.