Skip to content

Commit

Permalink
Try defining functions as suggested by @grst
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Feb 16, 2022
1 parent fa33e55 commit edb2156
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/rich_click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@
__version__ = "0.4.0.dev0"

from click import *
from .rich_click import rich_format_help
from .rich_click import RichGroup
from .rich_click import RichCommand
from .rich_click import rich_format_error
from .rich_click import Group
from .rich_click import Command

## 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):
from click import group as click_group

return click_group(*args, cls=cls, **kwargs)


def command(*args, cls=RichCommand, **kwargs):
from click import command as click_command

return click_command(*args, cls=cls, **kwargs)

## TODO: This works for simple top-level commands,
# but not commands nested within a group
14 changes: 12 additions & 2 deletions src/rich_click/rich_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,21 @@ def rich_format_error(self):
)


class Group(click.Group):
class RichGroup(click.Group):
def format_help(self, ctx, formatter):
rich_format_help(self, ctx, formatter)


class Command(click.Command):
class RichCommand(click.Command):
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 edb2156

Please sign in to comment.