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

cli/parser: allow commands that look like options #11923

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def parse_remaining(argv, ignore_invalid_options: false)
remaining = []

argv, non_options = split_non_options(argv)
allow_commands = Array(@named_args_type).include?(:command)

while i < argv.count
begin
Expand All @@ -279,7 +280,7 @@ def parse_remaining(argv, ignore_invalid_options: false)
i += 1
end
rescue OptionParser::InvalidOption
if ignore_invalid_options
if ignore_invalid_options || (allow_commands && Commands.path(arg))
remaining << arg
else
$stderr.puts generate_help_text
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/test/cli/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,5 +559,19 @@
Homebrew::CLI::MaxNamedArgumentsError, /This command does not take more than 1 formula or cask argument/
)
end

it "accepts commands with :command" do
parser = described_class.new do
named_args :command
end
expect { parser.parse(["--prefix", "--version"]) }.not_to raise_error
end

it "doesn't accept invalid options with :command" do
parser = described_class.new do
named_args :command
end
expect { parser.parse(["--not-a-command"]) }.to raise_error(OptionParser::InvalidOption, /--not-a-command/)
end
end
end