parser: create NumberOfNamedArgumentsError

And commit `brew man` changes
This commit is contained in:
Rylan Polster 2021-01-23 15:25:56 -05:00
parent f295e36a22
commit 01e894e9c6
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
3 changed files with 30 additions and 14 deletions

View File

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

View File

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

View File

@ -898,6 +898,8 @@ _brew_extract() {
return
;;
esac
__brew_complete_formulae
__brew_complete_tapped
}
_brew_fetch() {