diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index e4ce0c784b..159f3ddf85 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -264,6 +264,7 @@ module Homebrew remaining = [] argv, non_options = split_non_options(argv) + allow_commands = Array(@named_args_type).include?(:command) while i < argv.count begin @@ -279,7 +280,7 @@ module Homebrew 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 diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index 30f8d4d225..69eea85296 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -559,5 +559,19 @@ describe Homebrew::CLI::Parser do 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