Skip to content
Myr

:memo: Add docs for callbacks and one or multiple commands

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

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent 13819bf · Browse files · 8 files+372 added−12 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+372 added−12 removed

  • other:+372 added−12 removed

8 files+372 added−12 removed

added:docs/src/commands/callback/tutorial001.py+36 added
1import typer23app = typer.Typer()4state = {"verbose": False}567@app.command()8def create(username: str):9 if state["verbose"]:10 typer.echo("About to create a user")11 typer.echo(f"Creating user: {username}")12 if state["verbose"]:13 typer.echo("Just created a user")141516@app.command()17def delete(username: str):18 if state["verbose"]:19 typer.echo("About to delete a user")20 typer.echo(f"Deleting user: {username}")21 if state["verbose"]:22 typer.echo("Just deleted a user")232425@app.callback()26def main(verbose: bool = False):27 """28 Manage users in the awesome CLI app.29 """30 if verbose:31 typer.echo("Will write verbose output")32 state["verbose"] = True333435if __name__ == "__main__":36 app()
added:docs/src/commands/callback/tutorial002.py+17 added
1import typer234def callback():5 typer.echo("Running a command")678app = typer.Typer(callback=callback)91011@app.command()12def create(name: str):13 typer.echo(f"Creating user: {name}")141516if __name__ == "__main__":17 app()
added:docs/src/commands/callback/tutorial003.py+22 added
1import typer234def callback():5 typer.echo("Running a command")678app = typer.Typer(callback=callback)91011@app.callback()12def new_callback():13 typer.echo("Override callback, running a command")141516@app.command()17def create(name: str):18 typer.echo(f"Creating user: {name}")192021if __name__ == "__main__":22 app()
added:docs/src/commands/callback/tutorial004.py+23 added
1import typer23app = typer.Typer()456@app.callback()7def callback():8 """9 Manage users CLI app.1011 Use it with the create command.12 13 A new user with the given NAME will be created.14 """151617@app.command()18def create(name: str):19 typer.echo(f"Creating user: {name}")202122if __name__ == "__main__":23 app()
added:docs/src/commands/one_or_multiple/tutorial001.py+17 added
1import typer23app = typer.Typer()456@app.command()7def create():8 typer.echo("Creating user: Hiro Hamada")91011@app.callback()12def callback():13 pass141516if __name__ == "__main__":17 app()
added:docs/src/commands/one_or_multiple/tutorial002.py+21 added
1import typer23app = typer.Typer()456@app.command()7def create():8 typer.echo("Creating user: Hiro Hamada")91011@app.callback()12def callback():13 """14 Creates a single user Hiro Hamada.1516 In the next version it will create 5 users more.17 """181920if __name__ == "__main__":21 app()
added:docs/tutorial/commands/callback.md+164 added
1When you create an `app = typer.Typer()` it works as a group of commands.23And you can create multiple commands with it.45Each of those commands can have their own *CLI parameters*.67But as those *CLI parameters* are handled by each of those commands, they don't allow us to create *CLI parameters* for the main CLI application itself.89But we can use `@app.callback()` for that.1011It's very similar to `@app.command()`, but it declares the *CLI parameters* for the main CLI application (before the commands):1213```Python hl_lines="25 26 27 28 29 30 31 32"14{!./src/commands/callback/tutorial001.py!}15```1617Here we create a `callback` with a `--verbose` *CLI option*.1819!!! tip20 After getting the `--verbose` flag, we modify a global `state`, and we use it in the other commands.2122 There are other ways to achieve the same, but this will suffice for this example.2324And as we added a docstring to the callback function, by default it will be extracted and used as the help text.2526Check it:2728<div class="termy">2930```console31// Check the help32$ python main.py --help3334// Notice the main help text, extracted from the callback function: "Manage users in the awesome CLI app."35Usage: main.py [OPTIONS] COMMAND [ARGS]...3637 Manage users in the awesome CLI app.3839Options:40 --verbose / --no-verbose41 --install-completion Install completion for the current shell.42 --show-completion Show completion for the current shell, to copy it or customize the installation.43 --help Show this message and exit.4445Commands:46 create47 delete4849// Check the new top level CLI option --verbose5051// Try it normally52$ python main.py create Camila5354Creating user: Camila5556// And now with --verbose57$ python main.py --verbose create Camila5859Will write verbose output60About to create a user61Creating user: Camila62Just created a user6364// Notice that --verbose belongs to the callback, it has to go before create or delete ⛔️65$ python main.py create --verbose Camila6667Usage: main.py create [OPTIONS] USERNAME68Try "main.py create --help" for help.6970Error: no such option: --verbose71```7273</div>7475## Adding a callback on creation7677It's also possible to add a callback when creating the `typer.Typer()` app:7879```Python hl_lines="4 5 8"80{!./src/commands/callback/tutorial002.py!}81```8283That achieves the same as with `@app.callback()`.8485Check it:8687<div class="termy">8889```console90$ python main.py create Camila9192Running a command93Creating user: Camila94```9596</div>9798## Overriding a callback99100If you added a callback when creating the `typer.Typer()` app, it's possible to override it with `@app.callback()`:101102```Python hl_lines="11 12 13"103{!./src/commands/callback/tutorial003.py!}104```105106Now `new_callback()` will be the one used.107108Check it:109110<div class="termy">111112```console113$ python main.py create Camila114115// Notice that the message is the one from new_callback()116Override callback, running a command117Creating user: Camila118```119120</div>121122## Adding a callback only for documentation123124You can also add a callback just to add the documentation in the docstring.125126It can be convenient especially if you have several lines of text, as the indentation will be automatically handled for you:127128```Python hl_lines="8 9 10 11 12 13 14 15 16"129{!./src/commands/callback/tutorial004.py!}130```131132Now the callback will be used mainly to extract the docstring for the help text.133134Check it:135136<div class="termy">137138```console139$ python main.py --help140141// Notice all the help text extracted from the callback docstring142Usage: main.py [OPTIONS] COMMAND [ARGS]...143144 Manage users CLI app.145146 Use it with the create command.147148 A new user with the given NAME will be created.149150Options:151 --install-completion Install completion for the current shell.152 --show-completion Show completion for the current shell, to copy it or customize the installation.153 --help Show this message and exit.154155Commands:156 create157158// And it just works as normally159$ python main.py create Camila160161Creating user: Camila162```163164</div>
modified:docs/tutorial/commands/one-or-multiple.md+72 added-12 removed