Format click help output nicely with Rich.
- Click is a "Python package for creating beautiful command line interfaces".
- Rich is a "Python library for rich text and beautiful formatting in the terminal".
The intention of rich-click
is to provide attractive help output from
click, formatted with rich, with minimal customisation required.
Native click help | With rich-click |
---|---|
You can install rich-click
from the Python Package Index (PyPI) with pip
or equivalent.
python -m pip install rich-click
To use rich-click
, import it instead of click
but under the same namespace:
import rich_click as click
That's it. Then continue to use click
as you would normally.
The intention is to maintain most / all of the normal click functionality and arguments. If you spot something that is missing once you start using the plugin, please create an issue about it.
rich-click
gives functionality to list subcommands in groups, printed as separate panels.
It accepts a list of commands which means you can also choose a custom sorting order.
For example, you can produce something that looks like this:
To do this, set rich_click.core.COMMAND_GROUPS
.
In this example, we create two groups of commands for the base command of mytool
.
Any subcommands not listed will automatically be printed in a panel at the end labelled "Commands" as usual.
click.core.COMMAND_GROUPS = {
"mytool": [
{
"name": "Commands for uploading",
"commands": ["sync", "upload"],
},
{
"name": "Download data",
"commands": ["get", "fetch", "download"],
},
]
}
If you omit name
it will use Commands
(can be configured with COMMANDS_PANEL_TITLE
, see below).
If you use nested subcommands, you can specify multiple base paths using the base dictionary keys:
click.core.COMMAND_GROUPS = {
"mytool": ["commands": ["sync", "auth"]],
"mytool sync": [
{
"name": "Commands for uploading",
"commands": ["sync", "upload"],
},
{
"name": "Download data",
"commands": ["get", "fetch", "download"],
},
],
"mytool auth":[{"commands": ["login", "logout"]}],
}
You can customise most things that are related to formatting.
For example, to limit the maximum width of the help output to 100 characters:
click.core.MAX_WIDTH = 100
To print the option flags in a different colour, use:
click.core.STYLE_OPTION = "magenta"
Full list of config options
# Default colours
STYLE_OPTION = "bold cyan"
STYLE_SWITCH = "bold green"
STYLE_METAVAR = "bold yellow"
STYLE_USAGE = "yellow"
STYLE_USAGE_COMMAND = "bold"
STYLE_DEPRECATED = "red"
STYLE_HELPTEXT_FIRST_LINE = ""
STYLE_HELPTEXT = "dim"
STYLE_METAVAR = "bold yellow"
STYLE_OPTION_HELP = ""
STYLE_OPTION_DEFAULT = "dim"
STYLE_REQUIRED_SHORT = "red"
STYLE_REQUIRED_LONG = "dim red"
STYLE_OPTIONS_PANEL_BORDER = "dim"
ALIGN_OPTIONS_PANEL = "left"
STYLE_COMMANDS_PANEL_BORDER = "dim"
ALIGN_COMMANDS_PANEL = "left"
MAX_WIDTH = None # Set to an int to limit to that many characters
# Fixed strings
DEPRECATED_STRING = "(Deprecated) "
DEFAULT_STRING = " [default: {}]"
REQUIRED_SHORT_STRING = "*"
REQUIRED_LONG_STRING = " [required]"
OPTIONS_PANEL_TITLE = "Options"
COMMANDS_PANEL_TITLE = "Commands"
# Behaviours
SHOW_ARGUMENTS = False
Contributions and suggestions for new features are welcome, as are bug reports! Please create a new issue or better still, dive right in with a pull-request.
This package was put together hastily by Phil Ewels (@ewels), but the hard work was really done by Will McGugan (@willmcgugan) who not only is the author of Rich but also wrote the original code that this package is based on.