README.md
This file
Path
README.md
294 lines · 9.3 KiB · Markdown
Last changed by
✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
History of this fileTake it
:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 1why, one step back1<p align="center">:bug: Fix link in README for GitHubSebastián Ramírezbefore adoptionc820ba0
line 2why, one step back2 <a href="https://typer.tiangolo.com"><img src="https://typer.tiangolo.com/img/logo-margin/logo-margin-vector.svg" alt="Typer"></a>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 3–4why, one step back3</p>4<p align="center">:memo: Update sub-header, short descriptionSebastián Ramírezbefore adoptionbc931c1
line 5why, one step back5 <em>Typer, build great CLIs. Easy to code. Based on Python type hints.</em>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 6–7why, one step back6</p>7<p align="center">:memo: Update READMESebastián Ramírezbefore adoption08d36b1
lines 8–9why, one step back8<a href="https://travis-ci.com/tiangolo/typer" target="_blank">9 <img src="https://travis-ci.com/tiangolo/typer.svg?branch=master" alt="Build Status">:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 10–11why, one step back10</a>11<a href="https://codecov.io/gh/tiangolo/typer" target="_blank">:art: Update testing coverage badge (#78)Sebastián Ramírezbefore adoptionb7aa9e4
line 12why, one step back12 <img src="https://img.shields.io/codecov/c/github/tiangolo/typer" alt="Coverage">:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 13–17why, one step back13</a>14<a href="https://pypi.org/project/typer" target="_blank">15 <img src="https://badge.fury.io/py/typer.svg" alt="Package version">16</a>17</p>18:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 19why, one step back19---20:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 21why, one step back21**Documentation**: <a href="https://typer.tiangolo.com" target="_blank">https://typer.tiangolo.com</a>22:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 23–26why, one step back23**Source Code**: <a href="https://github.com/tiangolo/typer" target="_blank">https://github.com/tiangolo/typer</a>2425---26✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 27why, one step back27Typer 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: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 28–31why, one step back2829The key features are:3031* **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 32why, one step back32* **Easy to use**: It's easy to use for the final users. Automatic help, and automatic completion for all shells.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 33–34why, one step back33* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.34* **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 35why, one step back35* **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: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 36why, one step back36:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
lines 37–44why, one step back37## FastAPI of CLIs3839<a href="https://fastapi.tiangolo.com" target="_blank"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" style="width: 20%;"></a>4041**Typer** is <a href="https://fastapi.tiangolo.com" class="external-link" target="_blank">FastAPI</a>'s little sibling.4243And it's intended to be the FastAPI of CLIs.44:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 45–48why, one step back45## Requirements4647Python 3.6+48:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 49why, one step back49**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: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 50–52why, one step back5051## Installation52:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 53why, one step back53<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 54why, one step back54:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 55–58why, one step back55```console56$ pip install typer57---> 100%58Successfully installed typer:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 59why, one step back59```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 60why, one step back60:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 61why, one step back61</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 62–84why, one step back6263## Example6465### The absolute minimum6667* Create a file `main.py` with:6869```Python70import typer717273def main(name: str):74 typer.echo(f"Hello {name}")757677if __name__ == "__main__":78 typer.run(main)79```8081### Run it8283Run your application:84:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 85why, one step back85<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 86why, one step back86:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 87–89why, one step back87```console88// Run your application89$ python main.py:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 90why, one step back90:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 91why, one step back91// You get a nice error, you are missing NAME:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 92–94why, one step back92Usage: main.py [OPTIONS] NAME93Try "main.py --help" for help.94:sparkles: Add support for Click 7.1.1 (#60)Sebastián Ramírezbefore adoption96b29e9
line 95why, one step back95Error: Missing argument 'NAME'.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 96why, one step back96:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 97–98why, one step back97// You get a --help for free98$ python main.py --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 99why, one step back99:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 100why, one step back100Usage: main.py [OPTIONS] NAME:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 101why, one step back101✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 102–104why, one step back102Arguments:103 NAME [required]104:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 105why, one step back105Options::pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
lines 106–108why, one step back106 --install-completion Install completion for the current shell.107 --show-completion Show completion for the current shell, to copy it or customize the installation.108 --help Show this message and exit.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 109why, one step back109✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 110why, one step back110// 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 111why, one step back111:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 112–114why, one step back112// Now pass the NAME argument113$ python main.py Camila114:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 115–116why, one step back115Hello Camila116:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 117why, one step back117// It works! 🎉:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 118why, one step back118```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 119why, one step back119:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 120why, one step back120</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 121why, one step back121✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 122why, one step back122**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 123why, one step back123:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 124–125why, one step back124## Example upgrade125:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 126why, one step back126This was the simplest example possible.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 127–129why, one step back127128Now let's see one a bit more complex.129:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 130why, one step back130### An example with two subcommands:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 131–133why, one step back131132Modify the file `main.py`.133:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 134why, one step back134Create a `typer.Typer()` app, and create two subcommands with their parameters.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 135–162why, one step back135136```Python hl_lines="3 6 11 20"137import typer138139app = typer.Typer()140141142@app.command()143def hello(name: str):144 typer.echo(f"Hello {name}")145146147@app.command()148def goodbye(name: str, formal: bool = False):149 if formal:150 typer.echo(f"Goodbye Ms. {name}. Have a good day.")151 else:152 typer.echo(f"Bye {name}!")153154155if __name__ == "__main__":156 app()157```158159And that will:160161* Explicitly create a `typer.Typer` app.162 * The previous `typer.run` actually creates one implicitly for you.:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 163why, one step back163* Add two subcommands with `@app.command()`.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 164–167why, one step back164* Execute the `app()` itself, as if it was a function (instead of `typer.run`).165166### Run the upgraded example167:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 168why, one step back168<div class="termy">:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 169why, one step back169:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
lines 170–172why, one step back170```console171// Check the --help172$ python main.py --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 173–176why, one step back173174Usage: main.py [OPTIONS] COMMAND [ARGS]...175176Options::pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
lines 177–179why, one step back177 --install-completion Install completion for the current shell.178 --show-completion Show completion for the current shell, to copy it or customize the installation.179 --help Show this message and exit.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 180–184why, one step back180181Commands:182 goodbye183 hello184:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 185why, one step back185// You have 2 subcommands (the 2 functions): goodbye and hello:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 186why, one step back186:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 187why, one step back187// Now get the --help for hello:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 188why, one step back188:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 189why, one step back189$ python main.py hello --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 190–192why, one step back190191Usage: main.py hello [OPTIONS] NAME192✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 193–195why, one step back193Arguments:194 NAME [required]195:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 196–198why, one step back196Options:197 --help Show this message and exit.198:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 199why, one step back199// And now get the --help for goodbye:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 200why, one step back200:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 201why, one step back201$ python main.py goodbye --help:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 202–204why, one step back202203Usage: main.py goodbye [OPTIONS] NAME204✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
lines 205–207why, one step back205Arguments:206 NAME [required]207:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 208why, one step back208Options:✨ Add support for help for CLI arguments (#123)Sebastián Ramírezbefore adoption15380dd
line 209why, one step back209 --formal / --no-formal [default: False]:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 210–211why, one step back210 --help Show this message and exit.211:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 212why, one step back212// Automatic --formal and --no-formal for the bool option 🎉:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 213why, one step back213:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 214why, one step back214// And if you use it with the hello command:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 215why, one step back215:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 216why, one step back216$ python main.py hello Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 217–219why, one step back217218Hello Camila219:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 220why, one step back220// And with the goodbye command:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 221why, one step back221:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 222why, one step back222$ python main.py goodbye Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 223–225why, one step back223224Bye Camila!225:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 226why, one step back226// And with --formal:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 227why, one step back227:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 228why, one step back228$ python main.py goodbye --formal Camila:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 229–231why, one step back229230Goodbye Ms. Camila. Have a good day.231```:memo: Fix GitHub markdown formattingSebastián Ramírezbefore adoptione91c5a4
line 232why, one step back232:memo: Update README.md from docs/index.mdSebastián Ramírezbefore adoption30c8e5f
line 233why, one step back233</div>:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 234–236why, one step back234235### Recap236:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f
line 237why, one step back237In summary, you declare **once** the types of parameters (*CLI arguments* and *CLI options*) as function parameters.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 238–256why, one step back238239You do that with standard modern Python types.240241You don't have to learn a new syntax, the methods or classes of a specific library, etc.242243Just standard **Python 3.6+**.244245For example, for an `int`:246247```Python248total: int249```250251or for a `bool` flag:252253```Python254force: bool255```256:pencil: Update docs with references to --install-completionSebastián Ramírezbefore adoption9b44a9f
line 257why, one step back257And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 258–260why, one step back258259**You get**: great editor support, including **completion** and **type checks** everywhere.260✏ Fix typos in main page/README (#103)Evan E. Bairdbefore adoption2bc3b9a
line 261why, one step back261**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: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 262why, one step back262:memo: Refactor docs with internal links #38Sebastián Ramírezbefore adoption8d6b201
line 263why, one step back263For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 264–266why, one step back264265## Optional Dependencies266:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
line 267why, one step back267Typer uses <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a> internally. That's the only dependency.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 268why, one step back268:memo: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 269why, one step back269But you can also install extras::memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 270why, one step back270:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
line 271why, one step back271* <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: Update README with latest docsSebastián Ramírezbefore adoptionea35b8d
line 272why, one step back272 * 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 273why, one step back273 * 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 274–275why, one step back274* <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.275 * 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 276why, one step back276 * Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 277why, one step back277:sparkles: Re-implement completion, fixes, add PowerShell support (#66)Sebastián Ramírezbefore adoption1d3337a
line 278why, one step back278You can install `typer` with `colorama` and `shellingham` with `pip install typer[all]`.:memo: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
lines 279–285why, one step back279280## Other tools and plug-ins281282Click 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.283284For example:285:art: Tweak external links' format #36Sebastián Ramírezbefore adoption38c19f0
lines 286–287why, one step back286* <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.287 * 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 288why, one step back288* <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 289why, one step back289* <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: Add README and CONTRIBUTINGSebastián Ramírezbefore adoptionddbf033
line 290why, one step back290* etc... you can re-use many of the great available tools for building CLIs.:tada: First commit, Typer scaffoldingSebastián Ramírezbefore adoption0cfc112
lines 291–294291292## License293294This project is licensed under the terms of the MIT license.