Skip to content

Command Help

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:

import typer
from typing_extensions import Annotated

app = typer.Typer(help="Awesome CLI user manager.")


@app.command()
def create(username: str):
    """
    Create a new user with USERNAME.
    """
    print(f"Creating user: {username}")


@app.command()
def delete(
    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.
    """
    if force:
        print(f"Deleting user: {username}")
    else:
        print("Operation cancelled")


@app.command()
def delete_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.
    """
    if force:
        print("Deleting all users")
    else:
        print("Operation cancelled")


@app.command()
def init():
    """
    Initialize the users database.
    """
    print("Initializing user database")


if __name__ == "__main__":
    app()
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

import typer

app = typer.Typer(help="Awesome CLI user manager.")


@app.command()
def create(username: str):
    """
    Create a new user with USERNAME.
    """
    print(f"Creating user: {username}")


@app.command()
def delete(
    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.
    """
    if force:
        print(f"Deleting user: {username}")
    else:
        print("Operation cancelled")


@app.command()
def delete_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.
    """
    if force:
        print("Deleting all users")
    else:
        print("Operation cancelled")


@app.command()
def init():
    """
    Initialize the users database.
    """
    print("Initializing user database")


if __name__ == "__main__":
    app()

Check it:

fast →python 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.


python main.py create --help
Usage: main.py create [OPTIONS] USERNAME

Create a new user with USERNAME.

Options:
--help Show this message and exit.

python 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.

python 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.

python main.py init --help
Usage: main.py init [OPTIONS]

Initialize the users database.

Options:
--help Show this message and exit.

restart ↻

Tip

typer.Typer() receives several other parameters for other things, we'll see that later.

You will also see how to use "Callbacks" later, and those include a way to add this same help message in a function docstring.

Overwrite command help

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():

import typer

app = typer.Typer()


@app.command(help="Create a new user with USERNAME.")
def create(username: str):
    """
    Some internal utility function to create.
    """
    print(f"Creating user: {username}")


@app.command(help="Delete a user with USERNAME.")
def delete(username: str):
    """
    Some internal utility function to delete.
    """
    print(f"Deleting user: {username}")


if __name__ == "__main__":
    app()

Check it:

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 with USERNAME.
delete Delete a user with USERNAME.


restart ↻

Deprecate a Command

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:

import typer

app = typer.Typer()


@app.command()
def create(username: str):
    """
    Create a user.
    """
    print(f"Creating user: {username}")


@app.command(deprecated=True)
def delete(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)
╰───────────────────────────────────────────────────────────────────╯

restart ↻

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. │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

Rich Markdown and Markup

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.

Rich Markup

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:

import typer
from typing_extensions import Annotated

app = typer.Typer(rich_markup_mode="rich")


@app.command()
def create(
    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].")
def delete(
    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()
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

import typer

app = typer.Typer(rich_markup_mode="rich")


@app.command()
def create(
    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].")
def delete(
    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. │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

And check the help for the delete command:

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. │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

Rich Markdown

If you set rich_markup_mode="markdown" when creating the typer.Typer() app, you will be able to use Markdown in the docstring:

import typer
from typing_extensions import Annotated

app = typer.Typer(rich_markup_mode="markdown")


@app.command()
def create(
    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*.")
def delete(
    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()
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

import typer

app = typer.Typer(rich_markup_mode="markdown")


@app.command()
def create(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*.")
def delete(
    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. │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

And the same for the delete command:

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. │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

Info

Notice that in Markdown you cannot define colors. For colors you might prefer to use Rich markup.

Help Panels

If you have many commands or CLI parameters, you might want to show their documentation in different panels when using the --help option.

If you installed Rich as described in Printing and Colors, you can configure the panel to use for each command or CLI parameter.

Help Panels for Commands

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:

import typer

app = typer.Typer(rich_markup_mode="rich")


@app.command()
def create(username: str):
    """
    [green]Create[/green] a new user. :sparkles:
    """
    print(f"Creating user: {username}")


@app.command()
def delete(username: str):
    """
    [red]Delete[/red] a user. :fire:
    """
    print(f"Deleting user: {username}")


@app.command(rich_help_panel="Utils and Configs")
def config(configuration: str):
    """
    [blue]Configure[/blue] the system. :wrench:
    """
    print(f"Configuring the system with: {configuration}")


@app.command(rich_help_panel="Utils and Configs")
def sync():
    """
    [blue]Synchronize[/blue] the system or something fancy like that. :recycle:
    """
    print("Syncing the system")


@app.command(rich_help_panel="Help and Others")
def help():
    """
    Get [yellow]help[/yellow] with the system. :question:
    """
    print("Opening help portal...")


@app.command(rich_help_panel="Help and Others")
def report():
    """
    [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. 🐛 │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

Help Panels for CLI Parameters

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.

from typing import Union

import typer
from typing_extensions import Annotated

app = typer.Typer(rich_markup_mode="rich")


@app.command()
def create(
    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")
def config(configuration: str):
    """
    [blue]Configure[/blue] the system. :wrench:
    """
    print(f"Configuring the system with: {configuration}")


if __name__ == "__main__":
    app()
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

from typing import Union

import typer

app = typer.Typer(rich_markup_mode="rich")


@app.command()
def create(
    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")
def config(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 ─────────────────────────────────────────────────╮
--age INTEGER The age of the new user │
│ [default: None] │
--favorite-color TEXT The favorite color of the new │
│ user │
│ [default: None] │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

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. 🔧 │
╰───────────────────────────────────────────────────────────────────╯

restart ↻

You can see the custom panel for the commands for "Utils and Configs".

Epilog

If you need, you can also add an epilog section to the help of your commands:

import typer

app = typer.Typer(rich_markup_mode="rich")


@app.command(epilog="Made with :heart: in [blue]Venus[/blue]")
def create(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. │
╰───────────────────────────────────────────────────────────────────╯

Made with ❤ in Venus

restart ↻
Was this page helpful?