Rich Argparse

1.7.2 · active · verified Mon Apr 06

rich-argparse provides beautiful, Rich-powered help formatters for Python's argparse and optparse modules. It enhances the standard help output with syntax highlighting, improved layout, and the ability to include Rich renderables like Markdown and tables in descriptions and epilogs. The current version is 1.7.2, and it maintains an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply `RichHelpFormatter` to an `argparse.ArgumentParser` instance. It showcases rich markup directly within the description and help messages, which `RichHelpFormatter` will render beautifully in the terminal when `parser.print_help()` is called or `--help` is invoked.

import argparse
from rich_argparse import RichHelpFormatter

parser = argparse.ArgumentParser(
    description='A command-line tool with \n[bold blue]rich[/bold blue] help formatting.',
    formatter_class=RichHelpFormatter
)

parser.add_argument('--name', '-n', help='Your name.')
parser.add_argument('--age', '-a', type=int, help='Your age.')
parser.add_argument(
    '--verbose', '-v', action='store_true', 
    help='[dim]Enable verbose output.[/dim]'
)

# To display the rich help, you would typically run your script with --help
# or allow argparse to display it on invalid input.
# For demonstration, we can try parsing arguments that trigger help:
# try:
#     parser.parse_args(['--help'])
# except SystemExit: # argparse raises SystemExit on --help
#     pass

# To simply see the formatted output in a script context:
# This will print the help to the console
parser.print_help()

view raw JSON →