Skip to content

Text Markup & Formatting

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 the rst extra).
  • 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.

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"}
Rich markup example
$ python examples/04_rich_markup.py --help
Output

 Usage: 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.py for an example.

If you prefer, you can use Markdown text.

help_config = {"text_markup": "markdown"}
Markdown example
$ python examples/05_markdown.py --help
Output

 Usage: 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.py for an example.

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:

Terminal window
pip install 'rich-click[rst]'
help_config = {"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"}.

reStructuredText example
$ python examples/14_rst.py --help
Output

 Usage: 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.py for an example.

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"]
# ///
import rich_click as click
@click.command(epilog="[blue]For more information, read the docs[/blue]")
@click.option("--foo", "-f", help="[green]foo[/]")
@click.option("--bar", "-b", help="[default on green]bar[/]")
@click.option_panel("Options",
help="[magenta not dim]These are [bold]all[/b] the options available.[/]")
@click.rich_config({"text_markup": "rich"})
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
Terminal window
$ python rich_markup.py --help
Output

 Usage: 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"]
# ///
import rich_click as click
@click.command(epilog="[blue]For more information, read the docs[/blue]")
@click.option("--foo", "-f", help="foo", help_style="green")
@click.option("--bar", "-b", help="bar", help_style="default on green")
@click.option_panel("Options",
help="These are [bold]all[/b] the options available.",
help_style="magenta not dim")
@click.rich_config({"text_markup": "rich"})
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
Terminal window
$ python markup_with_help_style.py --help
Output

 Usage: 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

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"]
# ///
import rich_click as click
@click.option("--foo", "-f", help="Boo! :ghost: Oops, I mean... foo! :sweat_smile:")
@click.option("--bar", "-b", help="Bar - :bar_chart:")
@click.rich_config({"text_emojis": True})
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
Terminal window
$ python emojis.py --help
Output

 Usage: 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:

Terminal window
uvx --from rich -- python3 -m rich.emoji

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"]
# ///
import rich_click as click
@click.command("newline-control")
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
Terminal window
$ python newline_control.py --help
Output

 Usage: 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
Terminal window
$ python newline_control_base_click.py --help
Output
Usage: 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.

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"]
# ///
import rich_click as click
@click.command("newline-control")
@click.rich_config({"text_paragraph_linebreaks": "\n\n"})
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
Terminal window
$ python newline_control_double.py --help
Output

 Usage: 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.