diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 9b4f41e6e4..33fed1bfb8 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -605,8 +605,11 @@ module Homebrew :subcommand end.compact.uniq - exception = if @min_named_args && args.size < @min_named_args - MinNamedArgumentsError.new(@min_named_args, types: types, exact: @min_named_args == @max_named_args) + exception = if @min_named_args && @max_named_args && @min_named_args == @max_named_args && + args.size != @max_named_args + NumberOfNamedArgumentsError.new(@min_named_args, types: types) + elsif @min_named_args && args.size < @min_named_args + MinNamedArgumentsError.new(@min_named_args, types: types) elsif @max_named_args && args.size > @max_named_args MaxNamedArgumentsError.new(@max_named_args, types: types) end @@ -701,15 +704,26 @@ module Homebrew class MinNamedArgumentsError < UsageError extend T::Sig - sig { params(minimum: Integer, types: T::Array[Symbol], exact: T::Boolean).void } - def initialize(minimum, types: [], exact: false) - number_phrase = exact ? "exactly" : "at least" - + sig { params(minimum: Integer, types: T::Array[Symbol]).void } + def initialize(minimum, types: []) types << :named if types.empty? arg_types = types.map { |type| type.to_s.tr("_", " ") } .to_sentence two_words_connector: " or ", last_word_connector: " or " - super "This command requires #{number_phrase} #{minimum} #{arg_types} #{"argument".pluralize(minimum)}." + super "This command requires at least #{minimum} #{arg_types} #{"argument".pluralize(minimum)}." + end + end + + class NumberOfNamedArgumentsError < UsageError + extend T::Sig + + sig { params(minimum: Integer, types: T::Array[Symbol]).void } + def initialize(minimum, types: []) + types << :named if types.empty? + arg_types = types.map { |type| type.to_s.tr("_", " ") } + .to_sentence two_words_connector: " or ", last_word_connector: " or " + + super "This command requires exactly #{minimum} #{arg_types} #{"argument".pluralize(minimum)}." end end end diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index 45f31c5223..c50c9fe2f7 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -455,11 +455,11 @@ describe Homebrew::CLI::Parser do end it "doesn't accept fewer than the passed number of arguments" do - expect { parser_number.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError) + expect { parser_number.parse([]) }.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError) end it "doesn't accept more than the passed number of arguments" do - expect { parser_number.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::MaxNamedArgumentsError) + expect { parser_number.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError) end it "accepts the passed number of arguments" do @@ -489,7 +489,7 @@ describe Homebrew::CLI::Parser do named_args number: 2 end expect { parser.parse([]) }.to raise_error( - Homebrew::CLI::MinNamedArgumentsError, /This command requires exactly 2 named arguments/ + Homebrew::CLI::NumberOfNamedArgumentsError, /This command requires exactly 2 named arguments/ ) end @@ -507,7 +507,7 @@ describe Homebrew::CLI::Parser do named_args %w[on off], number: 1 end expect { parser.parse([]) }.to raise_error( - Homebrew::CLI::MinNamedArgumentsError, /This command requires exactly 1 subcommand/ + Homebrew::CLI::NumberOfNamedArgumentsError, /This command requires exactly 1 subcommand/ ) end @@ -542,7 +542,7 @@ describe Homebrew::CLI::Parser do end it "doesn't allow less than the specified number of arguments" do - expect { parser.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError) + expect { parser.parse([]) }.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError) end it "treats a symbol as a single argument of the specified type" do @@ -550,12 +550,12 @@ describe Homebrew::CLI::Parser do named :formula end expect { formula_parser.parse([]) }.to raise_error( - Homebrew::CLI::MinNamedArgumentsError, /This command requires exactly 1 formula argument/ + Homebrew::CLI::NumberOfNamedArgumentsError, /This command requires exactly 1 formula argument/ ) end it "doesn't allow more than the specified number of arguments" do - expect { parser.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::MaxNamedArgumentsError) + expect { parser.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError) end end diff --git a/completions/bash/brew b/completions/bash/brew index a086378a02..323a30a082 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -898,6 +898,8 @@ _brew_extract() { return ;; esac + __brew_complete_formulae + __brew_complete_tapped } _brew_fetch() {