Merge pull request #11923 from FnControlOption/cli

cli/parser: allow commands that look like options
This commit is contained in:
Mike McQuaid 2021-08-26 13:18:57 +01:00 committed by GitHub
commit 37739df38a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -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

View File

@ -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