Skip to content

Commit

Permalink
reinstate CommandCollection support
Browse files Browse the repository at this point in the history
  • Loading branch information
dwreeves committed Oct 7, 2023
1 parent 5b09dc2 commit 3cb6bd0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/rich_click/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# We need to manually import these instead of `from click import *` to force mypy to recognize a few type annotation overrides for the rich_click decorators.
from click.core import Argument as Argument
from click.core import Command as Command
from click.core import CommandCollection as CommandCollection
from click.core import Context as Context
from click.core import Group as Group
from click.core import Option as Option
Expand Down Expand Up @@ -76,6 +77,7 @@
from rich_click.decorators import pass_context as pass_context
from rich_click.decorators import rich_config as rich_config
from rich_click.rich_command import RichCommand as RichCommand
from rich_click.rich_command import RichCommandCollection as RichCommandCollection
from rich_click.rich_command import RichGroup as RichGroup
from rich_click.rich_context import RichContext as RichContext
from rich_click.rich_help_configuration import RichHelpConfiguration as RichHelpConfiguration
Expand Down
3 changes: 2 additions & 1 deletion src/rich_click/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from rich_click import command as rich_command
from rich_click import group as rich_group
from rich_click import RichCommand, RichGroup, RichMultiCommand
from rich_click import RichCommand, RichCommandCollection, RichGroup, RichMultiCommand
from rich_click.rich_click import (
ALIGN_ERRORS_PANEL,
ERRORS_PANEL_TITLE,
Expand Down Expand Up @@ -68,6 +68,7 @@ def patch() -> None:
click.command = rich_command
click.Group = RichGroup # type: ignore[misc]
click.Command = RichCommand # type: ignore[misc]
click.CommandCollection = RichCommandCollection # type: ignore[misc]
if "MultiCommand" in dir(click):
click.MultiCommand = RichMultiCommand # type: ignore[assignment,misc]

Expand Down
56 changes: 38 additions & 18 deletions src/rich_click/rich_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from click.utils import make_str, PacifyFlushWrapper
from rich.console import Console

from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X
from rich_click._compat_click import CLICK_IS_BEFORE_VERSION_8X, CLICK_IS_BEFORE_VERSION_9X
from rich_click.rich_click import rich_abort_error, rich_format_error, rich_format_help
from rich_click.rich_context import RichContext
from rich_click.rich_help_configuration import RichHelpConfiguration
Expand Down Expand Up @@ -162,23 +162,6 @@ def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> Non
rich_format_help(self, ctx, formatter)


with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning, module="click")

class RichMultiCommand(RichCommand, click.MultiCommand):
"""Richly formatted click MultiCommand.
Inherits click.MultiCommand and overrides help and error methods
to print richly formatted output.
"""

@wraps(click.MultiCommand.__init__)
def __init__(self, *args: Any, **kwargs: Any) -> None: # type: ignore[no-untyped-def]
"""Initialize RichGroup class."""
click.MultiCommand.__init__(self, *args, **kwargs)
self._register_rich_context_settings_from_callback()


class RichGroup(RichCommand, click.Group):
"""Richly formatted click Group.
Expand Down Expand Up @@ -212,3 +195,40 @@ def command(
# (The command_class ClassVar was not added until 8.0.)
kwargs.setdefault("cls", self.command_class)
return super().command(*args, **kwargs) # type: ignore[no-any-return]


if CLICK_IS_BEFORE_VERSION_9X:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning, module="click")

class RichMultiCommand(RichCommand, click.MultiCommand):
"""Richly formatted click MultiCommand.
Inherits click.MultiCommand and overrides help and error methods
to print richly formatted output.
"""

@wraps(click.MultiCommand.__init__)
def __init__(self, *args: Any, **kwargs: Any) -> None: # type: ignore[no-untyped-def]
"""Initialize RichGroup class."""
click.MultiCommand.__init__(self, *args, **kwargs)
self._register_rich_context_settings_from_callback()

else:

class RichMultiCommand(RichGroup): # type: ignore[no-redef]
"""This class is deprecated."""


class RichCommandCollection(RichGroup, click.CommandCollection):
"""Richly formatted click CommandCollection.
Inherits click.CommandCollection and overrides help and error methods
to print richly formatted output.
"""

@wraps(click.CommandCollection.__init__)
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Initialize RichCommandCollection class."""
click.CommandCollection.__init__(self, *args, **kwargs)
self._register_rich_context_settings_from_callback()

0 comments on commit 3cb6bd0

Please sign in to comment.