README.md
This file
Path
README.md
297 lines · 9.5 KiB · Markdown
Last changed by
💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e
History of this fileTake it
The walk stopped after 200 commits: lines older than e91c5a4 point at it.
:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 1–7why, one step back1<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">💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e
lines 8–9why, one step back8<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">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 10why, one step back10</a>💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e
lines 11–13why, one step back11<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>:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 14why, one step back14<a href="https://codecov.io/gh/tiangolo/typer" target="_blank">💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e
line 15why, one step back15 <img src="https://img.shields.io/codecov/c/github/tiangolo/typer?color=%2334D058" alt="Coverage">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 16–17why, one step back16</a>17<a href="https://pypi.org/project/typer" target="_blank">💄 Update badges and remove Travis (#145)Sebastián Ramírezbefore adoptionf67a47e
line 18why, one step back18 <img src="https://img.shields.io/pypi/v/typer?color=%2334D058&label=pypi%20package" alt="Package version">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 19–29why, one step back19</a>20</p>2122---2324**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>2526**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>2728---29✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 30why, one step back30Typer 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 3.6+ type hints.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 31–34why, one step back3132The key features are:3334* **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.:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a
line 35why, one step back35* **Easy to use**: It's easy to use for the final users. Automatic help, and automatic completion for all shells.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 36–37why, one step back36* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.37* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4
line 38why, one step back38* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 39why, one step back39:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
lines 40–47why, one step back40## FastAPI of CLIs4142<a href="https://fastapi.tiangolo.com" target="_blank"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" style="width: 20%;"></a>4344**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling.4546And it's intended to be the FastAPI of CLIs.47:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 48–51why, one step back48## Requirements4950Python 3.6+51:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 52why, one step back52**Typer** stands on the shoulders of a giant. Its only internal dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 53–97why, one step back5354## Installation5556<div class="termy">5758```console59$ pip install typer60---> 100%61Successfully installed typer62```6364</div>6566## Example6768### The absolute minimum6970* Create a file `main.py` with:7172```Python73import typer747576def main(name: str):77 typer.echo(f"Hello {name}")787980if __name__ == "__main__":81 typer.run(main)82```8384### Run it8586Run your application:8788<div class="termy">8990```console91// Run your application92$ python main.py9394// You get a nice error, you are missing NAME95Usage: main.py [OPTIONS] NAME96Try "main.py --help" for help.97:sparkles: Add support for Click 7.1.1 (#60)Sebastián Ramírezbefore adoption96b29e9
line 98why, one step back98Error: Missing argument 'NAME'.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 99–104why, one step back99100// You get a --help for free101$ python main.py --help102103Usage: main.py [OPTIONS] NAME104✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 105–107why, one step back105Arguments:106 NAME [required]107:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 108why, one step back108Options::pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
lines 109–111why, one step back109 --install-completion Install completion for the current shell.110 --show-completion Show completion for the current shell, to copy it or customize the installation.111 --help Show this message and exit.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 112why, one step back112✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 113why, one step back113// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 114why, one step back114:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 115–124why, one step back115// Now pass the NAME argument116$ python main.py Camila117118Hello Camila119120// It works! 🎉121```122123</div>124✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 125why, one step back125**Note**: auto-completion works when you create a Python package and run it with `--install-completion` or when you use <a href="https://typer.tiangolo.com/typer-cli/" class="internal-link" target="_blank">Typer CLI</a>.:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4
line 126why, one step back126:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 127–132why, one step back127## Example upgrade128129This was the simplest example possible.130131Now let's see one a bit more complex.132:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 133why, one step back133### An example with two subcommands:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 134–136why, one step back134135Modify the file `main.py`.136:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 137why, one step back137Create a `typer.Typer()` app, and create two subcommands with their parameters.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 138–165why, one step back138139```Python hl_lines="3 6 11 20"140import typer141142app = typer.Typer()143144145@app.command()146def hello(name: str):147 typer.echo(f"Hello {name}")148149150@app.command()151def goodbye(name: str, formal: bool = False):152 if formal:153 typer.echo(f"Goodbye Ms. {name}. Have a good day.")154 else:155 typer.echo(f"Bye {name}!")156157158if __name__ == "__main__":159 app()160```161162And that will:163164* Explicitly create a `typer.Typer` app.165 * The previous `typer.run` actually creates one implicitly for you.:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 166why, one step back166* Add two subcommands with `@app.command()`.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 167–179why, one step back167* Execute the `app()` itself, as if it was a function (instead of `typer.run`).168169### Run the upgraded example170171<div class="termy">172173```console174// Check the --help175$ python main.py --help176177Usage: main.py [OPTIONS] COMMAND [ARGS]...178179Options::pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
lines 180–182why, one step back180 --install-completion Install completion for the current shell.181 --show-completion Show completion for the current shell, to copy it or customize the installation.182 --help Show this message and exit.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 183–187why, one step back183184Commands:185 goodbye186 hello187:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 188why, one step back188// You have 2 subcommands (the 2 functions): goodbye and hello:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 189–195why, one step back189190// Now get the --help for hello191192$ python main.py hello --help193194Usage: main.py hello [OPTIONS] NAME195✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 196–198why, one step back196Arguments:197 NAME [required]198:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 199–207why, one step back199Options:200 --help Show this message and exit.201202// And now get the --help for goodbye203204$ python main.py goodbye --help205206Usage: main.py goodbye [OPTIONS] NAME207✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 208–210why, one step back208Arguments:209 NAME [required]210:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 211why, one step back211Options:✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
line 212why, one step back212 --formal / --no-formal [default: False]:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 213–239why, one step back213 --help Show this message and exit.214215// Automatic --formal and --no-formal for the bool option 🎉216217// And if you use it with the hello command218219$ python main.py hello Camila220221Hello Camila222223// And with the goodbye command224225$ python main.py goodbye Camila226227Bye Camila!228229// And with --formal230231$ python main.py goodbye --formal Camila232233Goodbye Ms. Camila. Have a good day.234```235236</div>237238### Recap239:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 240why, one step back240In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 241–259why, one step back241242You do that with standard modern Python types.243244You don't have to learn a new syntax, the methods or classes of a specific library, etc.245246Just standard **Python 3.6+**.247248For example, for an `int`:249250```Python251total: int252```253254or for a `bool` flag:255256```Python257force: bool258```259:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 260why, one step back260And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 261–263why, one step back261262**You get**: great editor support, including **completion** and **type checks** everywhere.263✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 264why, one step back264**Your users get**: automatic **`--help`**, **auto-completion** in their terminal (Bash, Zsh, Fish, PowerShell) when they install your package or when using <a href="https://typer.tiangolo.com/typer-cli/" class="internal-link" target="_blank">Typer CLI</a>.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 265why, one step back265:memo: Refactor docs with internal links #38Sebastián Ramírezbefore adoption8d6b201
line 266why, one step back266For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 267–269why, one step back267268## Optional Dependencies269:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
line 270why, one step back270Typer uses <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a> internally. That's the only dependency.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 271–273why, one step back271272But you can also install extras:273:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
line 274why, one step back274* <a href="https://pypi.org/project/colorama/" class="external-link" target="_blank"><code>colorama</code></a>: and Click will automatically use it to make sure your terminal's colors always work correctly, even in Windows.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 275why, one step back275 * Then you can use any tool you want to output your terminal's colors in all the systems, including the integrated `typer.style()` and `typer.secho()` (provided by Click).:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
line 276why, one step back276 * Or any other tool, e.g. <a href="https://pypi.org/project/wasabi/" class="external-link" target="_blank"><code>wasabi</code></a>, <a href="https://github.com/erikrose/blessings" class="external-link" target="_blank"><code>blessings</code></a>.:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a
lines 277–278why, one step back277* <a href="https://github.com/sarugaku/shellingham" class="external-link" target="_blank"><code>shellingham</code></a>: and Typer will automatically detect the current shell when installing completion.278 * With `shellingham` you can just use `--install-completion`.:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 279why, one step back279 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 280why, one step back280:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a
line 281why, one step back281You can install `typer` with `colorama` and `shellingham` with `pip install typer[all]`.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 282–288why, one step back282283## Other tools and plug-ins284285Click has many plug-ins available that you can use. And there are many tools that help with command line applications that you can use as well, even if they are not related to Typer or Click.286287For example:288:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
lines 289–290why, one step back289* <a href="https://github.com/click-contrib/click-spinner" class="external-link" target="_blank"><code>click-spinner</code></a>: to show the user that you are loading data. A Click plug-in.290 * There are several other Click plug-ins at <a href="https://github.com/click-contrib" class="external-link" target="_blank">click-contrib</a> that you can explore.:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 291why, one step back291* <a href="https://pypi.org/project/tabulate/" class="external-link" target="_blank"><code>tabulate</code></a>: to automatically display tabular data nicely. Independent of Click or Typer.:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4
line 292why, one step back292* <a href="https://github.com/tqdm/tqdm" class="external-link" target="_blank"><code>tqdm</code></a>: a fast, extensible progress bar, alternative to Typer's own `typer.progressbar()`.:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
lines 293–297why, one step back293* etc... you can re-use many of the great available tools for building CLIs.294295## License296297This project is licensed under the terms of the MIT license.