Replies: 4 comments 3 replies
-
|
I would also be interested in using rich to render argparse output, even if it was only the |
Beta Was this translation helpful? Give feedback.
-
|
Did you try to override |
Beta Was this translation helpful? Give feedback.
-
|
Yes, I would love if rich could work with click to give colored help messages like poetry |
Beta Was this translation helpful? Give feedback.
-
|
Rich provides import argparse
from rich.console import Console
from rich.table import Table
from rich_argparse import RichHelpFormatter
# Option 1: Use rich_argparse library (recommended)
# pip install rich-argparse
parser = argparse.ArgumentParser(formatter_class=RichHelpFormatter)
parser.add_argument("--verbose", "-v", action="store_true", help="Enable verbose output")
parser.parse_args()
# Option 2: Manual rich formatting in argparse help
class CustomRichFormatter(argparse.HelpFormatter):
def format_help(self):
console = Console()
table = Table(title="Usage", show_header=False)
table.add_column("", style="cyan")
table.add_column("", style="white")
for action in self._actions:
if action.option_strings:
table.add_row(", ".join(action.option_strings), action.help or "")
with console.capture() as capture:
console.print(table)
return capture.get()
parser = argparse.ArgumentParser(formatter_class=CustomRichFormatter)For click instead of argparse (recommended), use Rich's click integration directly: # pip install rich-click
import rich_click
rich_click.COMMAND_GROUPS = {
"mycli": [
{"name": "Main Commands", "commands": ["run", "serve"]},
]
}
# Then use regular click - rich-click handles the formatting
import click
@click.command()
@click.option("--verbose", "-v", is_flag=True)
def run(verbose):
pass
|
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
See the code below:
I am creating an argument parser with argparse. I want rich text for help, description and epilogs.
how can i use rich along with argparse ?
Beta Was this translation helpful? Give feedback.
All reactions