Format click help output nicely with rich
Project description
rich-click
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.
Features
- 🌈 Rich command-line formatting of click help and error messages
- 💫 Nice styles be default, usage is simply
import rich_click as click
- 🎁 Group commands and options into named panels
- ❌ Well formatted error messages
- 🔢 Easily give custom sort order for options and commands
- 🎨 Extensive customisation of styling and behaviour possible
Screenshot from examples/03_groups_sorting.py
Installation
You can install rich-click
from the Python Package Index (PyPI) with pip
or equivalent.
python -m pip install rich-click
Usage
To use rich-click
, switch out your normal click
import with rich-click
, using the same namespace:
import rich_click as click
That's it ✨ Then continue to use click
as you would normally.
See
examples/01_simple.py
for an example.
The intention is to maintain most / all of the normal click functionality and arguments. If you spot something that breaks or is missing once you start using the plugin, please create an issue about it.
Alternatively, if you prefer you can RichGroup
or RichCommand
with the cls
argument in your click usage instead.
This means that you can continue to use the unmodified click
package in parallel.
See
examples/02_declarative.py
for an example.
Customisation
There are a large number of customisation options in rich-click.
These can be modified by changing variables in the click.rich_click
namespace.
Note that most normal click options should still work, such as show_default=True
, required=True
and hidden=True
.
Using rich markup
In order to be as widely compatible as possible with a simple import, rich-click does not parse rich formatting markup (eg. [red]
) by default. You need to opt-in to this behaviour.
To use rich markup in your help texts, add the following:
click.rich_click.USE_RICH_MARKUP = True
Remember that you'll need to escape any regular square brackets using a back slash in your help texts,
for example: [dim]\[my-default: foo][\]
See
examples/04_rich_markup.py
fo
Using Markdown
If you prefer, you can use Markdown text. You must choose either Markdown or rich markup. If you specify both, Markdown takes preference.
click.rich_click.USE_MARKDOWN = True
See
examples/05_markdown.py
fo
Positional arguments
The default click behaviour is to only show positional arguments in the top usage string, and not in the list below with the options.
If you prefer, you can tell rich-click to show arguments with SHOW_ARGUMENTS
.
By default, they will get their own panel but you can tell rich-click to bundle them together with GROUP_ARGUMENTS_OPTIONS
:
click.rich_click.SHOW_ARGUMENTS = True
click.rich_click.GROUP_ARGUMENTS_OPTIONS = True
See
examples/06_arguments.py
for an example.
Metavars
Metavars are click's way of showing expected input types.
For example, if you have an option that must be an integer, the metavar is INTEGER
.
If you have a choice, the metavar is a list of the possible values.
By default, rich-click shows metavars in their own column. However, with some tools this column can be quite wide and result in a lot of white space. It may look better to show metavars appended to the help text, instead of in their own column. For this, use the following:
click.rich_click.SHOW_METAVARS_COLUMN = False
click.rich_click.APPEND_METAVARS_HELP = True
Error messages
By default, rich-click gives some nice formatting to error messages:
You can customise the Try 'command --help' for help. message with ERRORS_SUGGESTION
using rich-click though, and add some text after the error with ERRORS_EPILOGUE
.
For example, from examples/07_custom_errors.py
:
click.rich_click.STYLE_ERRORS_SUGGESTION = "blue italic"
click.rich_click.ERRORS_SUGGESTION = "Try running the '--help' flag for more information."
click.rich_click.ERRORS_EPILOGUE = "To find out more, visit https://mytool.com"
Help width
The default behaviour of rich-click is to use the full width of the terminal for output. However, if you've carefully crafted your help texts for the default narrow click output, you may find that you now have a lot of whitespace at the side of the panels.
To limit the maximum width of the help output, set MAX_WIDTH
in characters, as follows:
click.rich_click.MAX_WIDTH = 100
Styling
Most aspects of rich-click formatting can be customised, from colours to alignment.
For example, to print the option flags in a different colour, you can use:
click.rich_click.STYLE_OPTION = "magenta"
See the Configuration options section below for the full list of available optoins.
Groups and sorting
rich-click
gives functionality to list options and subcommands in groups, printed as separate panels.
It accepts a list of options / commands which means you can also choose a custom sorting order.
- For options (flags), set
click.rich_click.OPTION_GROUPS
- For subcommands (groups), set
click.rich_click.COMMAND_GROUPS
See examples/03_groups_sorting.py
for a full example.
Options
To group option flags into two sections with custom names, see the following example:
click.rich_click.OPTION_GROUPS = {
"mytool": [
{
"name": "Simple options",
"options": ["--name", "--description", "--version", "--help"],
},
{
"name": "Advanced options",
"options": ["--force", "--yes", "--delete"],
},
]
}
If you omit name
it will use Commands
(can be configured with OPTIONS_PANEL_TITLE
).
Commands
Here 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.rich_click.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
).
Multiple commands
If you use multiple nested subcommands, you can specify their commands using the top-level dictionary keys:
click.rich_click.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"]}],
}
Configuration options
Here is the full list of config options:
# Default styles
STYLE_OPTION = "bold cyan"
STYLE_SWITCH = "bold green"
STYLE_METAVAR = "bold yellow"
STYLE_METAVAR_APPEND = "dim yellow"
STYLE_USAGE = "yellow"
STYLE_USAGE_COMMAND = "bold"
STYLE_DEPRECATED = "red"
STYLE_HELPTEXT_FIRST_LINE = ""
STYLE_HELPTEXT = "dim"
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"
STYLE_ERRORS_PANEL_BORDER = "red"
ALIGN_ERRORS_PANEL = "left"
STYLE_ERRORS_SUGGESTION = "dim"
MAX_WIDTH = None # Set to an int to limit to that many characters
COLOR_SYSTEM = "auto" # Set to None to disable colors
# Fixed strings
DEPRECATED_STRING = "(Deprecated) "
DEFAULT_STRING = "[default: {}]"
REQUIRED_SHORT_STRING = "*"
REQUIRED_LONG_STRING = "[required]"
RANGE_STRING = " [{}]"
APPEND_METAVARS_HELP_STRING = "({})"
ARGUMENTS_PANEL_TITLE = "Arguments"
OPTIONS_PANEL_TITLE = "Options"
COMMANDS_PANEL_TITLE = "Commands"
ERRORS_PANEL_TITLE = "Error"
ERRORS_SUGGESTION = None # Default: Try 'cmd -h' for help. Set to False to disable.
ERRORS_EPILOGUE = None
# Behaviours
SHOW_ARGUMENTS = False # Show positional arguments
SHOW_METAVARS_COLUMN = True # Show a column with the option metavar (eg. INTEGER)
APPEND_METAVARS_HELP = False # Append metavar (eg. [TEXT]) after the help text
GROUP_ARGUMENTS_OPTIONS = False # Show arguments with options instead of in own panel
USE_MARKDOWN = False # Parse help strings as markdown
USE_RICH_MARKUP = False # Parse help strings for rich markup (eg. [red]my text[/])
COMMAND_GROUPS = {}
OPTION_GROUPS = {}
Contributing
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.
Credits
This package was written by Phil Ewels (@ewels), based on initial code by Will McGugan (@willmcgugan).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rich-click-1.0.0.tar.gz
.
File metadata
- Download URL: rich-click-1.0.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37773c4bdc9766c949f95876f346e5b08ae65b5691ba749a64e5d67fd607e8e9 |
|
MD5 | c8ccc08a46264cd7ce119b019d950ff3 |
|
BLAKE2b-256 | f91f68632006aa47d3ea9cd44acfe069c41ee5a793bcc9d9b9d4af2f0dad5f6f |
File details
Details for the file rich_click-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: rich_click-1.0.0-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c44e726d0f448bd5430f1d0075a3b6d150d2b51cd4260388afcd9eafff3f86f |
|
MD5 | 3740f3e66d11b159313619188a76d4e0 |
|
BLAKE2b-256 | 1cef1b5c7722d1c10e942bb331b072db348b212a52a45a889e580bb3669946d8 |