-
First Check
Commit to Help
Example Code# copy-pasted example from https://typer.tiangolo.com/tutorial/subcommands/single-file/
import typer
app = typer.Typer()
items_app = typer.Typer()
app.add_typer(items_app, name="items")
users_app = typer.Typer()
app.add_typer(users_app, name="users")
@items_app.command("create")
def items_create(item: str):
print(f"Creating item: {item}")
@items_app.command("delete")
def items_delete(item: str):
print(f"Deleting item: {item}")
@items_app.command("sell")
def items_sell(item: str):
print(f"Selling item: {item}")
@users_app.command("create")
def users_create(user_name: str):
print(f"Creating user: {user_name}")
@users_app.command("delete")
def users_delete(user_name: str):
print(f"Deleting user: {user_name}")
if __name__ == "__main__":
app()DescriptionI need a cli with several subcommands that are invoked simultaneously. I followed the SubCommands in a Single File, but it creates mutually exclusive commands that cannot be invoked in one command line. Following the example from the above tutorial, such command would look like this: $ python main.py users create "user name" items create "item name"It fails with an error: Is it possible to achieve this with Operating SystemLinux Operating System DetailsNo response Typer Version0.12.4 Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It seems you're trying to invoke multiple subcommands simultaneously within a single command line using typer. I am not an expert of typer, but I don't think it natively support invoking multiple subcommands in one command line. What are you trying to achieve? In a typical command-line interface, subcommands are designed to be mutually exclusive, meaning that only one subcommand can be executed at a time per command invocation. When you run: $ python main.py users create "user name" items create "item name"Typer interprets If you absolutely need to execute multiple actions in a single command, you can implement a command that processes multiple "subcommands" (actually arguments of your command). Here’s an example of how you could implement this: import typer
app = typer.Typer()
@app.command("multi")
def multi_command(users_create: str = None, items_create: str = None):
if users_create:
typer.echo(f"Creating user: {users_create}")
# Logic to create a user
if items_create:
typer.echo(f"Creating item: {items_create}")
# Logic to create an item
if __name__ == "__main__":
app()You would then run: $ python main.py --users-create "user name" --items-create "item name"Let me know if this helps or if you need further assistance. |
Beta Was this translation helpful? Give feedback.
It seems you're trying to invoke multiple subcommands simultaneously within a single command line using typer. I am not an expert of typer, but I don't think it natively support invoking multiple subcommands in one command line.
What are you trying to achieve? In a typical command-line interface, subcommands are designed to be mutually exclusive, meaning that only one subcommand can be executed at a time per command invocation.
When you run:
Typer interprets
items create "item name"as unexpected extra arguments for theusers createsubcommand because it's still processing the first subcommand.If you absolutely need to ex…