Panels - Overview
rich-click lets you configure grouping and sorting of command options and subcommands. The containers which contain grouped options and subcommands are called panels:
$ python panels_overview.py --help╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Commands ───────────────────────────────────────────────────────────────────╮ │ move-item Move an item from a src location to a dest location │ ╰──────────────────────────────────────────────────────────────────────────────╯
By default, RichCommands have a single panel for options named “Options”, and RichGroups have an additional panel for commands named “Commands”.
rich-click allows you to control and customize everything about these panels:
- The default panels can be renamed and stylized.
- Options and commands can be split up across multiple panels.
- Positional arguments can be given a separate panel, or included in options panels.
- The styles of these panels can be modified.
Introduction to API
Section titled “Introduction to API”The high-level API for defining panels is with the @click.command_panel() and @click.option_panel() decorators.
Under the hood, these decorators create RichPanel objects that are attached to the command.
Options
Section titled “Options”Options panels handle parameters for your command:
# /// script# dependencies = ["rich-click>=1.9"]# ///
options=["--src", "--dest"]) options=["--env", "--log-level", "--help", "--version"])def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_simple_decorators.py --helpUsage: panels_simple_decorators.py [OPTIONS] Move an item from a src location to a dest location ╭─ Main ───────────────────────────────────────────────────────────────────────╮ │ --src TEXT Source │ │ --dest TEXT Destination │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Extra ──────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ │ --version Show the version and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Alternatively, you can configure panels within the option itself. If the panel is not created in a decorator, then one is created on the fly.
The following code generates the same output as the example above:
# /// script# dependencies = ["rich-click>=1.9"]# ///
def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
Note that this output is the same as the previous example, even though it was defined differently.
$ python panels_simple_kwargs.py --helpUsage: panels_simple_kwargs.py [OPTIONS] Move an item from a src location to a dest location ╭─ Main ───────────────────────────────────────────────────────────────────────╮ │ --src TEXT Source │ │ --dest TEXT Destination │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Extra ──────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ │ --version Show the version and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Arguments
Section titled “Arguments”Despite the name, options panels handle more than just options; they can also handle positional arguments.
Arguments can be given their own panel with the show_arguments config option:
# /// script# dependencies = ["rich-click>=1.9"]# ///
def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_simple_arguments.py --helpUsage: panels_simple_arguments.py [OPTIONS] SRC DEST Move an item from a src location to a dest location ╭─ Arguments ──────────────────────────────────────────────────────────────────╮ │ * SRC TEXT [required] │ │ * DEST TEXT [required] │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Arguments can also be included in the options panel with the group_arguments_options config option (the show_arguments config option does not need to be set).
# /// script# dependencies = ["rich-click>=1.9"]# ///
def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_simple_arguments_combined.py --helpUsage: panels_simple_arguments_combined.py [OPTIONS] SRC DEST Move an item from a src location to a dest location ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ * SRC TEXT [required] │ │ * DEST TEXT [required] │ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
In rich-click, unlike base Click, arguments can have help text.
If help= if set for arguments, then the argument panel is automatically shown:
# /// script# dependencies = ["rich-click>=1.9"]# ///
def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_simple_arguments_help.py --helpUsage: panels_simple_arguments_help.py [OPTIONS] SRC DEST Move an item from a src location to a dest location ╭─ Arguments ──────────────────────────────────────────────────────────────────╮ │ * SRC TEXT Source [required] │ │ * DEST TEXT Destination [required] │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Commands
Section titled “Commands”Sub-commands also have panels that are defined similarly to option panels:
# /// script# dependencies = ["rich-click>=1.9"]# ///
commands=["move-item", "update-item"]) commands=["create-user", "update-user"], help="User management commands")def cli(): """CLI""" pass
@cli.command()def move_item(): """Move an item""" pass
@cli.command()def update_item(): """Update an item""" pass
@cli.command()def create_user(): """Create a user""" pass
@cli.command()def update_user(): """Update a user""" pass
if __name__ == "__main__": cli()Output
$ python panels_commands.py --helpUsage: panels_commands.py [OPTIONS] COMMAND [ARGS]... CLI ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Items ──────────────────────────────────────────────────────────────────────╮ │ move-item Move an item │ │ update-item Update an item │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Users ──────────────────────────────────────────────────────────────────────╮ │ User management commands │ │ create-user Create a user │ │ update-user Update a user │ ╰──────────────────────────────────────────────────────────────────────────────╯
Styles & Panel Help
Section titled “Styles & Panel Help”RichPanel objects inherit their base style behaviors from the rich config by default, but this can be set on a per-panel basis.
Panels can also have help text.
The below example shows both of these things:
# /// script# dependencies = ["rich-click>=1.9"]# ///
help="All of the options available", help_style="green", panel_styles={"box": "DOUBLE"}, table_styles={ "row_styles": ["dim on rgb(16,16,32)", "on rgb(32,32,72)"], "caption": "The arguments are optional" })def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_extra_kwargs.py --helpUsage: panels_extra_kwargs.py [OPTIONS] Move an item from a src location to a dest location ╔═ Options ════════════════════════════════════════════════════════════════════╗ ║ All of the options available ║ ║ --src TEXT ║ ║ --dest TEXT ║ ║ --env TEXT ║ ║ --log-level TEXT ║ ║ --help Show this message and exit. ║ ║ The arguments are optional ║ ╚══════════════════════════════════════════════════════════════════════════════╝
The panel_styles is passed into the outer rich.panel.Panel(), and the table_styles dict is pass as kwargs into the inner rich.table.Table().
See the available arguments for the rich library Table and Panel objects for more information:
Overriding defaults
Section titled “Overriding defaults”Default panel titles can be overridden with the config. Renamed panels can still have their panel-level configurations modified.
# /// script# dependencies = ["rich-click>=1.9"]# ///
panel_styles={"border_style": "dim blue"}) panel_styles={"border_style": "dim magenta"}) "arguments_panel_title": "Very Important Required Args", "options_panel_title": "Some Additional Options", "commands_panel_title": "My Tool's Subcommands", "show_arguments": True})def cli(env, log_level): """My CLI"""
@cli.command()def move_item(src, dest): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": cli()Output
$ python panels_defaults_renamed.py --helpUsage: panels_defaults_renamed.py [OPTIONS] COMMAND [ARGS]... My CLI ╭─ Some Additional Options ────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ My Tool's Subcommands ──────────────────────────────────────────────────────╮ │ move-item Move an item from a src location to a dest location │ ╰──────────────────────────────────────────────────────────────────────────────╯
Note that the rich config passes to subcommands, but panels are defined at the command level.
So running move-item --help from the above example will rename the children’s panels (because that’s set in the parent’s config), but it does not pass the panel_styles= to the subcommand:
Output
$ python panels_defaults_renamed.py move-item --helpUsage: panels_defaults_renamed.py move-item [OPTIONS] SRC DEST Move an item from a src location to a dest location ╭─ Very Important Required Args ───────────────────────────────────────────────╮ │ * SRC TEXT [required] │ │ * DEST TEXT [required] │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Some Additional Options ────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Default panel styles are also handled by the config, and will be overridden when conflicting options are defined at the panel level.
# /// script# dependencies = ["rich-click>=1.9"]# ///
# 'Main' panel: uses default title style "bright_red"# 'Extra' panel: title style is overridden to use "blue"
"theme": "plain", "style_options_panel_title_style": "bright_red" # <- default title style})def move_item(src, dest, env, log_level): """Move an item from a src location to a dest location""" pass
if __name__ == "__main__": move_item()Output
$ python panels_defaults_override_config.py --helpUsage: panels_defaults_override_config.py [OPTIONS] SRC DEST Move an item from a src location to a dest location ╭─ Main ───────────────────────────────────────────────────────────────────────╮ │ * SRC TEXT Source [required] │ │ * DEST TEXT Destination [required] │ ╰──────────────────────────────────────────────────────────────────────────────╯ ╭─ Extra ──────────────────────────────────────────────────────────────────────╮ │ --env TEXT Environment │ │ --log-level TEXT Log level │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