Skip to content
Myr

:sparkles: Add SubCommands sections

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 416aba4 · Browse files · 5 files+394 added−0 removed
This commit

State

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

Diff stats

5 files+394 added−0 removed

  • other:+394 added−0 removed

5 files+394 added−0 removed

added:docs/src/subcommands/tutorial002/main.py+36 added
1import typer23app = typer.Typer()4items_app = typer.Typer()5app.add_typer(items_app, name="items")6users_app = typer.Typer()7app.add_typer(users_app, name="users")8910@items_app.command("create")11def items_create(item: str):12 typer.echo(f"Creating item: {item}")131415@items_app.command("delete")16def items_delete(item: str):17 typer.echo(f"Deleting item: {item}")181920@items_app.command("sell")21def items_sell(item: str):22 typer.echo(f"Selling item: {item}")232425@users_app.command("create")26def users_create(user_name: str):27 typer.echo(f"Creating user: {user_name}")282930@users_app.command("delete")31def users_delete(user_name: str):32 typer.echo(f"Deleting user: {user_name}")333435if __name__ == "__main__":36 app()
added:docs/tutorial/subcommands/add-typer.md+167 added
1We'll start with the core ideas.23To add a `typer.Typer()` app inside of another.45## Manage items67Let's imagine that you are creating a *CLI program* to manage items in some distant land.89It could be in an `items.py` file with this:1011```Python hl_lines=""12{!./src/subcommands/tutorial001/items.py!}13```1415And you would use it like:1617<div class="termy">1819```console20$ python items.py create Wand2122Creating item: Wand23```2425</div>2627## Manage users2829But then you realize that you also have to manage users from your *CLI app*.3031It could be a file `users.py` with something like:3233```Python hl_lines=""34{!./src/subcommands/tutorial001/users.py!}35```3637And you would use it like:3839<div class="termy">4041```console42$ python users.py create Camila4344Creating user: Camila45```4647</div>4849## Put them together5051Both parts are similar. In fact, `items.py` and `users.py` both have commands `create` and `delete`.5253But we need them to be part of the same *CLI program*.5455In this case, as with `git remote`, we can put them together as subcommands in another `typer.Typer()` *CLI program*.5657Now create a `main.py` with:5859```Python hl_lines="3 4 7 8"60{!./src/subcommands/tutorial001/main.py!}61```6263Here's what we do in `main.py`:6465* Import the other Python modules (the files `users.py` and `items.py`).66* Create the main `typer.Typer()` application.67* Use `app.add_typer()` to include the `app` from `items.py` and `users.py`, each of those 2 was also created with `typer.Typer()`.68* Define a `name` with the command that will be used for each of these "sub-Typers" to group their own commands.6970And now your *CLI program* has 2 commands:7172* `users`: with all of the commands (subcommands) in the `app` from `users.py`.73* `items` with all the commands (subcommands) in the `app` from `items.py`.7475Check it:7677<div class="termy">7879```console80// Check the help81$ python main.py --help8283Usage: main.py [OPTIONS] COMMAND [ARGS]...8485Options:86 --install-completion Install completion for the current shell.87 --show-completion Show completion for the current shell, to copy it or customize the installation.88 --help Show this message and exit.8990Commands:91 items92 users93```9495</div>9697Now you have a *CLI program* with commands `items` and `users`, and they in turn have their own commands (subcommands).9899Let's check the `items` command:100101<div class="termy">102103```console104// Check the help for items105$ python main.py items --help106107// It shows its own commands (subcommands): create, delete, sell108Usage: main.py items [OPTIONS] COMMAND [ARGS]...109110Options:111 --help Show this message and exit.112113Commands:114 create115 delete116 sell117118// Try it119$ python main.py items create Wand120121Creating item: Wand122123$ python main.py items sell Vase124125Selling item: Vase126```127128</div>129130!!! tip131 Notice that we are still calling `$ python main.py` but now we are using the command `items`.132133And now check the command `users`, with all its subcommands:134135<div class="termy">136137```console138$ python main.py users --help139140Usage: main.py users [OPTIONS] COMMAND [ARGS]...141142Options:143 --help Show this message and exit.144145Commands:146 create147 delete148149// Try it150$ python main.py users create Camila151152Creating user: Camila153```154155</div>156157## Recap158159That's the core idea.160161You can just create `typer.Typer()` apps and add them inside one another.162163And you can do that with any levels of commands that you want.164165Do you need sub-sub-sub-subcommands? Go ahead, create all the `typer.Typer()`s you need and put them together with `app.add_typer()`.166167In the next sections we'll update this with more features, but you already have the core idea.
added:docs/tutorial/subcommands/index.md+39 added
added:docs/tutorial/subcommands/single-file.md+148 added
1In some cases, it's possible that your application code needs to live on a single file.23You can still use the same ideas:45```Python hl_lines=""6{!./src/subcommands/tutorial002/main.py!}7```89There are several things to notice here...1011## Apps at the top1213First, you can create `typer.Typer()` objects and add them to another one at the top.1415It doesn't have to be done after creating the subcommands:1617```Python hl_lines="4 5 6 7"18{!./src/subcommands/tutorial002/main.py!}19```2021You can add the commands (subcommands) to each `typer.Typer()` app later and it will still work.2223## Function names2425As you now have subcommands like `create` for `users` and for `items`, you can no longer call the functions with just the name, like `def create()`, because they would overwrite each other.2627So we use longer names:2829```Python hl_lines="11 16 21 26 31"30{!./src/subcommands/tutorial002/main.py!}31```3233## Command name3435We are naming the functions with longer names so that they don't overwrite each other.3637But we still want the subcommands to be `create`, `delete`, etc.3839To call them like:4041<div class="termy">4243```console44// We want this ✔️45$ python main.py items create46```4748</div>4950instead of:5152<div class="termy">5354```console55// We don't want this ⛔️56$ python main.py items items-create57```5859</div>6061So we pass the name we want to use for each subcommand as the function argument to the decorator:6263```Python hl_lines="10 15 20 25 30"64{!./src/subcommands/tutorial002/main.py!}65```6667## Check it6869It still works the same:707172<div class="termy">7374```console75// Check the help76$ python main.py --help7778Usage: main.py [OPTIONS] COMMAND [ARGS]...7980Options:81 --install-completion Install completion for the current shell.82 --show-completion Show completion for the current shell, to copy it or83 customize the installation.84 --help Show this message and exit.8586Commands:87 items88 users89```9091</div>9293Check the `items` command:949596<div class="termy">9798```console99// Check the help for items100$ python main.py items --help101102// It shows its own commands (subcommands): create, delete, sell103Usage: main.py items [OPTIONS] COMMAND [ARGS]...104105Options:106 --help Show this message and exit.107108Commands:109 create110 delete111 sell112113// Try it114$ python main.py items create Wand115116Creating item: Wand117118$ python main.py items sell Vase119120Selling item: Vase121```122123</div>124125And the same for the `users` command:126127128<div class="termy">129130```console131$ python main.py users --help132133Usage: main.py users [OPTIONS] COMMAND [ARGS]...134135Options:136 --help Show this message and exit.137138Commands:139 create140 delete141142// Try it143$ python main.py users create Camila144145Creating user: Camila146```147148</div>
modified:mkdocs.yml+4 added