Skip to content
Myr

index.md

This file

Path

docs/index.md

396 lines · 14.5 KiB · Markdown

Last changed by

📝 Add link to Typer developer survey (#1514)Sebastián Ramírezbefore adoption4540645

History of this file

Take it

The walk stopped after 200 commits: lines older than 9cdc5b5 point at it.

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 1–14why, one step back
1<style>
2.md-content .md-typeset h1 { display: none; }3</style>45<p align="center">6 <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg#only-light" alt="Typer"></a>7<!-- only-mkdocs -->8 <a href="https://typer.tiangolo.com"><img src="img/logo-margin/logo-margin-white-vector.svg#only-dark" alt="Typer"></a>9<!-- /only-mkdocs -->10</p>11<p align="center">12 <em>Typer, build great CLIs. Easy to code. Based on Python type hints.</em>13</p>14<p align="center">

🩺 Update test badge to only reflect pushes to `master` (#1414)Sofie Van Landeghembefore adoptioncc416ba

lines 15–16why, one step back
15<a href="https://github.com/fastapi/typer/actions?query=workflow%3ATest+event%3Apush+branch%3Amaster" target="_blank">
16 <img src="https://github.com/fastapi/typer/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="Test">

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 17–48why, one step back
17</a>
18<a href="https://github.com/fastapi/typer/actions?query=workflow%3APublish" target="_blank">19 <img src="https://github.com/fastapi/typer/workflows/Publish/badge.svg" alt="Publish">20</a>21<a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/typer" target="_blank">22 <img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/typer.svg" alt="Coverage">23<a href="https://pypi.org/project/typer" target="_blank">24 <img src="https://img.shields.io/pypi/v/typer?color=%2334D058&label=pypi%20package" alt="Package version">25</a>26</p>2728---2930**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>3132**Source Code**: <a href="https://github.com/fastapi/typer" target="_blank">https://github.com/fastapi/typer</a>3334---3536Typer 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 type hints.3738It's also a command line tool to run scripts, automatically converting them to CLI applications.3940The key features are:4142* **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.43* **Easy to use**: It's easy to use for the final users. Automatic help, and automatic completion for all shells.44* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.45* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.46* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.47* **Run scripts**: Typer includes a `typer` command/program that you can use to run scripts, automatically converting them to CLIs, even if they don't use Typer internally.48

📝 Add link to Typer developer survey (#1514)Sebastián Ramírezbefore adoption4540645

lines 49–52why, one step back
49## 2026 February - Typer developer survey
5051Help us define Typer's future by filling the <a href="https://forms.gle/nYvutPrVkmBQZLas7" class="external-link" target="_blank">Typer developer survey</a>. ✨52

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 53–396why, one step back
53## FastAPI of CLIs
5455**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling, it's the FastAPI of CLIs.5657## Installation5859Create and activate a <a href="https://typer.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a> and then install **Typer**:6061<div class="termy">6263```console64$ pip install typer65---> 100%66Successfully installed typer rich shellingham67```6869</div>7071## Example7273### The absolute minimum7475* Create a file `main.py` with:7677```Python78def main(name: str):79 print(f"Hello {name}")80```8182This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.8384### Run it8586Run your application with the `typer` command:8788<div class="termy">8990```console91// Run your application92$ typer main.py run9394// You get a nice error, you are missing NAME95Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME96Try 'typer [PATH_OR_MODULE] run --help' for help.97╭─ Error ───────────────────────────────────────────╮98│ Missing argument 'NAME'. │99╰───────────────────────────────────────────────────╯100101102// You get a --help for free103$ typer main.py run --help104105Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME106107Run the provided Typer app.108109╭─ Arguments ───────────────────────────────────────╮110│ * name TEXT [default: None] [required] |111╰───────────────────────────────────────────────────╯112╭─ Options ─────────────────────────────────────────╮113│ --help Show this message and exit. │114╰───────────────────────────────────────────────────╯115116// Now pass the NAME argument117$ typer main.py run Camila118119Hello Camila120121// It works! 🎉122```123124</div>125126This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.127128**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.129130## Use Typer in your code131132Now let's start using Typer in your own code, update `main.py` with:133134```Python135import typer136137138def main(name: str):139 print(f"Hello {name}")140141142if __name__ == "__main__":143 typer.run(main)144```145146Now you could run it with Python directly:147148<div class="termy">149150```console151// Run your application152$ python main.py153154// You get a nice error, you are missing NAME155Usage: main.py [OPTIONS] NAME156Try 'main.py --help' for help.157╭─ Error ───────────────────────────────────────────╮158│ Missing argument 'NAME'. │159╰───────────────────────────────────────────────────╯160161162// You get a --help for free163$ python main.py --help164165Usage: main.py [OPTIONS] NAME166167╭─ Arguments ───────────────────────────────────────╮168│ * name TEXT [default: None] [required] |169╰───────────────────────────────────────────────────╯170╭─ Options ─────────────────────────────────────────╮171│ --help Show this message and exit. │172╰───────────────────────────────────────────────────╯173174// Now pass the NAME argument175$ python main.py Camila176177Hello Camila178179// It works! 🎉180```181182</div>183184**Note**: you can also call this same script with the `typer` command, but you don't need to.185186## Example upgrade187188This was the simplest example possible.189190Now let's see one a bit more complex.191192### An example with two subcommands193194Modify the file `main.py`.195196Create a `typer.Typer()` app, and create two subcommands with their parameters.197198```Python hl_lines="3 6 11 20"199import typer200201app = typer.Typer()202203204@app.command()205def hello(name: str):206 print(f"Hello {name}")207208209@app.command()210def goodbye(name: str, formal: bool = False):211 if formal:212 print(f"Goodbye Ms. {name}. Have a good day.")213 else:214 print(f"Bye {name}!")215216217if __name__ == "__main__":218 app()219```220221And that will:222223* Explicitly create a `typer.Typer` app.224 * The previous `typer.run` actually creates one implicitly for you.225* Add two subcommands with `@app.command()`.226* Execute the `app()` itself, as if it was a function (instead of `typer.run`).227228### Run the upgraded example229230Check the new help:231232<div class="termy">233234```console235$ python main.py --help236237 Usage: main.py [OPTIONS] COMMAND [ARGS]...238239╭─ Options ─────────────────────────────────────────╮240│ --install-completion Install completion │241│ for the current │242│ shell. │243│ --show-completion Show completion for │244│ the current shell, │245│ to copy it or │246│ customize the │247│ installation. │248│ --help Show this message │249│ and exit. │250╰───────────────────────────────────────────────────╯251╭─ Commands ────────────────────────────────────────╮252│ goodbye │253│ hello │254╰───────────────────────────────────────────────────╯255256// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion257258// You have 2 subcommands (the 2 functions): goodbye and hello259```260261</div>262263Now check the help for the `hello` command:264265<div class="termy">266267```console268$ python main.py hello --help269270 Usage: main.py hello [OPTIONS] NAME271272╭─ Arguments ───────────────────────────────────────╮273│ * name TEXT [default: None] [required] │274╰───────────────────────────────────────────────────╯275╭─ Options ─────────────────────────────────────────╮276│ --help Show this message and exit. │277╰───────────────────────────────────────────────────╯278```279280</div>281282And now check the help for the `goodbye` command:283284<div class="termy">285286```console287$ python main.py goodbye --help288289 Usage: main.py goodbye [OPTIONS] NAME290291╭─ Arguments ───────────────────────────────────────╮292│ * name TEXT [default: None] [required] │293╰───────────────────────────────────────────────────╯294╭─ Options ─────────────────────────────────────────╮295│ --formal --no-formal [default: no-formal] │296│ --help Show this message │297│ and exit. │298╰───────────────────────────────────────────────────╯299300// Automatic --formal and --no-formal for the bool option 🎉301```302303</div>304305Now you can try out the new command line application:306307<div class="termy">308309```console310// Use it with the hello command311312$ python main.py hello Camila313314Hello Camila315316// And with the goodbye command317318$ python main.py goodbye Camila319320Bye Camila!321322// And with --formal323324$ python main.py goodbye --formal Camila325326Goodbye Ms. Camila. Have a good day.327```328329</div>330331**Note**: If your app only has one command, by default the command name is **omitted** in usage: `python main.py Camila`. However, when there are multiple commands, you must **explicitly include the command name**: `python main.py hello Camila`. See [One or Multiple Commands](https://typer.tiangolo.com/tutorial/commands/one-or-multiple/) for more details.332333### Recap334335In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.336337You do that with standard modern Python types.338339You don't have to learn a new syntax, the methods or classes of a specific library, etc.340341Just standard **Python**.342343For example, for an `int`:344345```Python346total: int347```348349or for a `bool` flag:350351```Python352force: bool353```354355And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.356357**You get**: great editor support, including **completion** and **type checks** everywhere.358359**Your users get**: automatic **`--help`**, **auto-completion** in their terminal (Bash, Zsh, Fish, PowerShell) when they install your package or when using the `typer` command.360361For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.362363## Dependencies364365**Typer** stands on the shoulders of a giant. Its only internal required dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.366367By default it also comes with extra standard dependencies:368369* <a href="https://rich.readthedocs.io/en/stable/index.html" class="external-link" target="_blank"><code>rich</code></a>: to show nicely formatted errors automatically.370* <a href="https://github.com/sarugaku/shellingham" class="external-link" target="_blank"><code>shellingham</code></a>: to automatically detect the current shell when installing completion.371 * With `shellingham` you can just use `--install-completion`.372 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.373374### `typer-slim`375376If you don't want the extra standard optional dependencies, install `typer-slim` instead.377378When you install with:379380```bash381pip install typer382```383384...it includes the same code and dependencies as:385386```bash387pip install "typer-slim[standard]"388```389390The `standard` extra dependencies are `rich` and `shellingham`.391392**Note**: The `typer` command is only included in the `typer` package.393394## License395396This project is licensed under the terms of the MIT license.