Skip to content
Myr

README.md

This file

Path

README.md

384 lines · 13.6 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 5728b2a point at it.

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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">

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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">

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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">

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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>

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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.

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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.

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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.

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 46–48why, one step back
46
47## Installation48

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

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 51–53why, one step back
51<div class="termy">
5253```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 54why, one step back
54$ pip install typer

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 57–67why, one step back
57```
5859</div>6061## Example6263### The absolute minimum6465* Create a file `main.py` with:6667```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 68–124why, one step back
68def main(name: str):
69 print(f"Hello {name}")70```7172This script doesn't even use Typer internally. But you can use the `typer` command to run it as a CLI application.7374### Run it7576Run your application with the `typer` command:7778<div class="termy">7980```console81// Run your application82$ typer main.py run8384// You get a nice error, you are missing NAME85Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME86Try 'typer [PATH_OR_MODULE] run --help' for help.87╭─ Error ───────────────────────────────────────────╮88│ Missing argument 'NAME'. │89╰───────────────────────────────────────────────────╯909192// You get a --help for free93$ typer main.py run --help9495Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME9697Run the provided Typer app.9899╭─ Arguments ───────────────────────────────────────╮100│ * name TEXT [default: None] [required] |101╰───────────────────────────────────────────────────╯102╭─ Options ─────────────────────────────────────────╮103│ --help Show this message and exit. │104╰───────────────────────────────────────────────────╯105106// Now pass the NAME argument107$ typer main.py run Camila108109Hello Camila110111// It works! 🎉112```113114</div>115116This is the simplest use case, not even using Typer internally, but it can already be quite useful for simple scripts.117118**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use the `typer` command.119120## Use Typer in your code121122Now let's start using Typer in your own code, update `main.py` with:123124```Python

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 125–135why, one step back
125import typer
126127128def main(name: str):129 print(f"Hello {name}")130131132if __name__ == "__main__":133 typer.run(main)134```135

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 137–173why, one step back
137
138<div class="termy">139140```console141// Run your application142$ python main.py143144// You get a nice error, you are missing NAME145Usage: main.py [OPTIONS] NAME146Try 'main.py --help' for help.147╭─ Error ───────────────────────────────────────────╮148│ Missing argument 'NAME'. │149╰───────────────────────────────────────────────────╯150151152// You get a --help for free153$ python main.py --help154155Usage: main.py [OPTIONS] NAME156157╭─ Arguments ───────────────────────────────────────╮158│ * name TEXT [default: None] [required] |159╰───────────────────────────────────────────────────╯160╭─ Options ─────────────────────────────────────────╮161│ --help Show this message and exit. │162╰───────────────────────────────────────────────────╯163164// Now pass the NAME argument165$ python main.py Camila166167Hello Camila168169// It works! 🎉170```171172</div>173

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 175–328why, one step back
175
176## Example upgrade177178This was the simplest example possible.179180Now let's see one a bit more complex.181182### An example with two subcommands183184Modify the file `main.py`.185186Create a `typer.Typer()` app, and create two subcommands with their parameters.187188```Python hl_lines="3 6 11 20"189import typer190191app = typer.Typer()192193194@app.command()195def hello(name: str):196 print(f"Hello {name}")197198199@app.command()200def goodbye(name: str, formal: bool = False):201 if formal:202 print(f"Goodbye Ms. {name}. Have a good day.")203 else:204 print(f"Bye {name}!")205206207if __name__ == "__main__":208 app()209```210211And that will:212213* Explicitly create a `typer.Typer` app.214 * The previous `typer.run` actually creates one implicitly for you.215* Add two subcommands with `@app.command()`.216* Execute the `app()` itself, as if it was a function (instead of `typer.run`).217218### Run the upgraded example219220Check the new help:221222<div class="termy">223224```console225$ python main.py --help226227 Usage: main.py [OPTIONS] COMMAND [ARGS]...228229╭─ Options ─────────────────────────────────────────╮230│ --install-completion Install completion │231│ for the current │232│ shell. │233│ --show-completion Show completion for │234│ the current shell, │235│ to copy it or │236│ customize the │237│ installation. │238│ --help Show this message │239│ and exit. │240╰───────────────────────────────────────────────────╯241╭─ Commands ────────────────────────────────────────╮242│ goodbye │243│ hello │244╰───────────────────────────────────────────────────╯245246// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion247248// You have 2 subcommands (the 2 functions): goodbye and hello249```250251</div>252253Now check the help for the `hello` command:254255<div class="termy">256257```console258$ python main.py hello --help259260 Usage: main.py hello [OPTIONS] NAME261262╭─ Arguments ───────────────────────────────────────╮263│ * name TEXT [default: None] [required] │264╰───────────────────────────────────────────────────╯265╭─ Options ─────────────────────────────────────────╮266│ --help Show this message and exit. │267╰───────────────────────────────────────────────────╯268```269270</div>271272And now check the help for the `goodbye` command:273274<div class="termy">275276```console277$ python main.py goodbye --help278279 Usage: main.py goodbye [OPTIONS] NAME280281╭─ Arguments ───────────────────────────────────────╮282│ * name TEXT [default: None] [required] │283╰───────────────────────────────────────────────────╯284╭─ Options ─────────────────────────────────────────╮285│ --formal --no-formal [default: no-formal] │286│ --help Show this message │287│ and exit. │288╰───────────────────────────────────────────────────╯289290// Automatic --formal and --no-formal for the bool option 🎉291```292293</div>294295Now you can try out the new command line application:296297<div class="termy">298299```console300// Use it with the hello command301302$ python main.py hello Camila303304Hello Camila305306// And with the goodbye command307308$ python main.py goodbye Camila309310Bye Camila!311312// And with --formal313314$ python main.py goodbye --formal Camila315316Goodbye Ms. Camila. Have a good day.317```318319</div>320321### Recap322323In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.324325You do that with standard modern Python types.326327You don't have to learn a new syntax, the methods or classes of a specific library, etc.328

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

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 330–346why, one step back
330
331For example, for an `int`:332333```Python334total: int335```336337or for a `bool` flag:338339```Python340force: bool341```342343And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.344345**You get**: great editor support, including **completion** and **type checks** everywhere.346

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 348–350why, one step back
348
349For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.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## Dependencies

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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
353**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>.

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

line 355why, one step back
355By default it also comes with extra standard dependencies:

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

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

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

🐛 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 364why, one step back
364If 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 365–371why, one step back
365
366When you install with:367368```bash369pip install typer370```371

🐛 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 372why, one step back
372...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 373–377why, one step back
373
374```bash375pip install "typer-slim[standard]"376```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
378The `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 379why, one step back
379

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

🔖 Release version 0.9.3Sebastián Ramírezbefore adoption5728b2a

lines 381–384why, one step back
381
382## License383384This project is licensed under the terms of the MIT license.