Skip to content
Myr

:sparkles: Add docs and examples for subcommands

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 f3c97dc · Browse files · 22 files+1299 added−1 removed
This commit

State

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

Diff stats

22 files+1299 added−1 removed

  • other:+1299 added−1 removed

22 files+1299 added−1 removed

added:docs/src/subcommands/callback_override/tutorial001.py+20 added
1import typer23app = typer.Typer()45users_app = typer.Typer()6app.add_typer(users_app, name="users")789@users_app.callback()10def users_callback():11 typer.echo("Running a users command")121314@users_app.command()15def create(name: str):16 typer.echo(f"Creating user: {name}")171819if __name__ == "__main__":20 app()
added:docs/src/subcommands/callback_override/tutorial002.py+20 added
1import typer23app = typer.Typer()456def users_callback():7 typer.echo("Running a users command")8910users_app = typer.Typer(callback=users_callback)11app.add_typer(users_app, name="users")121314@users_app.command()15def create(name: str):16 typer.echo(f"Creating user: {name}")171819if __name__ == "__main__":20 app()
added:docs/src/subcommands/callback_override/tutorial003.py+25 added
1import typer23app = typer.Typer()456def default_callback():7 typer.echo("Running a users command")8910users_app = typer.Typer(callback=default_callback)11app.add_typer(users_app, name="users")121314@users_app.callback()15def user_callback():16 typer.echo("Callback override, running users command")171819@users_app.command()20def create(name: str):21 typer.echo(f"Creating user: {name}")222324if __name__ == "__main__":25 app()
added:docs/src/subcommands/callback_override/tutorial004.py+31 added
1import typer23app = typer.Typer()456def default_callback():7 typer.echo("Running a users command")8910users_app = typer.Typer(callback=default_callback)111213def callback_for_add_typer():14 typer.echo("I have the high land! Running users command")151617app.add_typer(users_app, name="users", callback=callback_for_add_typer)181920@users_app.callback()21def user_callback():22 typer.echo("Callback override, running users command")232425@users_app.command()26def create(name: str):27 typer.echo(f"Creating user: {name}")282930if __name__ == "__main__":31 app()
added:docs/src/subcommands/name_help/tutorial001.py+15 added
1import typer23app = typer.Typer()45users_app = typer.Typer()6app.add_typer(users_app, name="users", help="Manage users in the app.")789@users_app.command()10def create(name: str):11 typer.echo(f"Creating user: {name}")121314if __name__ == "__main__":15 app()
added:docs/src/subcommands/name_help/tutorial002.py+22 added
1import typer23app = typer.Typer()45users_app = typer.Typer()6app.add_typer(users_app)789@users_app.callback()10def users():11 """12 Manage users in the app.13 """141516@users_app.command()17def create(name: str):18 typer.echo(f"Creating user: {name}")192021if __name__ == "__main__":22 app()
added:docs/src/subcommands/name_help/tutorial003.py+22 added
1import typer23app = typer.Typer()456def users():7 """8 Manage users in the app.9 """101112users_app = typer.Typer(callback=users)13app.add_typer(users_app)141516@users_app.command()17def create(name: str):18 typer.echo(f"Creating user: {name}")192021if __name__ == "__main__":22 app()
added:docs/src/subcommands/name_help/tutorial004.py+29 added
1import typer23app = typer.Typer()456def old_callback():7 """8 Old callback help.9 """101112users_app = typer.Typer(callback=old_callback)13app.add_typer(users_app)141516@users_app.callback()17def users():18 """19 Manage users in the app.20 """212223@users_app.command()24def create(name: str):25 typer.echo(f"Creating user: {name}")262728if __name__ == "__main__":29 app()
added:docs/src/subcommands/name_help/tutorial005.py+37 added
1import typer23app = typer.Typer()456def old_callback():7 """8 Old callback help.9 """101112users_app = typer.Typer(callback=old_callback)131415def new_users():16 """17 I have the highland! Create some users.18 """192021app.add_typer(users_app, callback=new_users)222324@users_app.callback()25def users():26 """27 Manage users in the app.28 """293031@users_app.command()32def create(name: str):33 typer.echo(f"Creating user: {name}")343536if __name__ == "__main__":37 app()
added:docs/src/subcommands/name_help/tutorial006.py+37 added
1import typer23app = typer.Typer()456def old_callback():7 """8 Old callback help.9 """101112users_app = typer.Typer(callback=old_callback, name="exp-users", help="Explicit help.")131415def new_users():16 """17 I have the highland! Create some users.18 """192021app.add_typer(users_app, callback=new_users)222324@users_app.callback()25def users():26 """27 Manage users in the app.28 """293031@users_app.command()32def create(name: str):33 typer.echo(f"Creating user: {name}")343536if __name__ == "__main__":37 app()
added:docs/src/subcommands/name_help/tutorial007.py+37 added
1import typer23app = typer.Typer()456def old_callback():7 """8 Old callback help.9 """101112users_app = typer.Typer(callback=old_callback, name="exp-users", help="Explicit help.")131415def new_users():16 """17 I have the highland! Create some users.18 """192021app.add_typer(users_app, callback=new_users)222324@users_app.callback("call-users", help="Help from callback for users.")25def users():26 """27 Manage users in the app.28 """293031@users_app.command()32def create(name: str):33 typer.echo(f"Creating user: {name}")343536if __name__ == "__main__":37 app()
added:docs/src/subcommands/name_help/tutorial008.py+42 added
1import typer23app = typer.Typer()456def old_callback():7 """8 Old callback help.9 """101112users_app = typer.Typer(callback=old_callback, name="exp-users", help="Explicit help.")131415def new_users():16 """17 I have the highland! Create some users.18 """192021app.add_typer(22 users_app,23 callback=new_users,24 name="cake-sith-users",25 help="Unlimited powder! Eh, users.",26)272829@users_app.callback("call-users", help="Help from callback for users.")30def users():31 """32 Manage users in the app.33 """343536@users_app.command()37def create(name: str):38 typer.echo(f"Creating user: {name}")394041if __name__ == "__main__":42 app()
added:docs/src/subcommands/tutorial003/items.py+22 added
1import typer23app = typer.Typer()456@app.command()7def create(item: str):8 typer.echo(f"Creating item: {item}")91011@app.command()12def delete(item: str):13 typer.echo(f"Deleting item: {item}")141516@app.command()17def sell(item: str):18 typer.echo(f"Selling item: {item}")192021if __name__ == "__main__":22 app()
added:docs/src/subcommands/tutorial003/lands.py+11 added
1import typer23import reigns4import towns56app = typer.Typer()7app.add_typer(reigns.app, name="reigns")8app.add_typer(towns.app, name="towns")910if __name__ == "__main__":11 app()
added:docs/src/subcommands/tutorial003/main.py+13 added
1import typer23import items4import lands5import users67app = typer.Typer()8app.add_typer(users.app, name="users")9app.add_typer(items.app, name="items")10app.add_typer(lands.app, name="lands")1112if __name__ == "__main__":13 app()
added:docs/src/subcommands/tutorial003/reigns.py+17 added
1import typer23app = typer.Typer()456@app.command()7def conquer(name: str):8 typer.echo(f"Conquering reign: {name}")91011@app.command()12def destroy(name: str):13 typer.echo(f"Destroying reign: {name}")141516if __name__ == "__main__":17 app()
added:docs/src/subcommands/tutorial003/towns.py+17 added
1import typer23app = typer.Typer()456@app.command()7def found(name: str):8 typer.echo(f"Founding town: {name}")91011@app.command()12def burn(name: str):13 typer.echo(f"Burning town: {name}")141516if __name__ == "__main__":17 app()
added:docs/src/subcommands/tutorial003/users.py+17 added
1import typer23app = typer.Typer()456@app.command()7def create(user_name: str):8 typer.echo(f"Creating user: {user_name}")91011@app.command()12def delete(user_name: str):13 typer.echo(f"Deleting user: {user_name}")141516if __name__ == "__main__":17 app()
modified:docs/tutorial/subcommands/add-typer.md+3 added-1 removed
1We'll start with the core ideas.1We'll start with the core idea.23To add a `typer.Typer()` app inside of another.45## Manage items
158 unmodified lines
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.168169This way, in the same spirit of Click, **Typer** applications are composable, each `typer.Typer()` can be a *CLI app* by itself, but it can also be added as a command group to another Typer app.
added:docs/tutorial/subcommands/callback-override.md+104 added
1When creating a **Typer** app you can define a callback function, it always executes and defines the *CLI arguments* and *CLI options* that go before a command.23When adding a Typer app inside of another, the sub-Typer can also have its own callback.45It can handle any *CLI parameters* that go before its own commands and execute any extra code:67```Python hl_lines="9 10 11"8{!./src/subcommands/callback_override/tutorial001.py!}9```1011In this case it doesn't define any *CLI parameters*, it just writes a message.1213Check it:1415<div class="termy">1617```console18$ python main.py users create Camila1920// Notice the first message is not created by the command function but by the callback21Running a users command22Creating user: Camila23```2425</div>2627## Add a callback on creation2829It's also possible to add a callback when creating the `typer.Typer()` app that will be added to another Typer app:3031```Python hl_lines="6 7 10"32{!./src/subcommands/callback_override/tutorial002.py!}33```3435This achieves exactly the same as above, it's just another place to add the callback.3637Check it:3839<div class="termy">4041```console42$ python main.py users create Camila4344Running a users command45Creating user: Camila46```4748</div>4950## Overriding the callback on creation5152If a callback was added when creating the `typer.Typer()` app, it's possible to override it with a new one using `@app.callback()`.5354This is the same information you saw on the section about <a href="https://typer.tiangolo.com/tutorial/commands/callback/" target="_blank">Commands - Typer Callback</a>, and it applies the same for sub-Typer apps:5556```Python hl_lines="6 7 10 14 15 16"57{!./src/subcommands/callback_override/tutorial003.py!}58```5960Here we had defined a callback when creating the `typer.Typer()` sub-app, but then we override it with a new callback with the function `user_callback()`.6162As `@app.callback()` takes precedence over `typer.Typer(callback=some_function)`, now our CLI app will use this new callback.6364Check it:6566<div class="termy">6768```console69$ python main.py users create Camila7071// Notice the message from the new callback72Callback override, running users command73Creating user: Camila74```7576</div>7778## Overriding the callback when adding a sub-Typer7980Lastly, you can override the callback defined anywhere else when adding a sub-Typer with `app.add_typer()` using the `callback` parameter.8182This has the highest priority:8384```Python hl_lines="13 14 17"85{!./src/subcommands/callback_override/tutorial004.py!}86```8788Notice that the precedence goes to `app.add_typer()` and is not affected by the order of execution. There's another callback defined below, but the one from `app.add_typer()` wins.8990Now when you use the CLI program it will use the new callback function `callback_for_add_typer()`.9192Check it:9394<div class="termy">9596```console97$ python users create Camila9899// Notice the message from the callback added in add_typer()100I have the high land! Running users command101Creating user: Camila102```103104</div>
added:docs/tutorial/subcommands/name-and-help.md+463 added
1When adding a Typer app to another we have seen how to set the `name` to use for the command.23For example to set the command to `users`:45```Python6app.add_typer(users.app, name="users")7```89## Add a help text1011We can also set the `help` while adding a Typer:1213```Python hl_lines="6"14{!./src/subcommands/name_help/tutorial001.py!}15```1617And then we get that help text for that command in the *CLI program*:1819<div class="termy">2021```console22// Check the main help23$ python main.py --help2425Usage: main.py [OPTIONS] COMMAND [ARGS]...2627Options:28 --install-completion Install completion for the current shell.29 --show-completion Show completion for the current shell, to copy it or customize the installation.30 --help Show this message and exit.3132Commands:33 users Manage users in the app.3435// Check the help for the users command36$ python main.py users --help3738Usage: main.py users [OPTIONS] COMMAND [ARGS]...3940 Manage users in the app.4142Options:43 --help Show this message and exit.4445Commands:46 create47```4849</div>5051We can set the `name` and `help` in several places, each one taking precedence over the other, overriding the previous value.5253Let's see those locations.5455!!! tip56 There are other attributes that can be set in that same way in the same places we'll see next.5758 But those are documented later in another section.5960## Inferring name and help from callback6162### Inferring a command's name and help6364When you create a command with `@app.command()`, by default, it generates the name from the function name.6566And by default, the help text is extracted from the function's docstring.6768For example:6970```Python71@app.command()72def create(item: str):73 """74 Create an item.75 """76 typer.echo(f"Creating item: {item}")77```7879...will create a command `create` with a help text of `Create an item`.8081### Inferring name and help from `@app.callback()`8283The same way, if you define a callback in a `typer.Typer()`, the help text is extracted from the callback function's docstring.8485And if that Typer app is added to another Typer app, the default name of the command is generated from the name of the callback function.8687Here's an example:8889```Python hl_lines="6 9 10 11 12 13"90{!./src/subcommands/name_help/tutorial002.py!}91```9293Notice that now we added the sub-Typer without specifying a `name` nor a `help`.9495They are now inferred from the callback function.9697The command name will be the same callback function's name: `users`.9899And the help text for that `users` command will be the callback function's docstring: `Manage users in the app.`.100101Check it:102103<div class="termy">104105```console106// Check the main help107$ python main.py --help108109// Notice the command name "users" and the help text "Manage users in the app."110Usage: main.py [OPTIONS] COMMAND [ARGS]...111112Options:113 --install-completion Install completion for the current shell.114 --show-completion Show completion for the current shell, to copy it or customize the installation.115 --help Show this message and exit.116117Commands:118 users Manage users in the app.119120// Check the help for the users command121$ python main.py users --help122123// Notice the main description: "Manage users in the app."124Usage: main.py users [OPTIONS] COMMAND [ARGS]...125126 Manage users in the app.127128Options:129 --help Show this message and exit.130131Commands:132 create133```134135</div>136137### Name and help from callback parameter in `typer.Typer()`138139If you pass a `callback` parameter while creating a `typer.Typer(callback=some_function)` it will be used to infer the name and help text.140141This has the lowest priority, we'll see later what has a higher priority and can override it.142143Check the code:144145```Python hl_lines="6 7 8 9 12"146{!./src/subcommands/name_help/tutorial003.py!}147```148149This achieves exactly the same as the previous example.150151Check it:152153<div class="termy">154155```console156// Check the main help157$ python main.py --help158159// Notice the command name "users" and the help text "Manage users in the app."160Usage: main.py [OPTIONS] COMMAND [ARGS]...161162Options:163 --install-completion Install completion for the current shell.164 --show-completion Show completion for the current shell, to copy it or customize the installation.165 --help Show this message and exit.166167Commands:168 users Manage users in the app.169170// Check the help for the users command171$ python main.py users --help172173// Notice the main description: "Manage users in the app."174Usage: main.py users [OPTIONS] COMMAND [ARGS]...175176 Manage users in the app.177178Options:179 --help Show this message and exit.180181Commands:182 create183```184185</div>186187### Override a callback set in `typer.Typer()` with `@app.callback()`188189The same as with normal **Typer** apps, if you pass a `callback` to `typer.Typer(callback=some_function)` and then override it with `@app.callback()`, the name and help text will be inferred from the new callback:190191```Python hl_lines="16 17 18 19 20"192{!./src/subcommands/name_help/tutorial004.py!}193```194195Now the name of the command will be `users` instead of `old-callback`, and the help text will be `Manage users in the app.` instead of `Old callback help.`.196197Check it:198199<div class="termy">200201```console202// Check the main help203$ python main.py --help204205// Notice the command name "users" and the help text "Manage users in the app."206Usage: main.py [OPTIONS] COMMAND [ARGS]...207208Options:209 --install-completion Install completion for the current shell.210 --show-completion Show completion for the current shell, to copy it or customize the installation.211 --help Show this message and exit.212213Commands:214 users Manage users in the app.215216// Check the help for the users command217$ python main.py users --help218219// Notice the main description: "Manage users in the app."220Usage: main.py users [OPTIONS] COMMAND [ARGS]...221222 Manage users in the app.223224Options:225 --help Show this message and exit.226227Commands:228 create229```230231</div>232233### Infer name and help from callback in `app.add_typer()`234235If you override the callback in `app.add_typer()` when including a sub-app, the name and help will be inferred from this callback function.236237This takes precedence over inferring the name and help from a callback set in `@sub_app.callback()` and `typer.Typer(callback=sub_app_callback)`.238239Check the code:240241```Python hl_lines="15 16 17 18 21"242{!./src/subcommands/name_help/tutorial005.py!}243```244245Now the command will be `new-users` instead of `users`. And the help text will be `I have the highland! Create some users.` instead of the previous ones.246247Check it:248249<div class="termy">250251```console252// Check the main help253$ python main.py --help254255// Check the command new-users and its help text256257Usage: main.py [OPTIONS] COMMAND [ARGS]...258259Options:260 --install-completion Install completion for the current shell.261 --show-completion Show completion for the current shell, to copy it or customize the installation.262 --help Show this message and exit.263264Commands:265 new-users I have the highland! Create some users.266267// Now check the help for the new-users command268$ python main.py new-users --help269270// Notice the help text271Usage: main.py new-users [OPTIONS] COMMAND [ARGS]...272273 I have the highland! Create some users.274275Options:276 --help Show this message and exit.277278Commands:279 create280```281282</div>283284### Enough inferring285286So, when inferring a name and help text, the precedence order from lowest priority to highest is:287288* `sub_app = typer.Typer(callback=some_function)`289* `@sub_app.callback()`290* `app.add_typer(sub_app, callback=new_function)`291292That's for inferring the name and help text from functions.293294But if you set the name and help text explicitly, that has a higher priority than these.295296## Set the name and help297298Let's now see the places where you can set the command name and help text, from lowest priority to highest.299300!!! tip301 Setting the name and help text explicitly always has a higher precedence than inferring from a callback function.302303### Name and help in `typer.Typer()`304305You could have all the callbacks and overrides we defined before, but the name and help text was inferred from the function name and docstring.306307If you set it explicitly, that takes precedence over inferring.308309You can set it when creating a new `typer.Typer()`:310311```Python hl_lines="12"312{!./src/subcommands/name_help/tutorial006.py!}313```314315!!! info316 The rest of the callbacks and overrides are there only to show you that they don't affect the name and help text when you set it explicitly.317318We set an explicit name `exp-users`, and an explicit help `Explicit help.`.319320So that will take precedence now.321322Check it:323324<div class="termy">325326```console327// Check the main help328$ python main.py --help329330// Notice the command name is exp-users and the help text is "Explicit help."331Usage: main.py [OPTIONS] COMMAND [ARGS]...332333Options:334 --install-completion Install completion for the current shell.335 --show-completion Show completion for the current shell, to copy it or customize the installation.336 --help Show this message and exit.337338Commands:339 exp-users Explicit help.340341// Check the help for the exp-users command342$ python main.py exp-users --help343344// Notice the main help text345Usage: main.py exp-users [OPTIONS] COMMAND [ARGS]...346347 Explicit help.348349Options:350 --help Show this message and exit.351352Commands:353 create354```355356</div>357358### Name and help in `@app.callback()`359360Any parameter that you use when creating a `typer.Typer()` app can be overridden in the parameters of `@app.callback()`.361362Continuing with the previous example, we now override the values in `@user_app.callback()`:363364```Python hl_lines="24"365{!./src/subcommands/name_help/tutorial007.py!}366```367368And now the command name will be `call-users` and the help text will be `Help from callback for users.`.369370Check it:371372<div class="termy">373374```console375// Check the help376$ python main.py --help377378// The command name now is call-users and the help text is "Help from callback for users.".379Usage: main.py [OPTIONS] COMMAND [ARGS]...380381Options:382 --install-completion Install completion for the current shell.383 --show-completion Show completion for the current shell, to copy it or customize the installation.384 --help Show this message and exit.385386Commands:387 call-users Help from callback for users.388389// Check the call-users command help390$ python main.py call-users --help391392// Notice the main help text393Usage: main.py call-users [OPTIONS] COMMAND [ARGS]...394395 Help from callback for users.396397Options:398 --help Show this message and exit.399400Commands:401 create402```403404</div>405406### Name and help in `app.add_typer()`407408And finally, with the highest priority, you can override all that by explicitly setting the `name` and `help` in `app.add_typer()`, just like we did on the first example above:409410```Python hl_lines="21"411{!./src/subcommands/name_help/tutorial008.py!}412```413414And now, with the highest priorities of them all, the command name will now be `cake-sith-users` and the help text will be `Unlimited powder! Eh, users.`.415416Check it:417418<div class="termy">419420```console421// Check the help422$ python main.py --help423424// Notice the command name cake-sith-users and the new help text "Unlimited powder! Eh, users."425Usage: main.py [OPTIONS] COMMAND [ARGS]...426427Options:428 --install-completion Install completion for the current shell.429 --show-completion Show completion for the current shell, to copy it or customize the installation.430 --help Show this message and exit.431432Commands:433 cake-sith-users Unlimited powder! Eh, users.434435// And check the help for the command cake-sith-users436$ python main.py cake-sith-users --help437438// Notice the main help text439Usage: main.py cake-sith-users [OPTIONS] COMMAND [ARGS]...440441 Unlimited powder! Eh, users.442443Options:444 --help Show this message and exit.445446Commands:447 create448```449450</div>451452## Recap453454The precedence to generate a command's name and help, from lowest priority to highest, is:455456* Implicitly inferred from `sub_app = typer.Typer(callback=some_function)`457* Implicitly inferred from the callback function under `@sub_app.callback()`458* Implicitly inferred from `app.add_typer(sub_app, callback=some_function)`459* Explicitly set on `sub_app = typer.Typer(name="some-name", help="Some help.")`460* Explicitly set on `@sub_app.callback("some-name", help="Some help.")`461* Explicitly set on `app.add_typer(sub_app, name="some-name", help="Some help.")`462463So, `app.add_typer(sub_app, name="some-name", help="Some help.")` always wins.
added:docs/tutorial/subcommands/nested-subcommands.md+295 added
1We'll now see how these same ideas can be extended for deeply nested commands.23Let's imagine that the same *CLI program* from the previous examples now needs to handle `lands`.45But a land could be a `reign` or `town`.67And each of those could have their own commands, like `create` and `delete`.89## A CLI app for reigns1011Let's start with a file `reigns.py`:1213```Python hl_lines=""14{!./src/subcommands/tutorial003/reigns.py!}15```1617This is already a simple *CLI program* to manage reigns:1819<div class="termy">2021```console22// Check the help23$ python reigns.py --help2425Usage: reigns.py [OPTIONS] COMMAND [ARGS]...2627Options:28 --install-completion Install completion for the current shell.29 --show-completion Show completion for the current shell, to copy it or customize the installation.30 --help Show this message and exit.3132Commands:33 conquer34 destroy3536// Try it37$ python reigns.py conquer Cintra3839Conquering reign: Cintra4041$ python reigns.py destroy Mordor4243Destroying reign: Mordor44```4546</div>4748## A CLI app for towns4950And now the equivalent for managing towns in `towns.py`:5152```Python hl_lines=""53{!./src/subcommands/tutorial003/towns.py!}54```5556With it, you can manage towns:5758<div class="termy">5960```console61// Check the help62$ python towns.py --help6364Usage: towns.py [OPTIONS] COMMAND [ARGS]...6566Options:67 --install-completion Install completion for the current shell.68 --show-completion Show completion for the current shell, to copy it or customize the installation.69 --help Show this message and exit.7071Commands:72 burn73 found7475// Try it76$ python towns.py found "New Asgard"7778Founding town: New Asgard7980$ python towns.py burn Vizima8182Burning town: Vizima83```8485</div>8687## Manage the land in a CLI app8889Now let's put the `reigns` and `towns` together in the same *CLI program* in `lands.py`:9091```Python hl_lines=""92{!./src/subcommands/tutorial003/lands.py!}93```9495And now we have a single *CLI program* with a command (or command group) `reigns` that has its own commands. And another command `towns` with its own subcommands.9697Check it:9899<div class="termy">100101```console102// Check the help103$ python lands.py --help104105Usage: lands.py [OPTIONS] COMMAND [ARGS]...106107Options:108 --install-completion Install completion for the current shell.109 --show-completion Show completion for the current shell, to copy it or customize the installation.110 --help Show this message and exit.111112Commands:113 reigns114 towns115116// We still have the help for reigns117$ python lands.py reigns --help118119Usage: lands.py reigns [OPTIONS] COMMAND [ARGS]...120121Options:122 --help Show this message and exit.123124Commands:125 conquer126 destroy127128// And the help for towns129$ python lands.py towns --help130131Usage: lands.py towns [OPTIONS] COMMAND [ARGS]...132133Options:134 --help Show this message and exit.135136Commands:137 burn138 found139```140141</div>142143Now try it, manage the lands through the CLI:144145<div class="termy">146147```console148// Try the reigns command149$ python lands.py reigns conquer Gondor150151Conquering reign: Gondor152153$ python lands.py reigns destroy Nilfgaard154155Destroying reign: Nilfgaard156157// Try the towns command158$ python lands.py towns found Springfield159160Founding town: Springfield161162$ python lands.py towns burn Atlantis163164Burning town: Atlantis165```166167</div>168169## Deeply nested subcommands170171Now let's say that all these commands in the `lands.py` *CLI program* should be part of the previous *CLI program* we built in the first example.172173We want our *CLI program* to have these commands/command groups:174175* `users`:176 * `create`177 * `delete`178* `items`:179 * `create`180 * `delete`181 * `sell`182* `lands`:183 * `reigns`:184 * `conquer`185 * `destroy`186 * `towns`:187 * `found`188 * `burn`189190This already is a quite deeply nested "tree" of commands/command groups.191192But to achieve that, we just have to add the `lands` **Typer** app to the same `main.py` file we already had:193194```Python hl_lines="4 10"195{!./src/subcommands/tutorial003/main.py!}196```197198And now we have everything in a single *CLI program*:199200<div class="termy">201202```console203// Check the main help204$ python main.py --help205206Usage: main.py [OPTIONS] COMMAND [ARGS]...207208Options:209 --install-completion Install completion for the current shell.210 --show-completion Show completion for the current shell, to copy it or customize the installation.211 --help Show this message and exit.212213Commands:214 items215 lands216 users217218// Try some users commands219$ python main.py users create Camila220221Creating user: Camila222223// Now try some items commands224$ python main.py items create Sword225226Creating item: Sword227228// And now some lands commands for reigns229$ python main.py lands reigns conquer Gondor230231Conquering reign: Gondor232233// And for towns234$ python main.py lands towns found Cartagena235236Founding town: Cartagena237```238239</div>240241## Review the files242243Here are all the files if you want to review/copy them:244245`reigns.py`:246247```Python hl_lines=""248{!./src/subcommands/tutorial003/reigns.py!}249```250251`towns.py`:252253```Python hl_lines=""254{!./src/subcommands/tutorial003/towns.py!}255```256257`lands.py`:258259```Python hl_lines=""260{!./src/subcommands/tutorial003/lands.py!}261```262263`users.py`:264265```Python hl_lines=""266{!./src/subcommands/tutorial003/users.py!}267```268269`items.py`:270271```Python hl_lines=""272{!./src/subcommands/tutorial003/items.py!}273```274275`main.py`:276277```Python hl_lines=""278{!./src/subcommands/tutorial003/main.py!}279```280281!!! tip282 All these files have an `if __name__ == "__main__"` block just to demonstrate how each of them can also be an independent *CLI app*.283284 But for your final application, only `main.py` would need it.285286## Recap287288That's it, you can just add **Typer** applications one inside another as much as you want and create complex *CLI programs* while writing simple code.289290You can probably achieve a simpler *CLI program* design that's easier to use than the example here. But if your requirements are complex, **Typer** helps you build your *CLI app* easily.291292!!! tip293 Auto completion helps a lot, specially with complex programs.294295 Check the docs about adding auto completion to your *CLI apps*.