Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autcompletion to huggingface-cli (fix #1197) #1207

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:
rev: 22.3.0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def get_version() -> str:

extras = {}

extras["cli"] = [
extras["completion"] = [
"shtab",
]

extras["cli"] = extras["completion"] + [
"InquirerPy==0.3.4",
# Note: installs `prompt-toolkit` in the background
]
Expand Down
44 changes: 44 additions & 0 deletions src/huggingface_hub/commands/_shtab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Fake shtab."""
from argparse import Action, ArgumentParser


_WARNING_MESSAGE = (
"You first need to install `shtab` to use autocompletion. Please run `pip install"
" 'huggingface_hub[completion]'`"
)


class PrintCompletionAction(Action):
"""Fake `PrintCompletionAction` when shtab is not installed.

See <https://github.com/iterative/shtab/blob/95b0e3092cd4dcf1ac2871d44cebda01a89992df/shtab/__init__.py#L786-L789>
"""

def __call__(self, parser, namespace, values, option_string=None):
"""Warn when shtab is not installed.

:param parser:
:param namespace:
:param values:
:param option_string:
"""
print(_WARNING_MESSAGE)
parser.exit(0)


def add_argument_to(parser: ArgumentParser, *args, **kwargs):
"""Add completion argument to parser.

:param parser:
:type parser: ArgumentParser
:param args:
:param kwargs:
"""
Action.complete = None # type: ignore
parser.add_argument(
"--print-completion",
choices=["bash", "zsh", "tcsh"],
action=PrintCompletionAction,
help=_WARNING_MESSAGE,
)
return parser
7 changes: 7 additions & 0 deletions src/huggingface_hub/commands/huggingface_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
from huggingface_hub.commands.user import UserCommands


try:
import shtab
except ImportError:
from . import _shtab as shtab # type: ignore


def main():
parser = ArgumentParser(
"huggingface-cli", usage="huggingface-cli <command> [<args>]"
)
shtab.add_argument_to(parser)
commands_parser = parser.add_subparsers(help="huggingface-cli command helpers")

# Register commands
Expand Down