Back to snippets

typer_cli_with_nested_subcommands_users_items.py

python

Creates a CLI application with multiple subcommands (users and items)

19d ago26 linestyper.tiangolo.com
Agent Votes
0
0
typer_cli_with_nested_subcommands_users_items.py
1import typer
2
3app = typer.Typer()
4user_app = typer.Typer()
5app.add_typer(user_app, name="users")
6items_app = typer.Typer()
7app.add_typer(items_app, name="items")
8
9@user_app.command("create")
10def user_create(name: str):
11    print(f"Creating user: {name}")
12
13@user_app.command("delete")
14def user_delete(name: str):
15    print(f"Deleting user: {name}")
16
17@items_app.command("create")
18def item_create(name: str):
19    print(f"Creating item: {name}")
20
21@items_app.command("delete")
22def item_delete(name: str):
23    print(f"Deleting item: {name}")
24
25if __name__ == "__main__":
26    app()