Configuration
There are two methods to configure rich-click:
- Decorator: Use the
@rich_config()decorator (andRichHelpConfiguration()). - Globals: Set the global variables in the
rich_config.rich_configmodule.
Configuration using the rich_config decorator
Section titled “Configuration using the rich_config decorator”Initializing a new RichHelpConfiguration object creates a configuration that you can then pass to your CLIs via the rich_config decorator. For example:
def cli(): """Help text here."""RichHelpConfiguration() initializes the default configuration, and the user is able to specify any changes to that default configuration that they’d like. Note that RichHelpConfiguration() is unaware of the global configuration.
You may also specify custom config as a dict:
def cli(): """Help text here."""There is a subtle difference between using a dict and using a RichHelpConfiguration.
Initializing a RichHelpConfiguration creates a fresh config from the defaults, whereas a dict merges to either the parent or (if the parent config does not exist) the global config.
In the below example subcommand’s configuration would get “merged” into my_group’s configuration, meaning that subcommand would inherit the style_option="bold red" style from my_group:
def my_group(): """Help text here."""
@my_group.command()def subcommand(): """Help text here."""Configuration using the global config
Section titled “Configuration using the global config”The other way to configure rich-click is to use the global configuration inside the rich_click.rich_click module:
def my_command(): """Help text here."""Compatibility between RichHelpConfiguration and global config
Section titled “Compatibility between RichHelpConfiguration and global config”You can load the global config into a RichHelpConfiguration using the RichHelpConfiguration.load_from_globals() classmethod:
# The help config will have `style_option = "bold red"`.
def my_command(): """Help text here."""You can also dump a help config into the global config using RichHelpConfiguration().dump_to_globals().
You probably do not need this in most cases; this is mostly for rich-click’s internal use.
help_config.dump_to_globals()
# The CLI will have `style_option = "bold red"` since it pulls from the globals.def my_command(): """Help text here."""Configuration options
Section titled “Configuration options”Below is a full list of configuration options from rich_click.py.
Config
# Default stylesTHEME: str | RichClickTheme | None = NoneENABLE_THEME_ENV_VAR: bool = True
STYLE_OPTION: StyleType = FROM_THEMESTYLE_OPTION_NEGATIVE: StyleType | None = FROM_THEMESTYLE_ARGUMENT: StyleType = FROM_THEMESTYLE_COMMAND: StyleType = FROM_THEMESTYLE_COMMAND_ALIASES: StyleType = FROM_THEMESTYLE_SWITCH: StyleType = FROM_THEMESTYLE_SWITCH_NEGATVE: StyleType | None = FROM_THEMESTYLE_METAVAR: StyleType = FROM_THEMESTYLE_METAVAR_APPEND: StyleType = FROM_THEMESTYLE_METAVAR_SEPARATOR: StyleType = FROM_THEMESTYLE_RANGE_APPEND: StyleType = FROM_THEMESTYLE_USAGE: StyleType = FROM_THEMESTYLE_USAGE_COMMAND: StyleType = FROM_THEMESTYLE_USAGE_SEPARATOR: StyleType = FROM_THEMESTYLE_DEPRECATED: StyleType = FROM_THEMESTYLE_HELPTEXT_FIRST_LINE: StyleType = FROM_THEMESTYLE_HELPTEXT: StyleType = FROM_THEMESTYLE_HELPTEXT_ALIASES: StyleType | None = NoneSTYLE_OPTION_HELP: StyleType = FROM_THEMESTYLE_COMMAND_HELP: StyleType = FROM_THEMESTYLE_OPTION_DEFAULT: StyleType = FROM_THEMESTYLE_OPTION_ENVVAR: StyleType = FROM_THEMESTYLE_REQUIRED_SHORT: StyleType = FROM_THEMESTYLE_REQUIRED_LONG: StyleType = FROM_THEMESTYLE_OPTIONS_PANEL_BORDER: StyleType = FROM_THEMESTYLE_OPTIONS_PANEL_BOX: str | Box | None = FROM_THEMESTYLE_OPTIONS_PANEL_HELP_STYLE: StyleType = FROM_THEMESTYLE_OPTIONS_PANEL_TITLE_STYLE: StyleType = FROM_THEMESTYLE_OPTIONS_PANEL_PADDING: PaddingDimensions = FROM_THEMESTYLE_OPTIONS_PANEL_STYLE: StyleType = FROM_THEMEALIGN_OPTIONS_PANEL: AlignMethod = "left"STYLE_OPTIONS_TABLE_SHOW_LINES: bool = FalseSTYLE_OPTIONS_TABLE_LEADING: int = 0STYLE_OPTIONS_TABLE_PAD_EDGE: bool = FalseSTYLE_OPTIONS_TABLE_PADDING: PaddingDimensions = (0, 1)STYLE_OPTIONS_TABLE_EXPAND: bool = FROM_THEMESTYLE_OPTIONS_TABLE_BOX: str | Box | None = FROM_THEMESTYLE_OPTIONS_TABLE_ROW_STYLES: list[StyleType] | None = NoneSTYLE_OPTIONS_TABLE_BORDER_STYLE: StyleType | None = FROM_THEMESTYLE_COMMANDS_PANEL_BORDER: StyleType = FROM_THEMEPANEL_INLINE_HELP_IN_TITLE: bool = FROM_THEMEPANEL_INLINE_HELP_DELIMITER: str = FROM_THEMESTYLE_COMMANDS_PANEL_BOX: str | Box | None = FROM_THEMESTYLE_COMMANDS_PANEL_HELP_STYLE: StyleType = FROM_THEMESTYLE_COMMANDS_PANEL_TITLE_STYLE: StyleType = FROM_THEMESTYLE_COMMANDS_PANEL_PADDING: PaddingDimensions = FROM_THEMESTYLE_COMMANDS_PANEL_STYLE: StyleType = FROM_THEMEALIGN_COMMANDS_PANEL: AlignMethod = "left"STYLE_COMMANDS_TABLE_SHOW_LINES: bool = FalseSTYLE_COMMANDS_TABLE_LEADING: int = 0STYLE_COMMANDS_TABLE_PAD_EDGE: bool = FalseSTYLE_COMMANDS_TABLE_PADDING: PaddingDimensions = (0, 1)STYLE_COMMANDS_TABLE_EXPAND: bool = FROM_THEMESTYLE_COMMANDS_TABLE_BOX: str | Box | None = FROM_THEMESTYLE_COMMANDS_TABLE_ROW_STYLES: list[StyleType] | None = NoneSTYLE_COMMANDS_TABLE_BORDER_STYLE: StyleType | None = FROM_THEMESTYLE_COMMANDS_TABLE_COLUMN_WIDTH_RATIO: tuple[None, None] | tuple[int, int] | None = (None, None)STYLE_ERRORS_PANEL_BORDER: StyleType = FROM_THEMESTYLE_ERRORS_PANEL_BOX: str | Box | None = FROM_THEMEALIGN_ERRORS_PANEL: AlignMethod = "left"STYLE_ERRORS_SUGGESTION: StyleType | None = NoneSTYLE_ERRORS_SUGGESTION_COMMAND: StyleType | None = NoneSTYLE_PADDING_ERRORS: StyleType = FROM_THEMESTYLE_ABORTED: StyleType = "red"STYLE_PADDING_USAGE: StyleType = FROM_THEMESTYLE_PADDING_HELPTEXT: StyleType = FROM_THEMESTYLE_PADDING_EPILOG: StyleType = FROM_THEMESTYLE_HEADER_TEXT: StyleType = FROM_THEMESTYLE_EPILOG_TEXT: StyleType = FROM_THEMESTYLE_FOOTER_TEXT: StyleType = FROM_THEME
PANEL_TITLE_PADDING: int = FROM_THEMEWIDTH: int | None = terminal_width_default()MAX_WIDTH: int | None = terminal_width_default()COLOR_SYSTEM: Literal["auto", "standard", "256", "truecolor", "windows"] | None = ( "auto" # Set to None to disable colors)FORCE_TERMINAL: bool | None = force_terminal_default()
OPTIONS_TABLE_COLUMN_TYPES: list[OptionColumnType] = FROM_THEMECOMMANDS_TABLE_COLUMN_TYPES: list[CommandColumnType] = FROM_THEME
OPTIONS_TABLE_HELP_SECTIONS: list[OptionHelpSectionType] = FROM_THEMECOMMANDS_TABLE_HELP_SECTIONS: list[CommandHelpSectionType] = FROM_THEME
# Fixed stringsHEADER_TEXT: str | Text | None = NoneFOOTER_TEXT: str | Text | None = NonePANEL_TITLE_STRING: str = FROM_THEMEDEPRECATED_STRING: str = FROM_THEMEDEPRECATED_WITH_REASON_STRING: str = FROM_THEMEDEFAULT_STRING: str = FROM_THEMEENVVAR_STRING: str = FROM_THEMEREQUIRED_SHORT_STRING: str = FROM_THEMEREQUIRED_LONG_STRING: str = FROM_THEMERANGE_STRING: str = FROM_THEMEAPPEND_METAVARS_HELP_STRING: str = FROM_THEMEAPPEND_RANGE_HELP_STRING: str = FROM_THEMEHELPTEXT_ALIASES_STRING: str = "Aliases: {}"ARGUMENTS_PANEL_TITLE: str = "Arguments"OPTIONS_PANEL_TITLE: str = "Options"COMMANDS_PANEL_TITLE: str = "Commands"ERRORS_PANEL_TITLE: str = "Error"DELIMITER_COMMA: str = FROM_THEMEDELIMITER_SLASH: str = FROM_THEMEERRORS_SUGGESTION: str | Text | None = None # Default: Try 'cmd -h' for help. Set to False to disable.ERRORS_EPILOGUE: str | Text | None = NoneABORTED_TEXT: str = "Aborted."
PADDING_HEADER_TEXT: PaddingDimensions = FROM_THEMEPADDING_USAGE: PaddingDimensions = FROM_THEMEPADDING_HELPTEXT: PaddingDimensions = FROM_THEMEPADDING_HELPTEXT_DEPRECATED: PaddingDimensions = 0PADDING_HELPTEXT_FIRST_LINE: PaddingDimensions = 0PADDING_EPILOG: PaddingDimensions = FROM_THEMEPADDING_FOOTER_TEXT: PaddingDimensions = FROM_THEMEPADDING_ERRORS_PANEL: PaddingDimensions = (0, 0, 1, 0)PADDING_ERRORS_SUGGESTION: PaddingDimensions = (0, 1, 0, 1)PADDING_ERRORS_EPILOGUE: PaddingDimensions = (0, 1, 1, 1)
# BehavioursSHOW_ARGUMENTS: bool | None = None # Show positional argumentsSHOW_METAVARS_COLUMN: bool | None = None # Show a column with the option metavar (eg. INTEGER)COMMANDS_BEFORE_OPTIONS: bool = False # If set, the commands panel show above the options panel.DEFAULT_PANELS_FIRST: bool = False # If set, default panels appear before custom panels of the same type.APPEND_METAVARS_HELP: bool | None = None # Append metavar (eg. [TEXT]) after the help textGROUP_ARGUMENTS_OPTIONS: bool = False # Show arguments with options instead of in own panelOPTION_ENVVAR_FIRST: bool | None = None # Show env vars before option help text instead of avertTEXT_MARKUP: Literal["ansi", "rich", "markdown", "rst", None] = notsetTEXT_KWARGS: dict[str, Any] | None = NoneTEXT_EMOJIS: bool = notsetTEXT_PARAGRAPH_LINEBREAKS: Literal["\n", "\n\n"] | None = None# If set, parse emoji codes and replace with actual emojis, e.g. :smiley_cat: -> 😺USE_MARKDOWN: bool | None = None # Parse help strings as markdownUSE_MARKDOWN_EMOJI: bool | None = None # Parse emoji codes in markdown :smile:USE_RICH_MARKUP: bool | None = None # Parse help strings for rich markup (eg. [red]my text[/])# Define sorted groups of panels to display subcommandsCOMMAND_GROUPS: dict[str, list[CommandGroupDict]] = {}# Define sorted groups of panels to display options and argumentsOPTION_GROUPS: dict[str, list[OptionGroupDict]] = {}USE_CLICK_SHORT_HELP: bool = False # Use click's default function to truncate help textHELPTEXT_SHOW_ALIASES: bool = TrueAll of these are available in the RichHelpConfiguration object, but as lowercase.
(Advanced) Config resolution order
Section titled “(Advanced) Config resolution order”It probably should not matter for most use cases, but just case it does matter, there is an explicitly defined order of operations for how the configuration gets resolved:
flowchart TD A["Did you pass in a @rich_config(help_config=...)?"] A --> |Yes| Ayes A --> |No| Ano Ayes["Was it a dict or a RichHelpConfiguration?"] Ayes --> |dict| AyesBdict Ayes --> |RichHelpConfiguration| AyesBrhc AyesBdict["Is there a 'parent' config?"] AyesBdict --> |Yes| AyesBdictCyes AyesBdict --> |No| AyesBdictCno AyesBdictCyes:::StoppingPoint AyesBdictCyes["Merge into the parent config, and use that"] AyesBdictCno:::StoppingPoint AyesBdictCno["Merge into the global config, and use that"] AyesBrhc:::Wide AyesBrhc["<font style="font-weight: 600;">Use the RichHelpConfiguration object.</font><br>(Note: RichHelpConfiguration's defaults are independent of the global config.)"] Ano["Is there a 'parent' config?"] Ano --> |Yes| AnoByes Ano --> |No| AnoBno AnoByes:::StoppingPoint AnoByes["Use the parent config"] AnoBno:::StoppingPoint AnoBno["Use the global config"] classDef StoppingPoint font-weight: 600; classDef Wide padding: 8.5em;
(Advanced) Themes
Section titled “(Advanced) Themes”Config options controlled by themes are assigned a sentinel value.
When a RichHelpConfiguration() object is created, if the theme is defined, then the theme is applied to any unset config values.
Other config resolution rules still apply.
So for example, the below will not apply the "green2-nu" theme to the CLI
because of the config resolution rule which states that config = RichHelpConfiguration() will not pull from globals:
# The following line will NOT do anything because the RichHelpConfiguration() below# completely ignores the the global config.
# This will NOT have the green2-nu theme.
def my_command(): """Help text here."""
def my_command(): """Help text here."""
def my_command(): """Help text here."""
def my_command(): """Help text here."""
# This works because of the config rule that dicts get merged# into the global config
def my_command(): """Help text here."""When we go to render help text, if there is still no theme, then the default theme (default-box) is applied.
In the event that there are multiple themes being assigned from different sources, one theme must be selected. The priority order of which theme gets selected is defined below (highest priority is #1):
- The theme defined in the
rich-clickCLI:rich-click --theme [theme] - Environment variable:
RICH_CLICK_THEME=[theme] - The
themeof the command’s config
Handling overrides and custom themes
Section titled “Handling overrides and custom themes”The user can override the theme that a developer sets because all end-user settings (CLI and env var) are prioritized above things set by the developer (command config).
Note that themes never override existing config options, so explicitly set options always take precedence over a theme. This can cause potentially unintended side effects where your style settings clash with the user’s theme. There are three ways around this.
The first is to do nothing. For many config options, like commands_before_options or options_panel_title, the theme should not make any real difference, or the config option may not even be stylistic in nature. Theme overrides only become a real issue when a user’s RICH_CLICK_THEME clashes stylistically with your custom settings.
The second way is to explicitly prevent theme overrides by setting the config option enable_theme_env_var = False. This option prevents all overrides when enabled.
def cli(): """My CLI help text"""
def cli(): """My CLI help text"""
def cli(): """My CLI help text"""The final way is to apply your config as a theme. Basically, since themes are applied last and can be overridden, then if you have your config applied as a theme, then it can be overridden. RichHelpConfigurations have a method called to_theme which generates a theme from your config:
theme = config.to_theme()
# This can be overridden.def cli(): """My CLI help text"""A more advanced pattern, which lets you make some options overridable by a theme, while keeping others immutable:
# All of these config options can be overridden style_option="bold red", style_command="bold green").to_theme()
# Here, `style_metavar` will not be overridden by the RICH_CLICK_THEME env var. theme=theme, style_metavar="magenta",)
def cli(): """My CLI help text"""