-
Notifications
You must be signed in to change notification settings - Fork 798
Refactor CLI implementation using Typer #3365
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
Open
hanouticelina
wants to merge
31
commits into
cli-improvements
Choose a base branch
from
typer-migration
base: cli-improvements
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 93136ef
(#3364) disable rich in all cases
hanouticelina a31db5a
update tests
hanouticelina 8ee6892
make typer-slim a required dep
hanouticelina 4437b4c
use Annotated
hanouticelina cb47d09
fix linting issues
hanouticelina d8f08d5
fix tests
hanouticelina e5fff14
refactoring
hanouticelina 7543f3c
update docs
hanouticelina f41bbcf
use built in types
hanouticelina 8ff6d0c
fix mypy
hanouticelina 53a4343
call whoami directly
hanouticelina f251a8a
lint
hanouticelina 2b422ee
Apply suggestions from code review
hanouticelina ab9ac9a
import Annotated from typing
hanouticelina 547a436
Use Enums
hanouticelina 9a65442
set verbosity globally
hanouticelina 7a7190d
refactor scan cache and update version docstring
hanouticelina b56c656
centralize where Typer is defined
hanouticelina 45c9fef
no need for ...
hanouticelina a2ca057
rename enum
hanouticelina d7823a3
no need for extra param name
hanouticelina 4233fdc
Merge branch 'typer-migration' of github.com:huggingface/huggingface_…
hanouticelina 5d18065
docstring
hanouticelina 2120e5d
revert
hanouticelina 148299e
centralize arguments and options definition
hanouticelina f2eb79b
add library version when initializing HfApi
hanouticelina 3e5bc64
add auto-completion
hanouticelina 9efaecf
Merge branch 'cli-improvements' of github.com:huggingface/huggingface…
hanouticelina 89f592e
sort commands alphabetically
hanouticelina 73aa986
suggestions
hanouticelina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.", | ||
), | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.