| 1 |
import sys |
| 2 |
|
| 3 |
import click |
| 4 |
|
| 5 |
from rich_click.rich_click import rich_abort_error, rich_format_error, rich_format_help |
| 6 |
|
| 7 |
|
| 8 |
class RichCommand(click.Command): |
| 9 |
"""Richly formatted click Command. |
| 10 |
|
| 11 |
Inherits click.Command and overrides help and error methods |
| 12 |
to print richly formatted output. |
| 13 |
""" |
| 14 |
|
| 15 |
standalone_mode = False |
| 16 |
|
| 17 |
def main(self, *args, standalone_mode: bool = True, **kwargs): |
| 18 |
try: |
| 19 |
rv = super().main(*args, standalone_mode=False, **kwargs) |
| 20 |
if not standalone_mode: |
| 21 |
return rv |
| 22 |
except click.ClickException as e: |
| 23 |
if not standalone_mode: |
| 24 |
raise |
| 25 |
rich_format_error(e) |
| 26 |
sys.exit(e.exit_code) |
| 27 |
except click.exceptions.Abort: |
| 28 |
if not standalone_mode: |
| 29 |
raise |
| 30 |
rich_abort_error() |
| 31 |
sys.exit(1) |
| 32 |
|
| 33 |
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter): |
| 34 |
rich_format_help(self, ctx, formatter) |