Text Markup & Formatting
Markup
Section titled “Markup”rich-click supports 5 different values for text_markup, which determines how text is rendered:
'ansi': (Default) Rendered as plain text with ANSI escape codes handled.'rich': Rendered using Rich’s markup syntax.'markdown': Rendered with markdown.'rst': Rendered as reStructuredText (requires therstextra).None: Rendered as plain text, ANSI escape codes are not handled.
rich-click ≥1.8.0 deprecation
Prior to rich-click 1.8.0, markup was controlled by the booleans use_rich_markup and use_markdown.
These booleans have been silently deprecated (read: they will still be supported for the distant future),
and users are encouraged to use the text_markup config option instead.
Rich markup
Section titled “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.
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][\]
For more information, read the Rich docs on markup and styles.
help_config = {"text_markup": "rich"}help_config = click.RichHelpConfiguration(text_markup="rich")click.rich_click.TEXT_MARKUP = "rich"$ python examples/04_rich_markup.py --helpUsage: 04_rich_markup.py [OPTIONS] My amazing tool does all the things. This is a minimal example based on documentation from the 'click' package. You can try using --help at the top level and also for specific group subcommands. ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --input PATH Input file. [default: a custom default] │ │ --type TEXT Type of file to sync [default: files] │ │ --all Sync all the things? │ │ --debug Enable 👉 debug mode 👈 │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
See
examples/04_rich_markup.pyfor an example.
Markdown
Section titled “Markdown”If you prefer, you can use Markdown text.
help_config = {"text_markup": "markdown"}help_config = click.RichHelpConfiguration(text_markup="markdown")click.rich_click.TEXT_MARKUP = "markdown"$ python examples/05_markdown.py --helpUsage: 05_markdown.py [OPTIONS] My amazing tool does all the things. This is a minimal example based on documentation from the click package. ▌ Remember: ▌ • You can try using --help at the top level ▌ • Also for specific group subcommands. ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --input PATH Input file. [default: a custom default] │ │ --type TEXT Type of file to sync │ │ [default: files] │ │ --all Sync │ │ │ │ 1 all │ │ 2 the │ │ 3 things? │ │ --debug Enable debug mode │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
See
examples/05_markdown.pyfor an example.
reStructuredText
Section titled “reStructuredText”You can also write help text in reStructuredText, which is handy if your docstrings are also used by Sphinx. Rendering is done by rich-rst, which is an optional dependency:
pip install 'rich-click[rst]'help_config = {"text_markup": "rst"}help_config = click.RichHelpConfiguration(text_markup="rst")click.rich_click.TEXT_MARKUP = "rst"Admonitions (.. note::, .. warning::, etc.) are rendered compactly by default to suit help text,
and :: literal blocks are highlighted as shell sessions (default_lexer="console").
Click’s \b no-rewrap paragraphs keep their line breaks and indentation, though inline markup in them shows as plain text.
The help text is rendered as one document, so padding_helptext_first_line pads all of it.
Any text_kwargs are passed to rich_rst.RestructuredText(), for example
text_kwargs={"admonition_style": "panel", "code_theme": "github-dark"}.
$ python examples/14_rst.py --helpUsage: 14_rst.py [OPTIONS] My amazing tool does all the things. This is a minimal example based on documentation from the click package. • You can try using --help at the top level • Also for specific group subcommands. Note: Admonitions are rendered compactly. Example: ┌──────────────────────────── console (default) ─────────────────────────────┐ │ $ cli --all --debug │ └────────────────────────────────────────────────────────────────────────────┘ ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --input PATH Input file. [default: a custom default] │ │ --type TEXT Type of file to sync │ │ [default: files] │ │ --all Sync │ │ │ │ 1. all │ │ 2. the │ │ 3. things? │ │ --debug Enable debug mode │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ See the rich-click docs for more. • Epilog lists • stay intact
See
examples/14_rst.pyfor an example.
Markup in text
Section titled “Markup in text”The selected text_markup is used to render all text in your CLI help that can be set in the high-level API:
- The command’s
help - Each subcommand’s
help - Each parameter’s
help - Each panel’s
help - Header text, footer text, and epilog text.
# /// script# dependencies = ["rich-click>=1.9"]# ///
help="[magenta not dim]These are [bold]all[/b] the options available.[/]")def cli(): """CLI for my [red]app[/red].
This is a demonstration of [bright_red bold]r[/][bright_yellow bold]i[/][bright_green bold]c[/][bright_cyan bold]h[/] markup in CLI help text. """
if __name__ == "__main__": cli()Output
$ python rich_markup.py --helpUsage: rich_markup.py [OPTIONS] CLI for my app. This is a demonstration of rich markup in CLI help text. ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ These are all the options available. │ │ --foo -f TEXT foo │ │ --bar -b TEXT bar │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ For more information, read the docs
You can also set the styles of help for objects in decorators using help_style=.
The below code renders the same as the above code:
# /// script# dependencies = ["rich-click>=1.9"]# ///
help="These are [bold]all[/b] the options available.", help_style="magenta not dim")def cli(): """CLI for my [red]app[/red].
This is a demonstration of [bright_red bold]r[/][bright_yellow bold]i[/][bright_green bold]c[/][bright_cyan bold]h[/] markup in CLI help text. """
if __name__ == "__main__": cli()Output
$ python markup_with_help_style.py --helpUsage: markup_with_help_style.py [OPTIONS] CLI for my app. This is a demonstration of rich markup in CLI help text. ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ These are all the options available. │ │ --foo -f TEXT foo │ │ --bar -b TEXT bar │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯ For more information, read the docs
Emojis
Section titled “Emojis”Emoji codes are rendered by default in 'rich' and 'markdown' text markup modes.
For other modes, including the default mode, emojis are not rendered.
You can explicitly enable and disable emojis with the config option text_emoji:
# /// script# dependencies = ["rich-click>=1.9"]# ///
def cli(): """Demo of :heart_eyes_cat: emojis!
Note that this text is not rich markup: [b]See?[/b] :joy:
Emojis can be set independently of markup. :point_up: :nerd_face: """
if __name__ == "__main__": cli()Output
$ python emojis.py --helpUsage: emojis.py [OPTIONS] Demo of 😻 emojis! Note that this text is not rich markup: [b]See?[/b] 😂 Emojis can be set independently of markup. ☝ 🤓 ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --foo -f TEXT Boo! 👻 Oops, I mean... foo! 😅 │ │ --bar -b TEXT Bar - 📊 │ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
You can view all available emojis with the following command:
uvx --from rich -- python3 -m rich.emojiNewlines
Section titled “Newlines”Handling newlines on rich-click’s end involves discretion because in a docstring, newlines can represent a softwrap; they are not necessarily genuine new lines.
For 'markdown' mode, we send all newline handling directly to rich.markdown.Markdown().
Likewise, 'rst' mode sends newline handling to rich_rst.RestructuredText().
For other modes, we implement the following rules:
- Double newlines collapse to single newlines by default.
- Single newlines are not preserved, unless…
- The line starts with
'- ','* ','> 'or‘ ’(4 spaces).
The following code snippet demonstrates this:
# /// script# dependencies = ["rich-click>=1.9"]# ///
def cli(): """Newline control
Double newlines are always preserved. But single newlines are usually not.
There are however a few situations where we preserve single newlines.
Indented lines with 4+ spaces are preserved: ```python from foo import bar \n\ bar.action() ```
Unordered lists are preserved:
- like - this
also:
* newlines within unordered lists are collapsed down. * pretty neat!
Last but not least, we preserve:
> block > quotes """
if __name__ == "__main__": cli()Output
$ python newline_control.py --helpUsage: newline_control.py [OPTIONS] Newline control Double newlines are always preserved. But single newlines are usually not. There are however a few situations where we preserve single newlines. Indented lines with 4+ spaces are preserved: ```python from foo import bar bar.action() ``` Unordered lists are preserved: - like - this also: * newlines within unordered lists are collapsed down. * pretty neat! Last but not least, we preserve: > block > quotes ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Note that this differs from how base Click handles newlines.
The following is the same CLI help text but using import click instead of import rich_click as click:
Output - import click instead of import rich_click as click
$ python newline_control_base_click.py --helpUsage: newline_control_base_click.py [OPTIONS] Newline control Double newlines are always preserved. But single newlines are usually not. There are however a few situations where we preserve single newlines. Indented lines with 4+ spaces are preserved: ```python from foo import bar bar.action() ``` Unordered lists are preserved: - like - this also: * newlines within unordered lists are collapsed down. * pretty neat! Last but not least, we preserve: > block > quotes Options: --help Show this message and exit.
Spacing
Section titled “Spacing”By default, rich-click renders double newlines as single newlines. This is a deliberate decision to make help text take up less vertical space.
However, you can override this behavior by setting the config option text_paragraph_linebreaks to "\n\n".
# /// script# dependencies = ["rich-click>=1.9"]# ///
def cli(): """Newline control
Double newlines are always preserved. But single newlines are usually not.
There are however a few situations where we preserve single newlines.
Indented lines with 4+ spaces are preserved: ```python from foo import bar \n\ bar.action() ```
Unordered lists are preserved:
- like - this
also:
* newlines within unordered lists are collapsed down. * pretty neat!
Last but not least, we preserve:
> block > quotes """
if __name__ == "__main__": cli()Output
$ python newline_control_double.py --helpUsage: newline_control_double.py [OPTIONS] Newline control Double newlines are always preserved. But single newlines are usually not. There are however a few situations where we preserve single newlines. Indented lines with 4+ spaces are preserved: ```python from foo import bar bar.action() ``` Unordered lists are preserved: - like - this also: * newlines within unordered lists are collapsed down. * pretty neat! Last but not least, we preserve: > block > quotes ╭─ Options ────────────────────────────────────────────────────────────────────╮ │ --help Show this message and exit. │ ╰──────────────────────────────────────────────────────────────────────────────╯
Info
This default behavior of collapsing newlines is not true when text_markup='markdown' or text_markup='rst'.
For these modes, we do not do any newline manipulation.