In fact, each command can have different CLI arguments and CLI options:
importtyperfromtyping_extensionsimportAnnotatedapp=typer.Typer()@app.command()defcreate(username:str):print(f"Creating user: {username}")@app.command()defdelete(username:str,force:Annotated[bool,typer.Option(prompt="Are you sure you want to delete the user?")],):ifforce:print(f"Deleting user: {username}")else:print("Operation cancelled")@app.command()defdelete_all(force:Annotated[bool,typer.Option(prompt="Are you sure you want to delete ALL users?")],):ifforce:print("Deleting all users")else:print("Operation cancelled")@app.command()definit():print("Initializing user database")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperapp=typer.Typer()@app.command()defcreate(username:str):print(f"Creating user: {username}")@app.command()defdelete(username:str,force:bool=typer.Option(...,prompt="Are you sure you want to delete the user?"),):ifforce:print(f"Deleting user: {username}")else:print("Operation cancelled")@app.command()defdelete_all(force:bool=typer.Option(...,prompt="Are you sure you want to delete ALL users?"),):ifforce:print("Deleting all users")else:print("Operation cancelled")@app.command()definit():print("Initializing user database")if__name__=="__main__":app()
Here we have multiple commands, with different CLI parameters:
create:
username: a CLI argument.
delete:
username: a CLI argument.
--force: a CLI option, if not provided, it's prompted.
delete-all:
--force: a CLI option, if not provided, it's prompted.
Options: --install-completion Install completion for the current shell. --show-completion Show completion for the current shell, to copy it or customize the installation. --help Show this message and exit.