click-contrib / click-didyoumean

Enable git-like did-you-mean feature in click :sparkles:
MIT License
98 stars 9 forks source link

Using with CatchAllExceptions #5

Closed chookie closed 2 years ago

chookie commented 4 years ago

Thanks for the module, it's nice. How do I use this when I am using @click.group(cls=CatchAllExceptions)? I need to change the to cls=DYMGroup or use DYMCommandCollection but this voids the exception handling.

timofurrer commented 4 years ago

What methods does CatchAllExceptions override?

You could create a Python type dynamically to multi-inherit from DYMGroup and CatchAllExceptions (or do it statically) and use that as your click.group class:

Dynamically:

@click.group(cls=type("MyClickGroup", (DYMGroup, CatchAllExceptions), {}))
def cli():
    ...

Statically:

class MyClickGroup(DYMGroup, CatchAllExceptions):
    ...

@click.group(cli=MyClickGroup)
def cli():
    ...

# with that you can also easily check the Method Resolution Order:
help(MyClickGroup)

Note: DYMGroup overrides click.Group.resolve_command using a mixin: https://github.com/click-contrib/click-didyoumean/blob/7485cc7212a8f28e4f160dd02ebf044d1badda67/click_didyoumean/__init__.py#L44