Skip to content
Myr

index.md

This file

Path

docs/index.md

390 lines · 13.8 KiB · Markdown

Last changed by

📝 Add docs for virtual environments, environment variables, and update contributing (#946)Sebastián Ramírezbefore adoption73b9698

History of this file

Take it

The walk stopped after 200 commits: lines older than 7202e86 point at it.

🔧 Update MkDocs configs and docs build setup (#900)Sebastián Ramírezbefore adoption0f4b07f

lines 1–4why, one step back
1<style>
2.md-content .md-typeset h1 { display: none; }3</style>4

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 5why, one step back
5<p align="center">

💄 Add dark mode logo (#907)Sebastián Ramírezbefore adoptiond3fe76a

lines 6–9why, one step back
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 -->

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 10–14why, one step back
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 docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b

lines 15–16why, one step back
15<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">

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 17why, one step back
17</a>

📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b

lines 18–19why, one step back
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">

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 20why, one step back
20</a>

📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b

lines 21–22why, one step back
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">

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 23–31why, one step back
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>31

📝 Update docs links, from tiangolo to new fastapi org (#919)Sebastián Ramírezbefore adoptione3be56b

line 32why, one step back
32**Source Code**: <a href="https://github.com/fastapi/typer" target="_blank">https://github.com/fastapi/typer</a>

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 33–35why, one step back
33
34---35

🔥 Remove support for Python 3.6 (#758)Sebastián Ramírezbefore adoption13404a0

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

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 37why, one step back
37

✨ 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 back
38It's also a command line tool to run scripts, automatically converting them to CLI applications.
39

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 40–46why, one step back
40The 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 back
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.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 48–50why, one step back
48
49## 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 back
51**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling, it's the FastAPI of CLIs.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 52–54why, one step back
52
53## Installation54

📝 Add docs for virtual environments, environment variables, and update contributing (#946)Sebastián Ramírezbefore adoption73b9698

lines 55–56why, one step back
55Create and activate a <a href="https://typer.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a> and then install **Typer**:
56

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 57–59why, one step back
57<div class="termy">
5859```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 60why, one step back
60$ pip install typer

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 61why, one step back
61---> 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 62why, one step back
62Successfully installed typer rich shellingham

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 63–73why, one step back
63```
6465</div>6667## Example6869### The absolute minimum7071* Create a file `main.py` with:7273```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 74–130why, one step back
74def main(name: str):
75 print(f"Hello {name}")76```7778This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.7980### Run it8182Run your application with the `typer` command:8384<div class="termy">8586```console87// Run your application88$ typer main.py run8990// You get a nice error, you are missing NAME91Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME92Try 'typer [PATH_OR_MODULE] run --help' for help.93╭─ Error ───────────────────────────────────────────╮94│ Missing argument 'NAME'. │95╰───────────────────────────────────────────────────╯969798// You get a --help for free99$ typer main.py run --help100101Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME102103Run the provided Typer app.104105╭─ Arguments ───────────────────────────────────────╮106│ * name TEXT [default: None] [required] |107╰───────────────────────────────────────────────────╯108╭─ Options ─────────────────────────────────────────╮109│ --help Show this message and exit. │110╰───────────────────────────────────────────────────╯111112// Now pass the NAME argument113$ typer main.py run Camila114115Hello Camila116117// It works! 🎉118```119120</div>121122This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.123124**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.125126## Use Typer in your code127128Now let's start using Typer in your own code, update `main.py` with:129130```Python

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 131–141why, one step back
131import typer
132133134def main(name: str):135 print(f"Hello {name}")136137138if __name__ == "__main__":139 typer.run(main)140```141

✨ 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 142why, one step back
142Now you could run it with Python directly:

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 143–179why, one step back
143
144<div class="termy">145146```console147// Run your application148$ python main.py149150// You get a nice error, you are missing NAME151Usage: main.py [OPTIONS] NAME152Try 'main.py --help' for help.153╭─ Error ───────────────────────────────────────────╮154│ Missing argument 'NAME'. │155╰───────────────────────────────────────────────────╯156157158// You get a --help for free159$ python main.py --help160161Usage: main.py [OPTIONS] NAME162163╭─ Arguments ───────────────────────────────────────╮164│ * name TEXT [default: None] [required] |165╰───────────────────────────────────────────────────╯166╭─ Options ─────────────────────────────────────────╮167│ --help Show this message and exit. │168╰───────────────────────────────────────────────────╯169170// Now pass the NAME argument171$ python main.py Camila172173Hello Camila174175// It works! 🎉176```177178</div>179

✨ 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 180why, one step back
180**Note**: you can also call this same script with the `typer` command, but you don't need to.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 181–334why, one step back
181
182## Example upgrade183184This was the simplest example possible.185186Now let's see one a bit more complex.187188### An example with two subcommands189190Modify the file `main.py`.191192Create a `typer.Typer()` app, and create two subcommands with their parameters.193194```Python hl_lines="3 6 11 20"195import typer196197app = typer.Typer()198199200@app.command()201def hello(name: str):202 print(f"Hello {name}")203204205@app.command()206def goodbye(name: str, formal: bool = False):207 if formal:208 print(f"Goodbye Ms. {name}. Have a good day.")209 else:210 print(f"Bye {name}!")211212213if __name__ == "__main__":214 app()215```216217And that will:218219* Explicitly create a `typer.Typer` app.220 * The previous `typer.run` actually creates one implicitly for you.221* Add two subcommands with `@app.command()`.222* Execute the `app()` itself, as if it was a function (instead of `typer.run`).223224### Run the upgraded example225226Check the new help:227228<div class="termy">229230```console231$ python main.py --help232233 Usage: main.py [OPTIONS] COMMAND [ARGS]...234235╭─ Options ─────────────────────────────────────────╮236│ --install-completion Install completion │237│ for the current │238│ shell. │239│ --show-completion Show completion for │240│ the current shell, │241│ to copy it or │242│ customize the │243│ installation. │244│ --help Show this message │245│ and exit. │246╰───────────────────────────────────────────────────╯247╭─ Commands ────────────────────────────────────────╮248│ goodbye │249│ hello │250╰───────────────────────────────────────────────────╯251252// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion253254// You have 2 subcommands (the 2 functions): goodbye and hello255```256257</div>258259Now check the help for the `hello` command:260261<div class="termy">262263```console264$ python main.py hello --help265266 Usage: main.py hello [OPTIONS] NAME267268╭─ Arguments ───────────────────────────────────────╮269│ * name TEXT [default: None] [required] │270╰───────────────────────────────────────────────────╯271╭─ Options ─────────────────────────────────────────╮272│ --help Show this message and exit. │273╰───────────────────────────────────────────────────╯274```275276</div>277278And now check the help for the `goodbye` command:279280<div class="termy">281282```console283$ python main.py goodbye --help284285 Usage: main.py goodbye [OPTIONS] NAME286287╭─ Arguments ───────────────────────────────────────╮288│ * name TEXT [default: None] [required] │289╰───────────────────────────────────────────────────╯290╭─ Options ─────────────────────────────────────────╮291│ --formal --no-formal [default: no-formal] │292│ --help Show this message │293│ and exit. │294╰───────────────────────────────────────────────────╯295296// Automatic --formal and --no-formal for the bool option 🎉297```298299</div>300301Now you can try out the new command line application:302303<div class="termy">304305```console306// Use it with the hello command307308$ python main.py hello Camila309310Hello Camila311312// And with the goodbye command313314$ python main.py goodbye Camila315316Bye Camila!317318// And with --formal319320$ python main.py goodbye --formal Camila321322Goodbye Ms. Camila. Have a good day.323```324325</div>326327### Recap328329In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.330331You do that with standard modern Python types.332333You don't have to learn a new syntax, the methods or classes of a specific library, etc.334

🔥 Remove support for Python 3.6 (#758)Sebastián Ramírezbefore adoption13404a0

line 335why, one step back
335Just standard **Python**.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 336–352why, one step back
336
337For example, for an `int`:338339```Python340total: int341```342343or for a `bool` flag:344345```Python346force: bool347```348349And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.350351**You get**: great editor support, including **completion** and **type checks** everywhere.352

✨ 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 353why, one step back
353**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.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 354–356why, one step back
354
355For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.356

✨ 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 back
357## Dependencies

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 358why, one step back
358

✨ 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 back
359**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>.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 360why, one step back
360

✨ 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 361why, one step back
361By default it also comes with extra standard dependencies:

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

line 362why, one step back
362

✨ 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 363–364why, one step back
363* <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.
364* <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.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 365–367why, one step back
365 * With `shellingham` you can just use `--install-completion`.
366 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.367

✨ 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 368–369why, one step back
368### `typer-slim`
369

🐛 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 370why, one step back
370If 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 371–377why, one step back
371
372When you install with:373374```bash375pip install typer376```377

🐛 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 378why, one step back
378...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 379–383why, one step back
379
380```bash381pip install "typer-slim[standard]"382```383

🐛 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 back
384The `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 385why, one step back
385

🐛 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 386why, one step back
386**Note**: The `typer` command is only included in the `typer` package.

🐛 Fix display of default value for Enum parameters inside of a list, include docs and tests (#473)Alexandre Sieirabefore adoption7202e86

lines 387–390why, one step back
387
388## License389390This project is licensed under the terms of the MIT license.