Skip to content
Myr

README.md

This file

Path

README.md

382 lines · 13.5 KiB · Markdown

Last changed by

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

History of this file

Take it

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

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

lines 2–3why, one step back
2 <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg#only-light" alt="Typer"></a>
3

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 4–8why, one step back
4</p>
5<p align="center">6 <em>Typer, build great CLIs. Easy to code. Based on Python type hints.</em>7</p>8<p align="center">

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

lines 9–10why, one step back
9<a href="https://github.com/fastapi/typer/actions?query=workflow%3ATest" target="_blank">
10 <img src="https://github.com/fastapi/typer/workflows/Test/badge.svg" alt="Test">

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

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

lines 12–13why, one step back
12<a href="https://github.com/fastapi/typer/actions?query=workflow%3APublish" target="_blank">
13 <img src="https://github.com/fastapi/typer/workflows/Publish/badge.svg" alt="Publish">

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

📝 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://coverage-badge.samuelcolvin.workers.dev/redirect/fastapi/typer" target="_blank">
16 <img src="https://coverage-badge.samuelcolvin.workers.dev/fastapi/typer.svg" alt="Coverage">

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 17–25why, one step back
17<a href="https://pypi.org/project/typer" target="_blank">
18 <img src="https://img.shields.io/pypi/v/typer?color=%2334D058&label=pypi%20package" alt="Package version">19</a>20</p>2122---2324**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>25

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 27–29why, one step back
27
28---29

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

line 31why, one step back
31

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 42–44why, one step back
42
43## FastAPI of CLIs44

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 46–51why, one step back
46
47## Installation4849<div class="termy">5051```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 52why, one step back
52$ pip install typer

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 55–65why, one step back
55```
5657</div>5859## Example6061### The absolute minimum6263* Create a file `main.py` with:6465```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 66–122why, one step back
66def main(name: str):
67 print(f"Hello {name}")68```6970This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.7172### Run it7374Run your application with the `typer` command:7576<div class="termy">7778```console79// Run your application80$ typer main.py run8182// You get a nice error, you are missing NAME83Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME84Try 'typer [PATH_OR_MODULE] run --help' for help.85╭─ Error ───────────────────────────────────────────╮86│ Missing argument 'NAME'. │87╰───────────────────────────────────────────────────╯888990// You get a --help for free91$ typer main.py run --help9293Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME9495Run the provided Typer app.9697╭─ Arguments ───────────────────────────────────────╮98│ * name TEXT [default: None] [required] |99╰───────────────────────────────────────────────────╯100╭─ Options ─────────────────────────────────────────╮101│ --help Show this message and exit. │102╰───────────────────────────────────────────────────╯103104// Now pass the NAME argument105$ typer main.py run Camila106107Hello Camila108109// It works! 🎉110```111112</div>113114This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.115116**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.117118## Use Typer in your code119120Now let's start using Typer in your own code, update `main.py` with:121122```Python

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 123–133why, one step back
123import typer
124125126def main(name: str):127 print(f"Hello {name}")128129130if __name__ == "__main__":131 typer.run(main)132```133

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 148–152why, one step back
148
149150// You get a --help for free151$ python main.py --help152

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

line 154why, one step back
154

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 161–171why, one step back
161
162// Now pass the NAME argument163$ python main.py Camila164165Hello Camila166167// It works! 🎉168```169170</div>171

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 173–224why, one step back
173
174## Example upgrade175176This was the simplest example possible.177178Now let's see one a bit more complex.179180### An example with two subcommands181182Modify the file `main.py`.183184Create a `typer.Typer()` app, and create two subcommands with their parameters.185186```Python hl_lines="3 6 11 20"187import typer188189app = typer.Typer()190191192@app.command()193def hello(name: str):194 print(f"Hello {name}")195196197@app.command()198def goodbye(name: str, formal: bool = False):199 if formal:200 print(f"Goodbye Ms. {name}. Have a good day.")201 else:202 print(f"Bye {name}!")203204205if __name__ == "__main__":206 app()207```208209And that will:210211* Explicitly create a `typer.Typer` app.212 * The previous `typer.run` actually creates one implicitly for you.213* Add two subcommands with `@app.command()`.214* Execute the `app()` itself, as if it was a function (instead of `typer.run`).215216### Run the upgraded example217218Check the new help:219220<div class="termy">221222```console223$ python main.py --help224

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

line 226why, one step back
226

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 246–257why, one step back
246// You have 2 subcommands (the 2 functions): goodbye and hello
247```248249</div>250251Now check the help for the `hello` command:252253<div class="termy">254255```console256$ python main.py hello --help257

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 266–276why, one step back
266```
267268</div>269270And now check the help for the `goodbye` command:271272<div class="termy">273274```console275$ python main.py goodbye --help276

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 287–326why, one step back
287
288// Automatic --formal and --no-formal for the bool option 🎉289```290291</div>292293Now you can try out the new command line application:294295<div class="termy">296297```console298// Use it with the hello command299300$ python main.py hello Camila301302Hello Camila303304// And with the goodbye command305306$ python main.py goodbye Camila307308Bye Camila!309310// And with --formal311312$ python main.py goodbye --formal Camila313314Goodbye Ms. Camila. Have a good day.315```316317</div>318319### Recap320321In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.322323You do that with standard modern Python types.324325You don't have to learn a new syntax, the methods or classes of a specific library, etc.326

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 328–344why, one step back
328
329For example, for an `int`:330331```Python332total: int333```334335or for a `bool` flag:336337```Python338force: bool339```340341And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.342343**You get**: great editor support, including **completion** and **type checks** everywhere.344

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

line 350why, one step back
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 back
351**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>.

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

line 354why, one step back
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

lines 355–356why, one step back
355* <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.
356* <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.

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

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

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

🐛 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 362why, one step back
362If 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 363–369why, one step back
363
364When you install with:365366```bash367pip install typer368```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
370...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 371–375why, one step back
371
372```bash373pip install "typer-slim[standard]"374```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 back
376The `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 377why, one step back
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**Note**: The `typer` command is only included in the `typer` package.

🔧 Add GitHub Discussion templates for questions (#541)Sebastián Ramírezbefore adoptionef1081e

lines 379–382why, one step back
379
380## License381382This project is licensed under the terms of the MIT license.