Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d46c5fa
migrate CLI to typer
hanouticelina Sep 15, 2025
93136ef
(#3364) disable rich in all cases
hanouticelina Sep 15, 2025
a31db5a
update tests
hanouticelina Sep 16, 2025
8ee6892
make typer-slim a required dep
hanouticelina Sep 16, 2025
4437b4c
use Annotated
hanouticelina Sep 16, 2025
cb47d09
fix linting issues
hanouticelina Sep 16, 2025
d8f08d5
fix tests
hanouticelina Sep 16, 2025
e5fff14
refactoring
hanouticelina Sep 16, 2025
7543f3c
update docs
hanouticelina Sep 16, 2025
f41bbcf
use built in types
hanouticelina Sep 16, 2025
8ff6d0c
fix mypy
hanouticelina Sep 16, 2025
53a4343
call whoami directly
hanouticelina Sep 16, 2025
f251a8a
lint
hanouticelina Sep 16, 2025
2b422ee
Apply suggestions from code review
hanouticelina Sep 17, 2025
ab9ac9a
import Annotated from typing
hanouticelina Sep 17, 2025
547a436
Use Enums
hanouticelina Sep 17, 2025
9a65442
set verbosity globally
hanouticelina Sep 17, 2025
7a7190d
refactor scan cache and update version docstring
hanouticelina Sep 17, 2025
b56c656
centralize where Typer is defined
hanouticelina Sep 17, 2025
45c9fef
no need for ...
hanouticelina Sep 17, 2025
a2ca057
rename enum
hanouticelina Sep 17, 2025
d7823a3
no need for extra param name
hanouticelina Sep 17, 2025
4233fdc
Merge branch 'typer-migration' of github.com:huggingface/huggingface_…
hanouticelina Sep 17, 2025
5d18065
docstring
hanouticelina Sep 17, 2025
2120e5d
revert
hanouticelina Sep 17, 2025
148299e
centralize arguments and options definition
hanouticelina Sep 17, 2025
f2eb79b
add library version when initializing HfApi
hanouticelina Sep 17, 2025
3e5bc64
add auto-completion
hanouticelina Sep 17, 2025
9efaecf
Merge branch 'cli-improvements' of github.com:huggingface/huggingface…
hanouticelina Sep 17, 2025
89f592e
sort commands alphabetically
hanouticelina Sep 17, 2025
73aa986
suggestions
hanouticelina Sep 17, 2025
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
39 changes: 21 additions & 18 deletions docs/source/en/guides/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ Once installed, you can check that the CLI is correctly setup:

```
>>> hf --help
usage: hf <command> [<args>]

positional arguments:
{auth,cache,download,repo,repo-files,upload,upload-large-folder,env,version,lfs-enable-largefiles,lfs-multipart-upload}
hf command helpers
auth Manage authentication (login, logout, etc.).
cache Manage local cache directory.
download Download files from the Hub
repo Manage repos on the Hub.
repo-files Manage files in a repo on the Hub.
upload Upload a file or a folder to the Hub. Recommended for single-commit uploads.
upload-large-folder
Upload a large folder to the Hub. Recommended for resumable uploads.
env Print information about the environment.
version Print information about the hf version.

options:
-h, --help show this help message and exit
Usage: hf [OPTIONS] COMMAND [ARGS]...

Hugging Face Hub CLI

Options:
--install-completion Install completion for the current shell.
--show-completion Show completion for the current shell, to copy it or
customize the installation.
--help Show this message and exit.

Commands:
download Download files from the Hub.
upload Upload a file or a folder to the Hub.
upload-large-folder Upload a large folder to the Hub.
env Print information about the environment.
version Print information about the hf version.
auth Manage authentication (login, logout, etc.).
cache Manage local cache directory.
repo Manage repos on the Hub.
repo-files Manage files in a repo on the Hub.
jobs Run and manage Jobs on the Hub.
```

If the CLI is correctly installed, you should see a list of all the options available in the CLI. If you get an error message such as `command not found: hf`, please refer to the [Installation](../installation) guide.
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ def get_version() -> str:
"pyyaml>=5.1",
"httpx>=0.23.0, <1",
"tqdm>=4.42.1",
"typer-slim",
"typing-extensions>=3.7.4.3", # to be able to import TypeAlias
]

extras = {}

extras["cli"] = [
"InquirerPy==0.3.4", # Note: installs `prompt-toolkit` in the background
"shellingham",
]

extras["inference"] = [
Expand Down
14 changes: 0 additions & 14 deletions src/huggingface_hub/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABC, abstractmethod
from argparse import _SubParsersAction


class BaseHuggingfaceCLICommand(ABC):
@staticmethod
@abstractmethod
def register_subcommand(parser: _SubParsersAction):
raise NotImplementedError()

@abstractmethod
def run(self):
raise NotImplementedError()
74 changes: 72 additions & 2 deletions src/huggingface_hub/cli/_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contains a utility for good-looking prints."""
"""Contains CLI utilities (styling, helpers)."""

import os
from typing import Union
from enum import Enum
from typing import Annotated, Optional, Union

import click
import typer


class ANSI:
Expand Down Expand Up @@ -67,3 +71,69 @@ def tabulate(rows: list[list[Union[str, int]]], headers: list[str]) -> str:
for row in rows:
lines.append(row_format.format(*row))
return "\n".join(lines)


#### TYPER UTILS


class AlphabeticalMixedGroup(typer.core.TyperGroup):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

"""
Typer Group that lists commands and sub-apps mixed and alphabetically.
"""

def list_commands(self, ctx: click.Context) -> list[str]: # type: ignore[name-defined]
# click.Group stores both commands and sub-groups in `self.commands`
return sorted(self.commands.keys())


def typer_factory(help: str) -> typer.Typer:
return typer.Typer(
help=help,
add_completion=True,
rich_markup_mode=None,
no_args_is_help=True,
cls=AlphabeticalMixedGroup,
)


class RepoType(str, Enum):
model = "model"
dataset = "dataset"
space = "space"


RepoIdArg = Annotated[
str,
typer.Argument(
help="The ID of the repo (e.g. `username/repo-name`).",
),
]


RepoTypeOpt = Annotated[
RepoType,
typer.Option(
help="The type of repository (model, dataset, or space).",
),
]

TokenOpt = Annotated[
Optional[str],
typer.Option(
help="A User Access Token generated from https://huggingface.co/settings/tokens.",
),
]

PrivateOpt = Annotated[
bool,
typer.Option(
help="Whether to create a private repo if repo doesn't exist on the Hub. Ignored if the repo already exists.",
),
]

RevisionOpt = Annotated[
Optional[str],
typer.Option(
help="Git revision id which can be a branch name, a tag, or a commit hash.",
),
]
Loading
Loading