Spaces:
Sleeping
Sleeping
Upload backend/venv/lib/python3.10/site-packages/click_didyoumean/__init__.py with huggingface_hub
Browse files
backend/venv/lib/python3.10/site-packages/click_didyoumean/__init__.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Extension for ``click`` to provide a group
|
| 3 |
+
with a git-like *did-you-mean* feature.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import difflib
|
| 7 |
+
import typing
|
| 8 |
+
|
| 9 |
+
import click
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class DYMMixin:
|
| 13 |
+
"""
|
| 14 |
+
Mixin class for click MultiCommand inherited classes
|
| 15 |
+
to provide git-like *did-you-mean* functionality when
|
| 16 |
+
a certain command is not registered.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
|
| 20 |
+
self.max_suggestions = kwargs.pop("max_suggestions", 3)
|
| 21 |
+
self.cutoff = kwargs.pop("cutoff", 0.5)
|
| 22 |
+
super().__init__(*args, **kwargs) # type: ignore
|
| 23 |
+
|
| 24 |
+
def resolve_command(
|
| 25 |
+
self, ctx: click.Context, args: typing.List[str]
|
| 26 |
+
) -> typing.Tuple[
|
| 27 |
+
typing.Optional[str], typing.Optional[click.Command], typing.List[str]
|
| 28 |
+
]:
|
| 29 |
+
"""
|
| 30 |
+
Overrides clicks ``resolve_command`` method
|
| 31 |
+
and appends *Did you mean ...* suggestions
|
| 32 |
+
to the raised exception message.
|
| 33 |
+
"""
|
| 34 |
+
try:
|
| 35 |
+
return super(DYMMixin, self).resolve_command(ctx, args) # type: ignore
|
| 36 |
+
except click.exceptions.UsageError as error:
|
| 37 |
+
error_msg = str(error)
|
| 38 |
+
original_cmd_name = click.utils.make_str(args[0])
|
| 39 |
+
matches = difflib.get_close_matches(
|
| 40 |
+
original_cmd_name,
|
| 41 |
+
self.list_commands(ctx), # type: ignore
|
| 42 |
+
self.max_suggestions,
|
| 43 |
+
self.cutoff,
|
| 44 |
+
)
|
| 45 |
+
if matches:
|
| 46 |
+
fmt_matches = "\n ".join(matches)
|
| 47 |
+
error_msg += "\n\n"
|
| 48 |
+
error_msg += f"Did you mean one of these?\n {fmt_matches}"
|
| 49 |
+
|
| 50 |
+
raise click.exceptions.UsageError(error_msg, error.ctx)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class DYMGroup(DYMMixin, click.Group):
|
| 54 |
+
"""
|
| 55 |
+
click Group to provide git-like
|
| 56 |
+
*did-you-mean* functionality when a certain
|
| 57 |
+
command is not found in the group.
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class DYMCommandCollection(DYMMixin, click.CommandCollection):
|
| 62 |
+
"""
|
| 63 |
+
click CommandCollection to provide git-like
|
| 64 |
+
*did-you-mean* functionality when a certain
|
| 65 |
+
command is not found in the group.
|
| 66 |
+
"""
|