Skip to content
Myr

:sparkles: Docs for Typer CLI (#64)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* :sparkles: Add docs for Typer CLI * :memo: Update references to Typer CLI in docs * :wrench: Update tests to check files and linting before tests, to error out early * :memo: Sync README.md with docs/index.md

authored by Sebastián Ramírez · committed by GitHub · parent 87fe78c · Browse files · 8 files+491 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+491 added−15 removed

  • other:+491 added−15 removed

8 files+491 added−15 removed

modified:docs/tutorial/first-steps.md+39 added
434 unmodified lines
435436And we'll use ***CLI option*** to refer to those *CLI parameters* that depend on a name that starts with `--` (like `--lastname`). That are **optional** by default.437438We will use ***CLI parameter*** to refer to both, *CLI arguments* and *CLI options*.439440## **Typer CLI**441442Now that you know the basics of **Typer**, you might want to install and use [Typer CLI](../typer-cli.md){.internal-link target=_blank}.443444**Typer CLI** is a tool to run your **Typer** scripts giving you ✨ auto completion ✨ in your terminal.445446As an alternative to running with Python:447448<div class="termy">449450```console451$ python main.py452453Hello World454```455456</div>457458You can run with **Typer CLI**:459460<div class="termy">461462```console463$ typer main.py run464465Hello World466```467468</div>469470...and it will give you auto completion in your terminal when you hit <kbd>TAB</kbd> for all your code.471472So you can use it to have auto completion as you continue with the tutorial.473474!!! tip475 Your CLI application built with **Typer** won't need [Typer CLI](../typer-cli.md){.internal-link target=_blank} to have auto completion once you create a Python package.476477 But for short scripts and for learning, before creating a Python package, it might be useful.
modified:docs/tutorial/index.md+2 added-2 removed
modified:docs/features.md+4 added-4 removed
modified:docs/index.md+6 added-3 removed
32 unmodified lines
33* **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.34* **Easy to use**: It's easy to use for the final users. Automatic help, and (optional) automatic completion for all shells.35* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.36* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.37* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups subcommands, with options and arguments.37* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.3839## Requirements4041Python 3.6+
55 unmodified lines
97 --install-completion Install completion for the current shell.98 --show-completion Show completion for the current shell, to copy it or customize the installation.99 --help Show this message and exit.100101// You get ✨ auto completion ✨ for free, installed with --install-completion101// When you create a package you get ✨ auto completion ✨ for free, installed with --install-completion102103// Now pass the NAME argument104$ python main.py Camila105
3 unmodified lines
109```110111</div>112113**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>.114115## Example upgrade116117This was the simplest example possible.118
123 unmodified lines
242And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.243244**You get**: great editor support, including **completion** and **type checks** everywhere.245244**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).246**Your users get**: automatic **`--help`**, (optional) **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>.247248For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.249250## Optional Dependencies
17 unmodified lines
268269* <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.270 * 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.271* <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.272* <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()`.273* etc... you can re-use many of the great available tools for building CLIs.274275## License276
1 unmodified line
added:docs/typer-cli.md+431 added
1# Typer CLI23<p align="center">4 <em>Run <strong>Typer</strong> scripts with completion, without having to create a package, using <strong>Typer CLI</strong>.</em>5</p>6<p align="center">7<a href="https://travis-ci.com/tiangolo/typer-cli" target="_blank">8 <img src="https://travis-ci.com/tiangolo/typer-cli.svg?branch=master" alt="Build Status">9</a>10<a href="https://codecov.io/gh/tiangolo/typer-cli" target="_blank">11 <img src="https://codecov.io/gh/tiangolo/typer-cli/branch/master/graph/badge.svg" alt="Coverage">12</a>13<a href="https://pypi.org/project/typer-cli" target="_blank">14 <img src="https://badge.fury.io/py/typer-cli.svg" alt="Package version">15</a>16</p>1718There is an optional utility tool called **Typer CLI**, additional to **Typer** itself.1920It's main feature is to provide ✨ completion ✨ in the Terminal for your own small programs built with **Typer**.2122...without you having to create a complete installable Python package.2324It's probably most useful if you have a small custom Python script using **Typer** (maybe as part of some project), for some small tasks, and it's not complex/important enough to create a whole installable Python package for it (something to be installed with `pip`).2526In that case, you can install **Typer CLI**, and run your program with the `typer` command in your Terminal, and it will provide completion for your script.2728You can also use **Typer CLI** to generate Markdown documentation for your own **Typer** programs 📝.2930---3132**Documentation**: <a href="https://typer.tiangolo.com/typer-cli/" target="_blank">https://typer.tiangolo.com/typer-cli/</a>3334**Source Code for Typer CLI**: <a href="https://github.com/tiangolo/typer-cli" target="_blank">https://github.com/tiangolo/typer-cli</a>3536---3738## **Typer** or **Typer CLI**3940**Typer** is a library for building CLIs (Command Line Interface applications).4142You use **Typer** in your Python scripts. Like in:4344```Python45import typer464748def main():49 typer.echo("Hello World")505152if __name__ == "__main__":53 typer.run(main)54```5556**Typer CLI** is a command line application to run simple programs created with **Typer**, with completion in your terminal 🚀.5758You use **Typer CLI** in your terminal, to run your scripts (as an alternative to calling `python` directly). Like in:5960<div class="termy">6162```console63$ typer my_script.py run6465Hello World66```6768</div>6970But you never import anything from **Typer CLI** in your own scripts.7172## Usage7374### Install7576Install **Typer CLI**:7778<div class="termy">7980```console81$ python -m pip install typer-cli82---> 100%83Successfully installed typer-cli84```8586</div>8788That creates a `typer` command you can call in your terminal, much like `python`, `git`, or `echo`.8990You can then install completion for it:9192<div class="termy">9394```console95$ typer --install-completion9697zsh completion installed in /home/user/.bashrc.98Completion will take effect once you restart the terminal.99```100101</div>102103### Sample script104105Let's say you have a script that uses **Typer** in `my_custom_script.py`:106107```Python108import typer109110app = typer.Typer()111112113@app.command()114def hello(name: str = None):115 if name:116 typer.echo(f"Hello {name}")117 else:118 typer.echo("Hello World!")119120121@app.command()122def bye(name: str = None):123 if name:124 typer.echo(f"Bye {name}")125 else:126 typer.echo("Goodbye!")127128129if __name__ == "__main__":130 app()131```132133For it to work, you would also install **Typer**:134135<div class="termy">136137```console138$ python -m pip install typer139---> 100%140Successfully installed typer141```142143</div>144145### Run with Python146147Then you could run your script with normal Python:148149<div class="termy">150151```console152$ python my_custom_script.py hello153154Hello World!155156$ python my_custom_script.py hello --name Camila157158Hello Camila!159160$ python my_custom_script.py bye --name Camila161162Bye Camila163```164165</div>166167There's nothing wrong with using Python directly to run it. And, in fact, if some other code or program uses your script, that would probably be the best way to do it.168169⛔️ But in your terminal, you won't get completion when hitting <kbd>TAB</kbd> for any of the subcommands or options, like `hello`, `bye`, and `--name`.170171### Run with **Typer CLI**172173Here's where **Typer CLI** is useful.174175You can also run the same script with the `typer` command you get after installing `typer-cli`:176177<div class="termy">178179```console180$ typer my_custom_script.py run hello181182Hello World!183184$ typer my_custom_script.py run hello --name Camila185186Hello Camila!187188$ typer my_custom_script.py run bye --name Camila189190Bye Camila191```192193</div>194195* Instead of using `python` directly you use the `typer` command.196* After the name of the file, add the subcommand `run`.197198✔️ If you installed completion for **Typer CLI** (for the `typer` command) as described above, when you hit <kbd>TAB</kbd> you will have ✨ completion for everything ✨, including all the subcommands and options of your script, like `hello`, `bye`, and `--name` 🚀.199200## If main201202Because **Typer CLI** won't use the block with:203204```Python205if __name__ == "__main__":206 app()207```208209...you can also remove it if you are calling that script only with **Typer CLI** (using the `typer` command).210211## Run other files212213**Typer CLI** can run any script with **Typer**, but the script doesn't even have to use **Typer** at all.214215**Typer CLI** could even run a file with a function that could be used with `typer.run()`, even if the script doesn't use `typer.run()` or anything else.216217For example, a file `main.py` like this will still work:218219```Python220def main(name: str = "World"):221 """222 Say hi to someone, by default to the World.223 """224 print(f"Hello {name}")225```226227Then you can call it with:228229<div class="termy">230231```console232$ typer main.py run --help233Usage: typer run [OPTIONS]234235 Say hi to someone, by default to the World.236237Options:238 --name TEXT239 --help Show this message and exit.240241$ typer main.py run --name Camila242243Hello Camila244```245246</div>247248And it will also have completion for things like the `--name` *CLI Option*.249250## Run a package or module251252Instead of a file path you can pass a module (possibly in a package) to import.253254For example:255256<div class="termy">257258```console259$ typer my_package.main run --help260Usage: typer run [OPTIONS]261262Options:263 --name TEXT264 --help Show this message and exit.265266$ typer my_package.main run --name Camila267268Hello Camila269```270271</div>272273## Options274275You can specify one of the following **CLI Options**:276277* `--app`: the name of the variable with a `Typer()` object to run as the main app.278* `--func`: the name of the variable with a function that would be used with `typer.run()`.279280### Defaults281282When your run a script with the **Typer CLI** (the `typer` command) it will use the app from the following priority:283284* An app object from the `--app` *CLI Option*.285* A function to convert to a **Typer** app from `--func` *CLI Option* (like when using `typer.run()`).286* A **Typer** app in a variable with a name of `app`, `cli`, or `main`.287* The first **Typer** app available in the file, with any name.288* A function in a variable with a name of `main`, `cli`, or `app`.289* The first function in the file, with any name.290291## Generate docs292293**Typer CLI** can also generate Markdown documentation for your **Typer** application.294295### Sample script with docs296297For example, you could have a script like:298299```Python300{!./src/commands/help/tutorial001.py!}301```302303### Generate docs with Typer CLI304305Then you could generate docs for it with **Typer CLI**.306307You can use the subcommand `utils`.308309And then the subcommand `docs`.310311<div class="termy">312313```console314$ typer some_script.py utils docs315```316317</div>318319**Options**:320321* `--name TEXT`: The name of the CLI program to use in docs.322* `--output FILE`: An output file to write docs to, like README.md.323324For example:325326<div class="termy">327328```console329$ typer my_package.main utils docs --name awesome-cli --output README.md330331Docs saved to: README.md332```333334</div>335336### Sample docs output337338For example, for the previous script, the generated docs would look like:339340---341342## `awesome-cli`343344Awesome CLI user manager.345346**Usage**:347348```console349$ awesome-cli [OPTIONS] COMMAND [ARGS]...350```351352**Options**:353354* `--install-completion`: Install completion for the current shell.355* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.356* `--help`: Show this message and exit.357358**Commands**:359360* `create`: Create a new user with USERNAME.361* `delete`: Delete a user with USERNAME.362* `delete-all`: Delete ALL users in the database.363* `init`: Initialize the users database.364365## `awesome-cli create`366367Create a new user with USERNAME.368369**Usage**:370371```console372$ awesome-cli create [OPTIONS] USERNAME373```374375**Options**:376377* `--help`: Show this message and exit.378379## `awesome-cli delete`380381Delete a user with USERNAME.382383If --force is not used, will ask for confirmation.384385**Usage**:386387```console388$ awesome-cli delete [OPTIONS] USERNAME389```390391**Options**:392393* `--force / --no-force`: Force deletion without confirmation. [required]394* `--help`: Show this message and exit.395396## `awesome-cli delete-all`397398Delete ALL users in the database.399400If --force is not used, will ask for confirmation.401402**Usage**:403404```console405$ awesome-cli delete-all [OPTIONS]406```407408**Options**:409410* `--force / --no-force`: Force deletion without confirmation. [required]411* `--help`: Show this message and exit.412413## `awesome-cli init`414415Initialize the users database.416417**Usage**:418419```console420$ awesome-cli init [OPTIONS]421```422423**Options**:424425* `--help`: Show this message and exit.426427---428429## License430431**Typer CLI**, the same as **Typer**, is licensed under the terms of the MIT license.
modified:scripts/test.sh+2 added-3 removed
modified:mkdocs.yml+1 added
63 unmodified lines
64 - Ask with Prompt: 'tutorial/prompt.md'65 - Progress Bar: 'tutorial/progressbar.md'66 - CLI Application Directory: 'tutorial/app-dir.md'67 - Launching Applications: 'tutorial/launch.md'68 - Typer CLI - completion for small scripts: 'typer-cli.md'69 - Alternatives, Inspiration and Comparisons: 'alternatives.md'70 - Help Typer - Get Help: 'help-typer.md'71 - Development - Contributing: 'contributing.md'72 - Release Notes: release-notes.md
40 unmodified lines
modified:README.md+6 added-3 removed
32 unmodified lines
33* **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.34* **Easy to use**: It's easy to use for the final users. Automatic help, and (optional) automatic completion for all shells.35* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.36* **Start simple**: The simplest example adds only 2 lines of code to your app: **1 import, 1 function call**.37* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups subcommands, with options and arguments.37* **Grow large**: Grow in complexity as much as you want, create arbitrarily complex trees of commands and groups of subcommands, with options and arguments.3839## Requirements4041Python 3.6+
55 unmodified lines
97 --install-completion Install completion for the current shell.98 --show-completion Show completion for the current shell, to copy it or customize the installation.99 --help Show this message and exit.100101// You get ✨ auto completion ✨ for free, installed with --install-completion101// When you create a package you get ✨ auto completion ✨ for free, installed with --install-completion102103// Now pass the NAME argument104$ python main.py Camila105
3 unmodified lines
109```110111</div>112113**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>.114115## Example upgrade116117This was the simplest example possible.118
123 unmodified lines
242And similarly for **files**, **paths**, **enums** (choices), etc. And there are tools to create **groups of subcommands**, add metadata, extra **validation**, etc.243244**You get**: great editor support, including **completion** and **type checks** everywhere.245244**Your users get**: automatic **`--help`**, (optional) **autocompletion** in their terminal (Bash, Zsh, Fish, PowerShell).246**Your users get**: automatic **`--help`**, (optional) **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>.247248For a more complete example including more features, see the <a href="https://typer.tiangolo.com/tutorial/">Tutorial - User Guide</a>.249250## Optional Dependencies
17 unmodified lines
268269* <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.270 * 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.271* <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.272* <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()`.273* etc... you can re-use many of the great available tools for building CLIs.274275## License276
1 unmodified line