Skip to content
Myr

typer-cli.md

This file

Path

docs/typer-cli.md

431 lines · 9.4 KiB · Markdown

Last changed by

:memo: Sync Typer CLI docs, Coverage badgeSebastián Ramírezbefore adoptiona44c9da

History of this file

Take it

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

lines 1–10why, one step back
1# Typer CLI
23<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">

:memo: Sync Typer CLI docs, Coverage badgeSebastián Ramírezbefore adoptiona44c9da

line 11why, one step back
11 <img src="https://img.shields.io/codecov/c/github/tiangolo/typer-cli" alt="Coverage">

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

lines 12–274why, one step back
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## Options274

:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)Sebastián Ramírezbefore adoption5a6379f

line 275why, one step back
275You can specify one of the following **CLI options**:

:sparkles: Docs for Typer CLI (#64)Sebastián Ramírezbefore adoption96731c4

lines 276–431why, one step back
276
277* `--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.