Skip to content
Myr

index.md

This file

Path

docs/index.md

381 lines · 13.5 KiB · Markdown

Last changed by

🐛 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

History of this file

Take it

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 1–13why, one step back
1<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 back
14<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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

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

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

line 29why, one step back
29Typer 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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 30why, one step back
30

✨ 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 31–32why, one step back
31It's also a command line tool to run scripts, automatically converting them to CLI applications.
32

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 33–39why, one step back
33The key features are:
3435* **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.36* **Easy to use**: It's easy to use for the final users. Automatic help, and automatic completion for all shells.37* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.38* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.39* **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 40why, one step back
40* **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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 41–43why, one step back
41
42## FastAPI of CLIs43

✨ 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 44why, one step back
44**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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 45–50why, one step back
45
46## Installation4748<div class="termy">4950```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 51why, one step back
51$ pip install typer

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 54–64why, one step back
54```
5556</div>5758## Example5960### The absolute minimum6162* Create a file `main.py` with:6364```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 65–121why, one step back
65def main(name: str):
66 print(f"Hello {name}")67```6869This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.7071### Run it7273Run your application with the `typer` command:7475<div class="termy">7677```console78// Run your application79$ typer main.py run8081// You get a nice error, you are missing NAME82Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME83Try 'typer [PATH_OR_MODULE] run --help' for help.84╭─ Error ───────────────────────────────────────────╮85│ Missing argument 'NAME'. │86╰───────────────────────────────────────────────────╯878889// You get a --help for free90$ typer main.py run --help9192Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME9394Run the provided Typer app.9596╭─ Arguments ───────────────────────────────────────╮97│ * name TEXT [default: None] [required] |98╰───────────────────────────────────────────────────╯99╭─ Options ─────────────────────────────────────────╮100│ --help Show this message and exit. │101╰───────────────────────────────────────────────────╯102103// Now pass the NAME argument104$ typer main.py run Camila105106Hello Camila107108// It works! 🎉109```110111</div>112113This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.114115**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.116117## Use Typer in your code118119Now let's start using Typer in your own code, update `main.py` with:120121```Python

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 122–132why, one step back
122import typer
123124125def main(name: str):126 print(f"Hello {name}")127128129if __name__ == "__main__":130 typer.run(main)131```132

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 134–141why, one step back
134
135<div class="termy">136137```console138// Run your application139$ python main.py140141// 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 142–146why, one step back
142Usage: main.py [OPTIONS] NAME
143Try 'main.py --help' for help.144╭─ Error ───────────────────────────────────────────╮145│ Missing argument 'NAME'. │146╰───────────────────────────────────────────────────╯

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 147–151why, one step back
147
148149// You get a --help for free150$ python main.py --help151

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

line 152why, one step back
152Usage: main.py [OPTIONS] NAME

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 153why, one step back
153

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

lines 154–159why, one step back
154╭─ Arguments ───────────────────────────────────────╮
155│ * name TEXT [default: None] [required] |156╰───────────────────────────────────────────────────╯157╭─ Options ─────────────────────────────────────────╮158│ --help Show this message and exit. │159╰───────────────────────────────────────────────────╯

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 160–170why, one step back
160
161// Now pass the NAME argument162$ python main.py Camila163164Hello Camila165166// It works! 🎉167```168169</div>170

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 172–223why, one step back
172
173## Example upgrade174175This was the simplest example possible.176177Now let's see one a bit more complex.178179### An example with two subcommands180181Modify the file `main.py`.182183Create a `typer.Typer()` app, and create two subcommands with their parameters.184185```Python hl_lines="3 6 11 20"186import typer187188app = typer.Typer()189190191@app.command()192def hello(name: str):193 print(f"Hello {name}")194195196@app.command()197def goodbye(name: str, formal: bool = False):198 if formal:199 print(f"Goodbye Ms. {name}. Have a good day.")200 else:201 print(f"Bye {name}!")202203204if __name__ == "__main__":205 app()206```207208And that will:209210* Explicitly create a `typer.Typer` app.211 * The previous `typer.run` actually creates one implicitly for you.212* Add two subcommands with `@app.command()`.213* Execute the `app()` itself, as if it was a function (instead of `typer.run`).214215### Run the upgraded example216217Check the new help:218219<div class="termy">220221```console222$ python main.py --help223

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

