The same as before, you can add help for the commands in the docstrings and the CLI options.
And the typer.Typer() application receives a parameter help that you can pass with the main help text for your CLI program:
importtyperfromtyping_extensionsimportAnnotatedapp=typer.Typer(help="Awesome CLI user manager.")@app.command()defcreate(username:str):""" Create a new user with USERNAME. """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?",help="Force deletion without confirmation.",),],):""" Delete a user with USERNAME. If --force is not used, will ask for confirmation. """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?",help="Force deletion without confirmation.",),],):""" Delete ALL users in the database. If --force is not used, will ask for confirmation. """ifforce:print("Deleting all users")else:print("Operation cancelled")@app.command()definit():""" Initialize the users database. """print("Initializing user database")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperapp=typer.Typer(help="Awesome CLI user manager.")@app.command()defcreate(username:str):""" Create a new user with USERNAME. """print(f"Creating user: {username}")@app.command()defdelete(username:str,force:bool=typer.Option(...,prompt="Are you sure you want to delete the user?",help="Force deletion without confirmation.",),):""" Delete a user with USERNAME. If --force is not used, will ask for confirmation. """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?",help="Force deletion without confirmation.",),):""" Delete ALL users in the database. If --force is not used, will ask for confirmation. """ifforce:print("Deleting all users")else:print("Operation cancelled")@app.command()definit():""" Initialize the users database. """print("Initializing user database")if__name__=="__main__":app()
Check it:
fast →💬 Check the new helppython main.py --help Usage: main.py [OPTIONS] COMMAND [ARGS]...
Awesome CLI user manager.
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.
Commands: create Create a new user with USERNAME. delete Delete a user with USERNAME. delete-all Delete ALL users in the database. init Initialize the users database.
💬 Now the commands have inline help 🎉 💬 Check the help for createpython main.py create --help Usage: main.py create [OPTIONS] USERNAME
Create a new user with USERNAME.
Options: --help Show this message and exit.
💬 Check the help for deletepython main.py delete --help Usage: main.py delete [OPTIONS] USERNAME
Delete a user with USERNAME.
If --force is not used, will ask for confirmation.
Options: --force / --no-force Force deletion without confirmation. [required] --help Show this message and exit.
💬 Check the help for delete-allpython main.py delete-all --help Usage: main.py delete-all [OPTIONS]
Delete ALL users in the database.
If --force is not used, will ask for confirmation.
Options: --force / --no-force Force deletion without confirmation. [required] --help Show this message and exit.
💬 Check the help for initpython main.py init --help Usage: main.py init [OPTIONS]
You will probably be better adding the help text as a docstring to your functions, but if for some reason you wanted to overwrite it, you can use the help function argument passed to @app.command():
importtyperapp=typer.Typer()@app.command(help="Create a new user with USERNAME.")defcreate(username:str):""" Some internal utility function to create. """print(f"Creating user: {username}")@app.command(help="Delete a user with USERNAME.")defdelete(username:str):""" Some internal utility function to delete. """print(f"Deleting user: {username}")if__name__=="__main__":app()
Check it:
fast →💬 Check the helppython main.py --help 💬 Notice it uses the help passed to @app.command()Usage: main.py [OPTIONS] COMMAND [ARGS]...
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.
Commands: create Create a new user with USERNAME. delete Delete a user with USERNAME.
💬 It uses "Create a new user with USERNAME." instead of "Some internal utility function to create." restart ↻
There could be cases where you have a command in your app that you need to deprecate, so that your users stop using it, even while it's still supported for a while.
You can mark it with the parameter deprecated=True:
importtyperapp=typer.Typer()@app.command()defcreate(username:str):""" Create a user. """print(f"Creating user: {username}")@app.command(deprecated=True)defdelete(username:str):""" Delete a user. This is deprecated and will stop being supported soon. """print(f"Deleting user: {username}")if__name__=="__main__":app()
And when you show the --help option you will see it's marked as "deprecated":
fast →python main.py --help Usage: main.py [OPTIONS] COMMAND [ARGS]... ╭─ 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. │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Commands ────────────────────────────────────────────────────────╮ │ create Create a user. │ │ delete Delete a user. (deprecated) │ ╰───────────────────────────────────────────────────────────────────╯
And if you check the --help for the deprecated command (in this example, the command delete), it also shows it as deprecated:
fast →python main.py delete --help Usage: main.py delete [OPTIONS] USERNAME (deprecated) Delete a user. This is deprecated and will stop being supported soon.
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT [default: None] [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯
If you have Rich installed as described in Printing and Colors, you can configure your app to enable markup text with the parameter rich_markup_mode.
Then you can use more formatting in the docstrings and the help parameter for CLI arguments and CLI options. You will see more about it below. 👇
Info
By default, rich_markup_mode is None if Rich is not installed, and "rich" if it is installed. In the latter case, you can set rich_markup_mode to None to disable rich text formatting.
If you set rich_markup_mode="rich" when creating the typer.Typer() app, you will be able to use Rich Console Markup in the docstring, and even in the help for the CLI arguments and options:
importtyperfromtyping_extensionsimportAnnotatedapp=typer.Typer(rich_markup_mode="rich")@app.command()defcreate(username:Annotated[str,typer.Argument(help="The username to be [green]created[/green]")],):""" [bold green]Create[/bold green] a new [italic]shinny[/italic] user. :sparkles: This requires a [underline]username[/underline]. """print(f"Creating user: {username}")@app.command(help="[bold red]Delete[/bold red] a user with [italic]USERNAME[/italic].")defdelete(username:Annotated[str,typer.Argument(help="The username to be [red]deleted[/red]")],force:Annotated[bool,typer.Option(help="Force the [bold red]deletion[/bold red] :boom:")]=False,):""" Some internal utility function to delete. """print(f"Deleting user: {username}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperapp=typer.Typer(rich_markup_mode="rich")@app.command()defcreate(username:str=typer.Argument(...,help="The username to be [green]created[/green]"),):""" [bold green]Create[/bold green] a new [italic]shiny[/italic] user. :sparkles: This requires a [underline]username[/underline]. """print(f"Creating user: {username}")@app.command(help="[bold red]Delete[/bold red] a user with [italic]USERNAME[/italic].")defdelete(username:str=typer.Argument(...,help="The username to be [red]deleted[/red]"),force:bool=typer.Option(False,help="Force the [bold red]deletion[/bold red] :boom:"),):""" Some internal utility function to delete. """print(f"Deleting user: {username}")if__name__=="__main__":app()
With that, you can use Rich Console Markup to format the text in the docstring for the command create, make the word "create" bold and green, and even use an emoji.
You can also use markup in the help for the username CLI Argument.
And the same as before, the help text overwritten for the command delete can also use Rich Markup, the same in the CLI Argument and CLI Option.
If you run the program and check the help, you will see that Typer uses Rich internally to format the help.
Check the help for the create command:
fast →python main.py create --help Usage: main.py create [OPTIONS] USERNAME Create a new shiny user. ✨ This requires a username.
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT The username to be created │ │ [default: None] │ │ [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯
fast →python main.py delete --help Usage: main.py delete [OPTIONS] USERNAME Delete a user with USERNAME.
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT The username to be deleted │ │ [default: None] │ │ [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --force--no-force Force the deletion 💥 │ │ [default: no-force] │ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯
If you set rich_markup_mode="markdown" when creating the typer.Typer() app, you will be able to use Markdown in the docstring:
importtyperfromtyping_extensionsimportAnnotatedapp=typer.Typer(rich_markup_mode="markdown")@app.command()defcreate(username:Annotated[str,typer.Argument(help="The username to be **created**")],):""" **Create** a new *shinny* user. :sparkles: * Create a username * Show that the username is created --- Learn more at the [Typer docs website](https://typer.tiangolo.com) """print(f"Creating user: {username}")@app.command(help="**Delete** a user with *USERNAME*.")defdelete(username:Annotated[str,typer.Argument(help="The username to be **deleted**")],force:Annotated[bool,typer.Option(help="Force the **deletion** :boom:")]=False,):""" Some internal utility function to delete. """print(f"Deleting user: {username}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
importtyperapp=typer.Typer(rich_markup_mode="markdown")@app.command()defcreate(username:str=typer.Argument(...,help="The username to be **created**")):""" **Create** a new *shiny* user. :sparkles: * Create a username * Show that the username is created --- Learn more at the [Typer docs website](https://typer.tiangolo.com) """print(f"Creating user: {username}")@app.command(help="**Delete** a user with *USERNAME*.")defdelete(username:str=typer.Argument(...,help="The username to be **deleted**"),force:bool=typer.Option(False,help="Force the **deletion** :boom:"),):""" Some internal utility function to delete. """print(f"Deleting user: {username}")if__name__=="__main__":app()
With that, you can use Markdown to format the text in the docstring for the command create, make the word "create" bold, show a list of items, and even use an emoji.
And the same as before, the help text overwritten for the command delete can also use Markdown.
Check the help for the create command:
fast →python main.py create --help Usage: main.py create [OPTIONS] USERNAME Create a new shiny user. ✨
• Create a username • Show that the username is created
─────────────────────────────────────────────────────────────────── Learn more at the Typer docs website
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT The username to be created │ │ [default: None] │ │ [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯
fast →python main.py delete --help Usage: main.py delete [OPTIONS] USERNAME Delete a user with USERNAME.
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT The username to be deleted │ │ [default: None] │ │ [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --force--no-force Force the deletion 💥 │ │ [default: no-force] │ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯
To set the panel for a command you can pass the argument rich_help_panel with the name of the panel you want to use:
importtyperapp=typer.Typer(rich_markup_mode="rich")@app.command()defcreate(username:str):""" [green]Create[/green] a new user. :sparkles: """print(f"Creating user: {username}")@app.command()defdelete(username:str):""" [red]Delete[/red] a user. :fire: """print(f"Deleting user: {username}")@app.command(rich_help_panel="Utils and Configs")defconfig(configuration:str):""" [blue]Configure[/blue] the system. :wrench: """print(f"Configuring the system with: {configuration}")@app.command(rich_help_panel="Utils and Configs")defsync():""" [blue]Synchronize[/blue] the system or something fancy like that. :recycle: """print("Syncing the system")@app.command(rich_help_panel="Help and Others")defhelp():""" Get [yellow]help[/yellow] with the system. :question: """print("Opening help portal...")@app.command(rich_help_panel="Help and Others")defreport():""" [yellow]Report[/yellow] an issue. :bug: """print("Please open a new issue online, not a direct message")if__name__=="__main__":app()
Commands without a panel will be shown in the default panel Commands, and the rest will be shown in the next panels:
fast →python main.py --help Usage: main.py [OPTIONS] COMMAND [ARGS]... ╭─ 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. │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Commands ────────────────────────────────────────────────────────╮ │ create Create a new user. ✨ │ │ delete Delete a user. 🔥 │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Utils and Configs ───────────────────────────────────────────────╮ │ config Configure the system. 🔧 │ │ sync Synchronize the system or something fancy like that. ♻ │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Help and Others ─────────────────────────────────────────────────╮ │ help Get help with the system. ❓ │ │ report Report an issue. 🐛 │ ╰───────────────────────────────────────────────────────────────────╯
The same way, you can configure the panels for CLI arguments and CLI options with rich_help_panel.
And of course, in the same application you can also set the rich_help_panel for commands.
fromtypingimportUnionimporttyperfromtyping_extensionsimportAnnotatedapp=typer.Typer(rich_markup_mode="rich")@app.command()defcreate(username:Annotated[str,typer.Argument(help="The username to create")],lastname:Annotated[str,typer.Argument(help="The last name of the new user",rich_help_panel="Secondary Arguments"),]="",force:Annotated[bool,typer.Option(help="Force the creation of the user")]=False,age:Annotated[Union[int,None],typer.Option(help="The age of the new user",rich_help_panel="Additional Data"),]=None,favorite_color:Annotated[Union[str,None],typer.Option(help="The favorite color of the new user",rich_help_panel="Additional Data",),]=None,):""" [green]Create[/green] a new user. :sparkles: """print(f"Creating user: {username}")@app.command(rich_help_panel="Utils and Configs")defconfig(configuration:str):""" [blue]Configure[/blue] the system. :wrench: """print(f"Configuring the system with: {configuration}")if__name__=="__main__":app()
Tip
Prefer to use the Annotated version if possible.
fromtypingimportUnionimporttyperapp=typer.Typer(rich_markup_mode="rich")@app.command()defcreate(username:str=typer.Argument(...,help="The username to create"),lastname:str=typer.Argument("",help="The last name of the new user",rich_help_panel="Secondary Arguments"),force:bool=typer.Option(False,help="Force the creation of the user"),age:Union[int,None]=typer.Option(None,help="The age of the new user",rich_help_panel="Additional Data"),favorite_color:Union[str,None]=typer.Option(None,help="The favorite color of the new user",rich_help_panel="Additional Data",),):""" [green]Create[/green] a new user. :sparkles: """print(f"Creating user: {username}")@app.command(rich_help_panel="Utils and Configs")defconfig(configuration:str):""" [blue]Configure[/blue] the system. :wrench: """print(f"Configuring the system with: {configuration}")if__name__=="__main__":app()
Then if you run the application you will see all the CLI parameters in their respective panels.
First the CLI arguments that don't have a panel name set in a default one named "Arguments".
Next the CLI arguments with a custom panel. In this example named "Secondary Arguments".
After that, the CLI options that don't have a panel in a default one named "Options".
And finally, the CLI options with a custom panel set. In this example named "Additional Data".
You can check the --help option for the command create:
fast →python main.py create --help Usage: main.py create [OPTIONS] USERNAME [LASTNAME] Create a new user. ✨
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT The username to create [default: None] │ │ [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Secondary Arguments ─────────────────────────────────────────────╮ │ lastname [LASTNAME] The last name of the new user │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Options ─────────────────────────────────────────────────────────╮ │ --force--no-force Force the creation of the user │ │ [default: no-force] │ │ --help Show this message and exit. │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Additional Data ─────────────────────────────────────────────────╮ │ --ageINTEGER The age of the new user │ │ [default: None] │ │ --favorite-colorTEXT The favorite color of the new │ │ user │ │ [default: None] │ ╰───────────────────────────────────────────────────────────────────╯
And of course, the rich_help_panel can be used in the same way for commands in the same application.
And those panels will be shown when you use the main --help option.
fast →python main.py --help Usage: main.py [OPTIONS] COMMAND [ARGS]... ╭─ 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. │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Commands ────────────────────────────────────────────────────────╮ │ create Create a new user. ✨ │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ Utils and Configs ───────────────────────────────────────────────╮ │ config Configure the system. 🔧 │ ╰───────────────────────────────────────────────────────────────────╯
If you need, you can also add an epilog section to the help of your commands:
importtyperapp=typer.Typer(rich_markup_mode="rich")@app.command(epilog="Made with :heart: in [blue]Venus[/blue]")defcreate(username:str):""" [green]Create[/green] a new user. :sparkles: """print(f"Creating user: {username}")if__name__=="__main__":app()
And when you check the --help option it will look like:
fast →python main.py --help Usage: main.py [OPTIONS] USERNAME Create a new user. ✨
╭─ Arguments ───────────────────────────────────────────────────────╮ │ * username TEXT [default: None] [required] │ ╰───────────────────────────────────────────────────────────────────╯ ╭─ 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. │ ╰───────────────────────────────────────────────────────────────────╯