: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
Provenance
- Commit
b36c75b- Tree
7a3ff6b- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
Sebastián Ramírez <tiangolo@gmail.com>- Parent
416aba4
5 files changed+394 added−0 removed
added:docs/src/subcommands/tutorial002/main.py+36 added
1+import typer2+3+app = typer.Typer()4+items_app = typer.Typer()5+app.add_typer(items_app, name="items")6+users_app = typer.Typer()7+app.add_typer(users_app, name="users")8+9+10+@items_app.command("create")11+def items_create(item: str):12+ typer.echo(f"Creating item: {item}")13+14+15+@items_app.command("delete")16+def items_delete(item: str):17+ typer.echo(f"Deleting item: {item}")18+19+20+@items_app.command("sell")21+def items_sell(item: str):22+ typer.echo(f"Selling item: {item}")23+24+25+@users_app.command("create")26+def users_create(user_name: str):27+ typer.echo(f"Creating user: {user_name}")28+29+30+@users_app.command("delete")31+def users_delete(user_name: str):32+ typer.echo(f"Deleting user: {user_name}")33+34+35+if __name__ == "__main__":36+ app()added:docs/tutorial/subcommands/add-typer.md+167 added
1+We'll start with the core ideas.2+3+To add a `typer.Typer()` app inside of another.4+5+## Manage items6+7+Let's imagine that you are creating a *CLI program* to manage items in some distant land.8+9+It could be in an `items.py` file with this:10+11+```Python hl_lines=""12+{!./src/subcommands/tutorial001/items.py!}13+```14+15+And you would use it like:16+17+<div class="termy">18+19+```console20+$ python items.py create Wand21+22+Creating item: Wand23+```24+25+</div>26+27+## Manage users28+29+But then you realize that you also have to manage users from your *CLI app*.30+31+It could be a file `users.py` with something like:32+33+```Python hl_lines=""34+{!./src/subcommands/tutorial001/users.py!}35+```36+37+And you would use it like:38+39+<div class="termy">40+41+```console42+$ python users.py create Camila43+44+Creating user: Camila45+```46+47+</div>48+49+## Put them together50+51+Both parts are similar. In fact, `items.py` and `users.py` both have commands `create` and `delete`.52+53+But we need them to be part of the same *CLI program*.54+55+In this case, as with `git remote`, we can put them together as subcommands in another `typer.Typer()` *CLI program*.56+57+Now create a `main.py` with:58+59+```Python hl_lines="3 4 7 8"60+{!./src/subcommands/tutorial001/main.py!}61+```62+63+Here's what we do in `main.py`:64+65+* 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.69+70+And now your *CLI program* has 2 commands:71+72+* `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`.74+75+Check it:76+77+<div class="termy">78+79+```console80+// Check the help81+$ python main.py --help82+83+Usage: main.py [OPTIONS] COMMAND [ARGS]...84+85+Options: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.89+90+Commands:91+ items92+ users93+```94+95+</div>96+97+Now you have a *CLI program* with commands `items` and `users`, and they in turn have their own commands (subcommands).98+99+Let's check the `items` command:100+101+<div class="termy">102+103+```console104+// Check the help for items105+$ python main.py items --help106+107+// It shows its own commands (subcommands): create, delete, sell108+Usage: main.py items [OPTIONS] COMMAND [ARGS]...109+110+Options:111+ --help Show this message and exit.112+113+Commands:114+ create115+ delete116+ sell117+118+// Try it119+$ python main.py items create Wand120+121+Creating item: Wand122+123+$ python main.py items sell Vase124+125+Selling item: Vase126+```127+128+</div>129+130+!!! tip131+ Notice that we are still calling `$ python main.py` but now we are using the command `items`.132+133+And now check the command `users`, with all its subcommands:134+135+<div class="termy">136+137+```console138+$ python main.py users --help139+140+Usage: main.py users [OPTIONS] COMMAND [ARGS]...141+142+Options:143+ --help Show this message and exit.144+145+Commands:146+ create147+ delete148+149+// Try it150+$ python main.py users create Camila151+152+Creating user: Camila153+```154+155+</div>156+157+## Recap158+159+That's the core idea.160+161+You can just create `typer.Typer()` apps and add them inside one another.162+163+And you can do that with any levels of commands that you want.164+165+Do you need sub-sub-sub-subcommands? Go ahead, create all the `typer.Typer()`s you need and put them together with `app.add_typer()`.166+167+In 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
123456789101112131415161718192021222324252627282930313233343536373839You read before how to create a program with <a href="https://typer.tiangolo.com/tutorial/commands/" target="_blank">Commands</a>.
Now we'll see how to create a *CLI program* with commands that have their own subcommands. Also known as command groups.
For example, the *CLI program* `git` has a command `remote`.
But `git remote`, in turn, has its own subcommands, like `add`:
<div class="termy">
```console// git remote alone shows the current remote repositories$ git remote
origin
// Use -v to make it verbose and show more info$ git remote -v
origin git@github.com:yourusername/typer.git (fetch)origin git@github.com:yourusername/typer.git (push)
// git remote add takes 2 CLI arguments, a name and URL$ git remote add upstream https://github.com/tiangolo/typer.git
// Doesn't output anything, but now you have another remote repository called upstream
// Now check again$ git remote -v
origin git@github.com:yourusername/typer.git (fetch)origin git@github.com:yourusername/typer.git (push)upstream https://github.com/tiangolo/typer.git (fetch)upstream https://github.com/tiangolo/typer.git (push)```
</div>
In the next sections we'll see how to create subcommands like these.added:docs/tutorial/subcommands/single-file.md+148 added
1+In some cases, it's possible that your application code needs to live on a single file.2+3+You can still use the same ideas:4+5+```Python hl_lines=""6+{!./src/subcommands/tutorial002/main.py!}7+```8+9+There are several things to notice here...10+11+## Apps at the top12+13+First, you can create `typer.Typer()` objects and add them to another one at the top.14+15+It doesn't have to be done after creating the subcommands:16+17+```Python hl_lines="4 5 6 7"18+{!./src/subcommands/tutorial002/main.py!}19+```20+21+You can add the commands (subcommands) to each `typer.Typer()` app later and it will still work.22+23+## Function names24+25+As 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.26+27+So we use longer names:28+29+```Python hl_lines="11 16 21 26 31"30+{!./src/subcommands/tutorial002/main.py!}31+```32+33+## Command name34+35+We are naming the functions with longer names so that they don't overwrite each other.36+37+But we still want the subcommands to be `create`, `delete`, etc.38+39+To call them like:40+41+<div class="termy">42+43+```console44+// We want this ✔️45+$ python main.py items create46+```47+48+</div>49+50+instead of:51+52+<div class="termy">53+54+```console55+// We don't want this ⛔️56+$ python main.py items items-create57+```58+59+</div>60+61+So we pass the name we want to use for each subcommand as the function argument to the decorator:62+63+```Python hl_lines="10 15 20 25 30"64+{!./src/subcommands/tutorial002/main.py!}65+```66+67+## Check it68+69+It still works the same:70+71+72+<div class="termy">73+74+```console75+// Check the help76+$ python main.py --help77+78+Usage: main.py [OPTIONS] COMMAND [ARGS]...79+80+Options: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.85+86+Commands:87+ items88+ users89+```90+91+</div>92+93+Check the `items` command:94+95+96+<div class="termy">97+98+```console99+// Check the help for items100+$ python main.py items --help101+102+// It shows its own commands (subcommands): create, delete, sell103+Usage: main.py items [OPTIONS] COMMAND [ARGS]...104+105+Options:106+ --help Show this message and exit.107+108+Commands:109+ create110+ delete111+ sell112+113+// Try it114+$ python main.py items create Wand115+116+Creating item: Wand117+118+$ python main.py items sell Vase119+120+Selling item: Vase121+```122+123+</div>124+125+And the same for the `users` command:126+127+128+<div class="termy">129+130+```console131+$ python main.py users --help132+133+Usage: main.py users [OPTIONS] COMMAND [ARGS]...134+135+Options:136+ --help Show this message and exit.137+138+Commands:139+ create140+ delete141+142+// Try it143+$ python main.py users create Camila144+145+Creating user: Camila146+```147+148+</div>modified:mkdocs.yml+4 added
45 unmodified lines46474849505152535455565738 unmodified lines45 unmodified lines - DateTime: 'tutorial/parameter-types/datetime.md' - Enum - Choices: 'tutorial/parameter-types/enum.md' - Path: 'tutorial/parameter-types/path.md' - File: 'tutorial/parameter-types/file.md' - SubCommands: - SubCommands Intro: 'tutorial/subcommands/index.md' - Add Typer: 'tutorial/subcommands/add-typer.md' - SubCommands in a Single File: 'tutorial/subcommands/single-file.md' - Ask with Prompt: 'tutorial/prompt.md' - Progress Bar: 'tutorial/progressbar.md' - CLI Application Directory: 'tutorial/app-dir.md' - Launching Applications: 'tutorial/launch.md'38 unmodified lines