Skip to content

Commit

Permalink
Handle error messages without monkey patching
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Feb 17, 2022
1 parent 1955dea commit 4384217
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions examples/03_simple_importas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def original(all):


if __name__ == "__main__":
# cli() # Use rich-click, should be fancy output
original() # Use vanilla click, should be simple output
cli() # Use rich-click, should be fancy output
# original() # Use vanilla click, should be simple output
6 changes: 0 additions & 6 deletions src/rich_click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
from click import *
from .rich_click import RichGroup
from .rich_click import RichCommand
from .rich_click import rich_format_error

## TODO: Replace with inheritance / custom function model as below
# Monkey patch click error formatting function
ClickException.show = rich_format_error
UsageError.show = rich_format_error


def group(*args, cls=RichGroup, **kwargs):
Expand Down
44 changes: 31 additions & 13 deletions src/rich_click/rich_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rich.text import Text
from rich.theme import Theme
import re
import sys

# Default styles
STYLE_OPTION = "bold cyan"
Expand Down Expand Up @@ -337,9 +338,6 @@ def rich_format_error(self):
"""
Custom function to overwrite default click error printing.
"""
# TODO: The click function has more complex code for UsageErrors:
# https:/pallets/click/blob/6411f425fae545f42795665af4162006b36c5e4a/src/click/exceptions.py#L62-L82
# Should bring this over too.
console = Console(
theme=Theme(
{
Expand All @@ -351,6 +349,16 @@ def rich_format_error(self):
),
highlighter=highlighter,
)
if self.ctx is not None:
console.print(self.ctx.get_usage())
if self.ctx is not None and self.ctx.command.get_help_option(self.ctx) is not None:
console.print(
"Try [blue]'{command} {option}'[/] for help.".format(
command=self.ctx.command_path, option=self.ctx.help_option_names[0]
),
style="dim",
)

console.print(
Panel(
highlighter(self.format_message()),
Expand All @@ -363,22 +371,32 @@ def rich_format_error(self):


class RichCommand(click.Command):
standalone_mode = False

def main(self, *args, standalone_mode=True, **kwargs):
try:
return super().main(*args, standalone_mode=False, **kwargs)
except click.ClickException as e:
if not standalone_mode:
raise
rich_format_error(e)
sys.exit(e.exit_code)

def format_help(self, ctx, formatter):
rich_format_help(self, ctx, formatter)


class RichGroup(click.Group):
command_class = RichCommand

def main(self, *args, standalone_mode=True, **kwargs):
try:
return super().main(*args, standalone_mode=False, **kwargs)
except click.ClickException as e:
if not standalone_mode:
raise
rich_format_error(e)
sys.exit(e.exit_code)

def format_help(self, ctx, formatter):
rich_format_help(self, ctx, formatter)


class RichClickException(click.ClickException):
def show(self, file):
rich_format_error(self, file)


class RichUsageError(click.UsageError):
def show(self, file):
rich_format_error(self, file)

0 comments on commit 4384217

Please sign in to comment.