[CLI] Make --format / --json / -q global#4162
Conversation
Move handling of --format, --json and -q/--quiet out of every command's signature into a single pre-processor in HFCliTyperGroup that consumes them from any leaf command's args and applies them to the `out` singleton. Document them in a dedicated "Formatting options" section shown on every --help page. Legacy commands that own their own --format (hf jobs ps, scheduled ps) keep working: their shorthand rewrites are preserved. Pass-through commands (hf extensions exec) are left untouched. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
hanouticelina
left a comment
There was a problem hiding this comment.
very nice improvement!
| def format_options(self, ctx: click.Context, formatter: click.HelpFormatter) -> None: | ||
| # MultiCommand.format_options writes regular options + the commands list. We | ||
| # insert the "Formatting options" section in between so it sits with the | ||
| # other options instead of below the subcommand list. | ||
| click.Command.format_options(self, ctx, formatter) | ||
| _format_formatting_options_section(formatter) | ||
| self.format_commands(ctx, formatter) |
There was a problem hiding this comment.
no strong opinion but I think it would be better to have:
hf --help -> no "Formatting options" section
hf models --help -> no section either
hf models ls --help -> shows the Fromatting section (where the flags actually work)
| def format_options(self, ctx: click.Context, formatter: click.HelpFormatter) -> None: | |
| # MultiCommand.format_options writes regular options + the commands list. We | |
| # insert the "Formatting options" section in between so it sits with the | |
| # other options instead of below the subcommand list. | |
| click.Command.format_options(self, ctx, formatter) | |
| _format_formatting_options_section(formatter) | |
| self.format_commands(ctx, formatter) |
Co-authored-by: célina <[email protected]>
Co-authored-by: célina <[email protected]>
Remove the section from group help pages (hf --help, hf models --help) since the flags only apply to leaf commands. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Resolve conflicts: take new file-listing features from main (human_readable, tree, recursive, revision params + list_repo_files_cmd) but drop local format parameters in favor of global --format handling. Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e44a4c6. Configure here.
| mode = OutputFormatWithAuto.agent if is_agent() else OutputFormatWithAuto.human | ||
| self.mode = mode | ||
| if mode != OutputFormatWithAuto.human: | ||
| disable_progress_bars() |
There was a problem hiding this comment.
Global flags always call set_mode(auto) disabling progress bars early
Medium Severity
Moving disable_progress_bars() into Output.set_mode() causes progress bars to be disabled at module import time for agent environments, since out = Output() is a module-level singleton that calls set_mode() in __init__. More critically, _consume_format_flags_for_leaf unconditionally calls out.set_mode(chosen_mode) even when no formatting flags are provided (chosen_mode defaults to auto). When auto resolves to agent, this disables progress bars for every leaf command invocation in agent mode — even commands where the old code would have left progress bars enabled because they never declared FormatWithAutoOpt. Previously, disable_progress_bars() was only called when a command explicitly declared and received the format parameter via the _set_output_mode callback.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit e44a4c6. Configure here.
There was a problem hiding this comment.
that's fine now that (almost?) all commands have been migrated
- Rename `secrets set` / `variables set` to `add` (mirrors add_space_secret/ add_space_variable, signals upsert vs volumes set's replace). - Add confirm + `-y/--yes` to `variables delete` for parity with `secrets delete`. Kept the warning text intentionally asymmetric: secrets says "value cannot be recovered" (true: write-only API), variables uses the shorter volumes-style phrasing (the value is visible via `ls` before deletion). - Remove now-stale per-command `--format` declarations. #4162 made --format global, so these were dead code that was breaking the import after the rebase. Per @Wauplin review at #4170 (comment 4351208819). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
This PR has been shipped as part of the v1.13.0 release. |
* [CLI] Add hf spaces secrets and variables subgroups
- `hf spaces secrets {set,delete}` wraps add_space_secret / delete_space_secret.
- `hf spaces variables {set,delete,ls}` wraps add/delete_space_variable and get_space_variables.
- Both `set` commands accept multiple `-s`/`-e` flags and `--secrets-file`/`--env-file`
(dotenv) via the existing `parse_env_map` helper — no new parsing code.
- No `secrets ls`: Hub exposes secrets as write-only (no GET endpoint).
- Confirm + `--yes` on `secrets delete` only (lost-value risk); variables delete is
replayable so it skips the prompt, per the #4155 rule.
- `set` is upsert (one API call per key — no bulk endpoint), not replace-collection.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
* [CLI] Apply review feedback: rename set→add, confirm on variables delete
- Rename `secrets set` / `variables set` to `add` (mirrors add_space_secret/
add_space_variable, signals upsert vs volumes set's replace).
- Add confirm + `-y/--yes` to `variables delete` for parity with
`secrets delete`. Kept the warning text intentionally asymmetric: secrets
says "value cannot be recovered" (true: write-only API), variables uses
the shorter volumes-style phrasing (the value is visible via `ls` before
deletion).
- Remove now-stale per-command `--format` declarations. #4162 made --format
global, so these were dead code that was breaking the import after the
rebase.
Per @Wauplin review at #4170 (comment 4351208819).
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
* Update docs/source/en/guides/cli.md
Co-authored-by: Lucain <[email protected]>
* Update src/huggingface_hub/cli/spaces.py
Co-authored-by: Lucain <[email protected]>
* [CLI] Regenerate CLI reference after suggestion commits
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Co-authored-by: Lucain <[email protected]>


Context: let's handle
--formatgloballyBenefits are:
--help(one consistent section per command), instead of being mixed into the regular options or only mentioned in some commands.format: FormatWithAutoOpt = OutputFormatWithAuto.auto(and the matching imports) when adding a new command. The flags work automatically.Summary
--format <value>,--json, and-q/--quietglobally inHFCliTyperGroupinstead of declaring them on every command. The flags can appear anywhere in a leaf command's args; they're stripped and applied to theoutsingleton before click parses the rest.Formatting optionssection in every--helppage (root, groups, and leaves).format: FormatWithAutoOpt = OutputFormatWithAuto.autofrom every command (~85 occurrences across 17 files).--format(hf jobs ps,hf jobs scheduled ps) keep working — the previous--json/--quietshorthand-rewrite behaviour is preserved for them. Pass-through commands (hf extensions exec) remain untouched.Output formattingsection to the CLI guide.Examples
Note
Medium Risk
Touches CLI argument parsing and help rendering globally, so regressions could affect many commands’ option handling; changes include explicit compatibility paths for legacy/pass-through commands to reduce risk.
Overview
Makes
--format,--json, and-q/--quietglobal formatting flags by consuming them during command resolution and applying the chosen mode to the sharedoutsingleton, so leaf commands no longer need to declareFormatWithAutoOpt(while pass-through commands remain untouched).Updates help generation to add a consistent “Formatting options” section to command
--helpoutput, preserves backward compatibility for legacy commands that own their own formatting options by rewriting shorthands, and switches a few commands (e.g.hf buckets sync) to read quiet-mode fromoutinstead of a local flag. Documentation is updated to explain the new behavior, and the generated CLI reference is refreshed accordingly.Reviewed by Cursor Bugbot for commit e44a4c6. Bugbot is set up for automated code reviews on this repo. Configure here.