index.md
This file
Path
docs/index.md
388 lines · 13.7 KiB · Markdown
Last changed by
📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b
History of this fileTake it
The walk stopped after 200 commits: lines older than 5c729e8 point at it.
🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f
lines 1–4why, one step back1<style>2.md-content .md-typeset h1 { display: none; }3</style>4⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 5why, one step back5<p align="center">💄 Add dark mode logo (#907)Sebastián Ramírezbefore adoptiond3fe76a
lines 6–9why, one step back6 <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 -->⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 10–14why, one step back10</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 docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b
lines 15–16why, one step back15<a href="https://github.com/fastapi/typer/actions?query=workflow%3ATest" target="_blank">16 <img src="https://github.com/fastapi/typer/workflows/Test/badge.svg" alt="Test">⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 17why, one step back17</a>📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b
lines 18–19why, one step back18<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">⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 20why, one step back20</a>📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b
lines 21–22why, one step back21<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">⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 23–31why, one step back23<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>31📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b
line 32why, one step back32**Source Code**: <a href="https://github.com/fastapi/typer" target="_blank">https://github.com/fastapi/typer</a>⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 33–35why, one step back3334---35🔥 Remove support for Python 3.6 (#758)Sebastián Ramírezbefore adoption13404a0
line 36why, one step back36Typer 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.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 37why, one step back37✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 38–39why, one step back38It's also a command line tool to run scripts, automatically converting them to CLI applications.39⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 40–46why, one step back40The 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.🐛 Fix broken installation when upgrading from `typer <0.12.0` to `typer >=0.12.0`, make `typer` independent of `typer-slim`, include `typer` command in `typer` package (#791)Sebastián Ramírezbefore adoption05ffb24
line 47why, one step back47* **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.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 48–50why, one step back4849## FastAPI of CLIs50✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 51why, one step back51**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling, it's the FastAPI of CLIs.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 52–57why, one step back5253## Installation5455<div class="termy">5657```console✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 58why, one step back58$ pip install typer⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 59why, one step back59---> 100%✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 60why, one step back60Successfully installed typer rich shellingham⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 61–71why, one step back61```6263</div>6465## Example6667### The absolute minimum6869* Create a file `main.py` with:7071```Python✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 72–128why, one step back72def main(name: str):73 print(f"Hello {name}")74```7576This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.7778### Run it7980Run your application with the `typer` command:8182<div class="termy">8384```console85// Run your application86$ typer main.py run8788// You get a nice error, you are missing NAME89Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME90Try 'typer [PATH_OR_MODULE] run --help' for help.91╭─ Error ───────────────────────────────────────────╮92│ Missing argument 'NAME'. │93╰───────────────────────────────────────────────────╯949596// You get a --help for free97$ typer main.py run --help9899Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME100101Run the provided Typer app.102103╭─ Arguments ───────────────────────────────────────╮104│ * name TEXT [default: None] [required] |105╰───────────────────────────────────────────────────╯106╭─ Options ─────────────────────────────────────────╮107│ --help Show this message and exit. │108╰───────────────────────────────────────────────────╯109110// Now pass the NAME argument111$ typer main.py run Camila112113Hello Camila114115// It works! 🎉116```117118</div>119120This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.121122**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.123124## Use Typer in your code125126Now let's start using Typer in your own code, update `main.py` with:127128```Python⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 129–139why, one step back129import typer130131132def main(name: str):133 print(f"Hello {name}")134135136if __name__ == "__main__":137 typer.run(main)138```139✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 140why, one step back140Now you could run it with Python directly:⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 141–177why, one step back141142<div class="termy">143144```console145// Run your application146$ python main.py147148// You get a nice error, you are missing NAME149Usage: main.py [OPTIONS] NAME150Try 'main.py --help' for help.151╭─ Error ───────────────────────────────────────────╮152│ Missing argument 'NAME'. │153╰───────────────────────────────────────────────────╯154155156// You get a --help for free157$ python main.py --help158159Usage: main.py [OPTIONS] NAME160161╭─ Arguments ───────────────────────────────────────╮162│ * name TEXT [default: None] [required] |163╰───────────────────────────────────────────────────╯164╭─ Options ─────────────────────────────────────────╮165│ --help Show this message and exit. │166╰───────────────────────────────────────────────────╯167168// Now pass the NAME argument169$ python main.py Camila170171Hello Camila172173// It works! 🎉174```175176</div>177✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 178why, one step back178**Note**: you can also call this same script with the `typer` command, but you don't need to.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 179–332why, one step back179180## Example upgrade181182This was the simplest example possible.183184Now let's see one a bit more complex.185186### An example with two subcommands187188Modify the file `main.py`.189190Create a `typer.Typer()` app, and create two subcommands with their parameters.191192```Python hl_lines="3 6 11 20"193import typer194195app = typer.Typer()196197198@app.command()199def hello(name: str):200 print(f"Hello {name}")201202203@app.command()204def goodbye(name: str, formal: bool = False):205 if formal:206 print(f"Goodbye Ms. {name}. Have a good day.")207 else:208 print(f"Bye {name}!")209210211if __name__ == "__main__":212 app()213```214215And that will:216217* Explicitly create a `typer.Typer` app.218 * The previous `typer.run` actually creates one implicitly for you.219* Add two subcommands with `@app.command()`.220* Execute the `app()` itself, as if it was a function (instead of `typer.run`).221222### Run the upgraded example223224Check the new help:225226<div class="termy">227228```console229$ python main.py --help230231 Usage: main.py [OPTIONS] COMMAND [ARGS]...232233╭─ Options ─────────────────────────────────────────╮234│ --install-completion Install completion │235│ for the current │236│ shell. │237│ --show-completion Show completion for │238│ the current shell, │239│ to copy it or │240│ customize the │241│ installation. │242│ --help Show this message │243│ and exit. │244╰───────────────────────────────────────────────────╯245╭─ Commands ────────────────────────────────────────╮246│ goodbye │247│ hello │248╰───────────────────────────────────────────────────╯249250// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion251252// You have 2 subcommands (the 2 functions): goodbye and hello253```254255</div>256257Now check the help for the `hello` command:258259<div class="termy">260261```console262$ python main.py hello --help263264 Usage: main.py hello [OPTIONS] NAME265266╭─ Arguments ───────────────────────────────────────╮267│ * name TEXT [default: None] [required] │268╰───────────────────────────────────────────────────╯269╭─ Options ─────────────────────────────────────────╮270│ --help Show this message and exit. │271╰───────────────────────────────────────────────────╯272```273274</div>275276And now check the help for the `goodbye` command:277278<div class="termy">279280```console281$ python main.py goodbye --help282283 Usage: main.py goodbye [OPTIONS] NAME284285╭─ Arguments ───────────────────────────────────────╮286│ * name TEXT [default: None] [required] │287╰───────────────────────────────────────────────────╯288╭─ Options ─────────────────────────────────────────╮289│ --formal --no-formal [default: no-formal] │290│ --help Show this message │291│ and exit. │292╰───────────────────────────────────────────────────╯293294// Automatic --formal and --no-formal for the bool option 🎉295```296297</div>298299Now you can try out the new command line application:300301<div class="termy">302303```console304// Use it with the hello command305306$ python main.py hello Camila307308Hello Camila309310// And with the goodbye command311312$ python main.py goodbye Camila313314Bye Camila!315316// And with --formal317318$ python main.py goodbye --formal Camila319320Goodbye Ms. Camila. Have a good day.321```322323</div>324325### Recap326327In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.328329You do that with standard modern Python types.330331You don't have to learn a new syntax, the methods or classes of a specific library, etc.332🔥 Remove support for Python 3.6 (#758)Sebastián Ramírezbefore adoption13404a0
line 333why, one step back333Just standard **Python**.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 334–350why, one step back334335For example, for an `int`:336337```Python338total: int339```340341or for a `bool` flag:342343```Python344force: bool345```346347And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.348349**You get**: great editor support, including **completion** and **type checks** everywhere.350✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 351why, one step back351**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.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 352–354why, one step back352353For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.354✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 355why, one step back355## Dependencies⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 356why, one step back356✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 357why, one step back357**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>.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 358why, one step back358✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 359why, one step back359By default it also comes with extra standard dependencies:⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
line 360why, one step back360✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 361–362why, one step back361* <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.362* <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.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 363–365why, one step back363 * With `shellingham` you can just use `--install-completion`.364 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.365✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 366–367why, one step back366### `typer-slim`367🐛 Fix broken installation when upgrading from `typer <0.12.0` to `typer >=0.12.0`, make `typer` independent of `typer-slim`, include `typer` command in `typer` package (#791)Sebastián Ramírezbefore adoption05ffb24
line 368why, one step back368If you don't want the extra standard optional dependencies, install `typer-slim` instead.✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 369–375why, one step back369370When you install with:371372```bash373pip install typer374```375🐛 Fix broken installation when upgrading from `typer <0.12.0` to `typer >=0.12.0`, make `typer` independent of `typer-slim`, include `typer` command in `typer` package (#791)Sebastián Ramírezbefore adoption05ffb24
line 376why, one step back376...it includes the same code and dependencies as:✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
lines 377–381why, one step back377378```bash379pip install "typer-slim[standard]"380```381🐛 Fix broken installation when upgrading from `typer <0.12.0` to `typer >=0.12.0`, make `typer` independent of `typer-slim`, include `typer` command in `typer` package (#791)Sebastián Ramírezbefore adoption05ffb24
line 382why, one step back382The `standard` extra dependencies are `rich` and `shellingham`.✨ Add `typer-slim` package without extras, make `typer` include `typer-slim[default]` and integrate Typer CLI (`typer` command) into Typer (#780)Sebastián Ramírezbefore adoption7d5d6bb
line 383why, one step back383🐛 Fix broken installation when upgrading from `typer <0.12.0` to `typer >=0.12.0`, make `typer` independent of `typer-slim`, include `typer` command in `typer` package (#791)Sebastián Ramírezbefore adoption05ffb24
line 384why, one step back384**Note**: The `typer` command is only included in the `typer` package.⬆ Bump dawidd6/action-download-artifact from 2.24.2 to 2.26.0 (#558)dependabot[bot]before adoption5c729e8
lines 385–388why, one step back385386## License387388This project is licensed under the terms of the MIT license.