line 224why, one step back
224 Usage: main.py [OPTIONS] COMMAND [ARGS]...

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 225why, one step back
225

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

lines 226–244why, one step back
226╭─ Options ─────────────────────────────────────────╮
227│ --install-completion Install completion │228│ for the current │229│ shell. │230│ --show-completion Show completion for │231│ the current shell, │232│ to copy it or │233│ customize the │234│ installation. │235│ --help Show this message │236│ and exit. │237╰───────────────────────────────────────────────────╯238╭─ Commands ────────────────────────────────────────╮239│ goodbye │240│ hello │241╰───────────────────────────────────────────────────╯242243// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion244

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 245–256why, one step back
245// You have 2 subcommands (the 2 functions): goodbye and hello
246```247248</div>249250Now check the help for the `hello` command:251252<div class="termy">253254```console255$ python main.py hello --help256

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

lines 257–264why, one step back
257 Usage: main.py hello [OPTIONS] NAME
258259╭─ Arguments ───────────────────────────────────────╮260│ * name TEXT [default: None] [required] │261╰───────────────────────────────────────────────────╯262╭─ Options ─────────────────────────────────────────╮263│ --help Show this message and exit. │264╰───────────────────────────────────────────────────╯

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 265–275why, one step back
265```
266267</div>268269And now check the help for the `goodbye` command:270271<div class="termy">272273```console274$ python main.py goodbye --help275

📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)Sebastián Ramírezbefore adoptione7c3467

lines 276–285why, one step back
276 Usage: main.py goodbye [OPTIONS] NAME
277278╭─ Arguments ───────────────────────────────────────╮279│ * name TEXT [default: None] [required] │280╰───────────────────────────────────────────────────╯281╭─ Options ─────────────────────────────────────────╮282│ --formal --no-formal [default: no-formal] │283│ --help Show this message │284│ and exit. │285╰───────────────────────────────────────────────────╯

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 286–325why, one step back
286
287// Automatic --formal and --no-formal for the bool option 🎉288```289290</div>291292Now you can try out the new command line application:293294<div class="termy">295296```console297// Use it with the hello command298299$ python main.py hello Camila300301Hello Camila302303// And with the goodbye command304305$ python main.py goodbye Camila306307Bye Camila!308309// And with --formal310311$ python main.py goodbye --formal Camila312313Goodbye Ms. Camila. Have a good day.314```315316</div>317318### Recap319320In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.321322You do that with standard modern Python types.323324You don't have to learn a new syntax, the methods or classes of a specific library, etc.325

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

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 327–343why, one step back
327
328For example, for an `int`:329330```Python331total: int332```333334or for a `bool` flag:335336```Python337force: bool338```339340And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.341342**You get**: great editor support, including **completion** and **type checks** everywhere.343

✨ 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 344why, one step back
344**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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

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

✨ 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 348why, one step back
348## Dependencies

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 349why, one step back
349

✨ 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 350why, one step back
350**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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 351why, one step back
351

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

line 353why, one step back
353

✨ 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 354–355why, one step back
354* <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.
355* <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 broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 356–358why, one step back
356 * With `shellingham` you can just use `--install-completion`.
357 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.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

lines 359–360why, one step back
359### `typer-slim`
360

🐛 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 361why, one step back
361If 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 362–368why, one step back
362
363When you install with:364365```bash366pip install typer367```368

🐛 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 369why, one step back
369...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 370–374why, one step back
370
371```bash372pip install "typer-slim[standard]"373```374

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

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

✏ Fix broken plac link (#275)Michael Gieldabefore adoptionddeebf1

lines 378–381why, one step back
378
379## License380381This project is licensed under the terms of the MIT license.