Skip to content

Add support for colorizing code examples in argparse description/epilog #142389

@savannahostrowski

Description

@savannahostrowski

Today, argparse does not support any native way to colorize code snippets in help descriptions. Example commands blend in with surrounding text, making help output harder to scan:
Image

Allowing users to specify that they want to colorize an example in help text is a common UX pattern in modern CLI frameworks. Both Typer and rich-click support markup in help text via Rich's console markup (e.g., [green]text[/green]). Now that argparse supports colorization, it would be nice to add similar functionality. I think this is perhaps the last remaining gap in argparse's colorization support. I'm proposing a limited semantic markup approach:

epilog="""Examples:
  [cmd]python -m myapp --verbose run[/cmd]
"""

After:

Image

This support could potentially be expanded out later to support more sophisticated syntax highlighting (e.g. [python]...[/python]), since we already have a lot of this tokenization logic in place in _colorize.py. However, I'd like to start here before boiling the ocean.

Linked PRs

Metadata

Metadata

Labels

3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

Status

Features

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions