From 326321c1fdcba7717ec27e3db8cec9c0dfe15069 Mon Sep 17 00:00:00 2001 From: FnControlOption <70830482+FnControlOption@users.noreply.github.com> Date: Wed, 25 Aug 2021 14:34:57 -0700 Subject: [PATCH] cli/parser: allow commands that look like options --- Library/Homebrew/cli/parser.rb | 3 ++- Library/Homebrew/test/cli/parser_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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