cli/parser: don't restrict global options to subcommands - #22408
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a CLI parsing regression where global options (e.g., -v/--verbose) become incorrectly restricted to only the subcommands that re-declare them, causing valid invocations like brew bundle cleanup -v to error despite help output listing the option.
Changes:
- Update
CLI::Parser#record_option_metadatato avoid recording subcommand constraints for globally-registered options when they are re-declared inside a subcommand block. - Add a parser-level regression test covering global option re-declaration inside a subcommand.
- Add a
brew bundleregression test verifying--verbose/-vis accepted on subcommands that do not re-declare it.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
Library/Homebrew/cli/parser.rb |
Prevents global options re-declared in a subcommand from being constrained to that subcommand. |
Library/Homebrew/test/cli/parser_spec.rb |
Adds coverage to ensure global options remain accepted across all subcommands after re-declaration. |
Library/Homebrew/test/cmd/bundle_spec.rb |
Verifies brew bundle subcommands like cleanup/dump/list accept --verbose/-v. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
brew lgtm(style, typechecking and tests) with your changes locally?This PR was prepared with assistance from Claude Code (model: claude-opus-4-7). I reviewed all generated code and tests manually, reproduced the bug locally, and ran brew lgtm successfully before pushing. This is my only AI-assisted PR currently open.
Problem
Since 5.1.12,
brew bundle cleanup -vis rejected:even though
brew bundle cleanup -hstill lists-v,--verboseamong its options — help output and runtime disagree.The same rejection happens on every bundle subcommand that doesn't re-declare
--verboseitself:cleanup,dump,list,add,remove,edit,exec,sh,env. Onlyinstallandcheckstill accept-v, because they declare it explicitly.Root cause
Library/Homebrew/cli/parser.rbauto-registers-d/-q/-v/-has global options for every command. #22280 moved switch-v,--verbosedeclarations into theinstallandchecksubcommand blocks so they could carry subcommand-specific descriptions.record_option_metadatatreats any option declared inside a subcommand block as constrained to that subcommand. That's correct for genuine subcommand-only options (e.g.--jobsoninstall), but re-declaring a global option inside a subcommand block silently constrains the global option too. So--verbosegot pinned to["install", "check"], while help generation kept showing it as a root option everywhere — producing the mismatch above.Same family of regression as #22325 (
--zapconstraint scope), but at the global-option layer rather than per-option.Fix
Skip subcommand-constraint recording for global options in record_option_metadata:
global_option?checks againstself.class.global_options. Subcommand-specific options keep their existing constraint behaviour —brew bundle exec --jobs=1 truestill correctly errors. Theinstall/checkre-declarations of--verbosekeep providing their custom descriptions; only the unintended constraint goes away.Alternative considered
Adding switch
-v,--verboseto each of the ~10 affected bundle subcommands would also fix the immediate symptom, but:--debug/--quietagainst the same mistake;A single guard in the parser keeps the fix minimal and addresses the root cause.
Scope
--verboseis the only currently-broken global option;--debugand--quietaren't re-declared anywhere today, but the fix protects all four.services,analytics,developer,completions,prof) don't re-declare global options inside subcommand blocks, so no behaviour change there.brew bundle upgradeis unaffected here because Make bundle upgrade an alias #22348 already turned it into an alias of install--upgrade.Tests
test/cli/parser_spec.rb: parser-level regression — a global option re-declared in one subcommand block is still accepted on other subcommands.test/cmd/bundle_spec.rb: bundle-level regression —cleanup,dump,listaccept--verbose/-vafter parsing.Both verified with a red/green cycle (failing on main, passing with the fix).
Manual verification