Format click help output nicely with rich
—
Comprehensive configuration system for customizing Rich-Click appearance, behavior, and styling. The configuration system provides extensive options for colors, panels, tables, text formatting, and behavioral settings to create visually appealing command-line interfaces.
The rich_click.rich_click module contains global configuration constants that control default styling and behavior across all Rich-Click commands.
# Style constants
STYLE_OPTION: str = "bold cyan"
STYLE_ARGUMENT: str = "bold cyan"
STYLE_COMMAND: str = "bold cyan"
STYLE_SWITCH: str = "bold green"
STYLE_METAVAR: str = "bold yellow"
STYLE_METAVAR_APPEND: str = "dim yellow"
STYLE_METAVAR_SEPARATOR: str = "dim"
STYLE_HEADER_TEXT: str = ""
STYLE_EPILOG_TEXT: str = ""
STYLE_FOOTER_TEXT: str = ""
STYLE_USAGE: str = "yellow"
STYLE_USAGE_COMMAND: str = "bold"
STYLE_DEPRECATED: str = "red"
STYLE_HELPTEXT_FIRST_LINE: str = ""
STYLE_HELPTEXT: str = "dim"
STYLE_OPTION_HELP: str = ""
STYLE_OPTION_DEFAULT: str = "dim"
STYLE_OPTION_ENVVAR: str = "dim yellow"
STYLE_REQUIRED_SHORT: str = "red"
STYLE_REQUIRED_LONG: str = "dim red"
STYLE_ABORTED: str = "red"
# Panel styling constants
STYLE_OPTIONS_PANEL_BORDER: str = "dim"
STYLE_OPTIONS_PANEL_BOX: Optional[str] = "ROUNDED"
ALIGN_OPTIONS_PANEL: str = "left"
STYLE_COMMANDS_PANEL_BORDER: str = "dim"
STYLE_COMMANDS_PANEL_BOX: Optional[str] = "ROUNDED"
ALIGN_COMMANDS_PANEL: str = "left"
STYLE_ERRORS_PANEL_BORDER: str = "red"
STYLE_ERRORS_PANEL_BOX: Optional[str] = "ROUNDED"
ALIGN_ERRORS_PANEL: str = "left"
# Table styling constants
STYLE_OPTIONS_TABLE_SHOW_LINES: bool = False
STYLE_OPTIONS_TABLE_LEADING: int = 0
STYLE_OPTIONS_TABLE_PAD_EDGE: bool = False
STYLE_OPTIONS_TABLE_PADDING: tuple = (0, 1)
STYLE_OPTIONS_TABLE_BOX: Optional[str] = ""
STYLE_OPTIONS_TABLE_ROW_STYLES: Optional[List[str]] = None
STYLE_OPTIONS_TABLE_BORDER_STYLE: Optional[str] = None
STYLE_COMMANDS_TABLE_SHOW_LINES: bool = False
STYLE_COMMANDS_TABLE_LEADING: int = 0
STYLE_COMMANDS_TABLE_PAD_EDGE: bool = False
STYLE_COMMANDS_TABLE_PADDING: tuple = (0, 1)
STYLE_COMMANDS_TABLE_BOX: Optional[str] = ""
STYLE_COMMANDS_TABLE_ROW_STYLES: Optional[List[str]] = None
STYLE_COMMANDS_TABLE_BORDER_STYLE: Optional[str] = None
STYLE_COMMANDS_TABLE_COLUMN_WIDTH_RATIO: Optional[tuple] = (None, None)
# Terminal configuration
WIDTH: Optional[int] = None
MAX_WIDTH: Optional[int] = None
COLOR_SYSTEM: Optional[str] = "auto"
FORCE_TERMINAL: Optional[bool] = None
# Text constants
HEADER_TEXT: Optional[str] = None
FOOTER_TEXT: Optional[str] = None
DEPRECATED_STRING: str = "(Deprecated) "
DEPRECATED_WITH_REASON_STRING: str = "(Deprecated: {}) "
DEFAULT_STRING: str = "[default: {}]"
ENVVAR_STRING: str = "[env var: {}]"
REQUIRED_SHORT_STRING: str = "*"
REQUIRED_LONG_STRING: str = "[required]"
RANGE_STRING: str = " [{}]"
APPEND_METAVARS_HELP_STRING: str = "({})"
ARGUMENTS_PANEL_TITLE: str = "Arguments"
OPTIONS_PANEL_TITLE: str = "Options"
COMMANDS_PANEL_TITLE: str = "Commands"
ERRORS_PANEL_TITLE: str = "Error"
ERRORS_SUGGESTION: Optional[str] = None
ERRORS_EPILOGUE: Optional[str] = None
ABORTED_TEXT: str = "Aborted."
# Behavior constants
SHOW_ARGUMENTS: bool = False
SHOW_METAVARS_COLUMN: bool = True
APPEND_METAVARS_HELP: bool = False
GROUP_ARGUMENTS_OPTIONS: bool = False
OPTION_ENVVAR_FIRST: bool = False
TEXT_MARKUP: str = "ansi"
USE_MARKDOWN: bool = False
USE_MARKDOWN_EMOJI: bool = True
USE_RICH_MARKUP: bool = False
COMMAND_GROUPS: Dict[str, List[CommandGroupDict]] = {}
OPTION_GROUPS: Dict[str, List[OptionGroupDict]] = {}
USE_CLICK_SHORT_HELP: bool = FalseHelper functions for configuration management and terminal detection.
def force_terminal_default() -> Optional[bool]:
"""
Use as the default factory for `force_terminal`.
Checks environment variables (FORCE_COLOR, PY_COLORS, GITHUB_ACTIONS)
to determine if terminal formatting should be forced.
Returns:
Optional[bool]: Terminal force setting or None
"""
def terminal_width_default() -> Optional[int]:
"""
Use as the default factory for `width` and `max_width`.
Reads TERMINAL_WIDTH environment variable to set terminal width.
Returns:
Optional[int]: Terminal width or None
Raises:
UserWarning: If TERMINAL_WIDTH cannot be cast to integer
"""Configuration for organizing commands and options into named panels with custom styling.
# Command grouping configuration
COMMAND_GROUPS: Dict[str, List[CommandGroupDict]] = {}
# Option grouping configuration
OPTION_GROUPS: Dict[str, List[OptionGroupDict]] = {}
# Type definitions for grouping
CommandGroupDict = TypedDict('CommandGroupDict', {
'name': NotRequired[str],
'commands': List[str],
'table_styles': NotRequired[Dict[str, Any]],
'panel_styles': NotRequired[Dict[str, Any]],
'deduplicate': NotRequired[bool]
})
OptionGroupDict = TypedDict('OptionGroupDict', {
'name': NotRequired[str],
'options': NotRequired[List[str]],
'table_styles': NotRequired[Dict[str, Any]],
'panel_styles': NotRequired[Dict[str, Any]],
'deduplicate': NotRequired[bool]
})Usage examples for grouping:
import rich_click as click
# Configure command groups
click.COMMAND_GROUPS = {
"mycli": [
{
"name": "Database Commands",
"commands": ["init-db", "migrate", "seed"]
},
{
"name": "Server Commands",
"commands": ["run", "debug", "test"]
}
]
}
# Configure option groups
click.OPTION_GROUPS = {
"mycli": [
{
"name": "Configuration Options",
"options": ["--config", "--env", "--debug"]
},
{
"name": "Output Options",
"options": ["--verbose", "--quiet", "--format"]
}
]
}
@click.group()
def mycli():
"""My CLI application."""
pass
@mycli.command()
@click.option("--config", help="Configuration file")
@click.option("--verbose", is_flag=True, help="Verbose output")
def init_db():
"""Initialize database."""
passExamples of common styling customizations:
import rich_click as click
# Customize colors
click.STYLE_OPTION = "bold blue"
click.STYLE_COMMAND = "bold green"
click.STYLE_USAGE = "bold yellow"
# Customize panel appearance
click.STYLE_OPTIONS_PANEL_BOX = "DOUBLE"
click.STYLE_COMMANDS_PANEL_BOX = "HEAVY"
click.OPTIONS_PANEL_TITLE = "Available Options"
click.COMMANDS_PANEL_TITLE = "Available Commands"
# Enable arguments display
click.SHOW_ARGUMENTS = True
# Enable markdown formatting
click.TEXT_MARKUP = "markdown"
# Customize error styling
click.STYLE_ERRORS_PANEL_BORDER = "bold red"
click.ERRORS_PANEL_TITLE = "Error Details"
@click.command()
@click.option("--name", help="Your name")
@click.argument("count", type=int)
def hello(name, count):
"""Say hello with custom styling."""
for _ in range(count):
click.echo(f"Hello, {name}!")The global constants integrate with RichHelpConfiguration for per-command customization:
import rich_click as click
# Create configuration from globals
config = click.RichHelpConfiguration.load_from_globals()
# Modify specific settings
config.style_option = "bold magenta"
config.show_arguments = True
config.options_panel_title = "Configuration"
# Apply to specific command
@click.command()
@click.rich_config(config)
def hello():
"""Command with custom configuration."""
click.echo("Hello!")
# Or dump back to globals
config.dump_to_globals()Rich-Click supports multiple text markup formats for help text:
import rich_click as click
# ANSI markup (default)
click.TEXT_MARKUP = "ansi"
# Rich markup
click.TEXT_MARKUP = "rich"
click.USE_RICH_MARKUP = True
# Markdown markup
click.TEXT_MARKUP = "markdown"
click.USE_MARKDOWN = True
click.USE_MARKDOWN_EMOJI = True
@click.command()
def hello():
"""
Say hello with [bold]rich[/bold] formatting.
This command supports **markdown** and :smile: emoji when configured.
"""
click.echo("Hello!")Some configuration options are deprecated but still supported:
# Deprecated - use text_markup instead
USE_MARKDOWN: bool = False
USE_RICH_MARKUP: bool = False
# Deprecated function access
def get_module_help_configuration():
"""Deprecated. Use RichHelpConfiguration.load_from_globals() instead."""Install with Tessl CLI
npx tessl i tessl/pypi-rich-click