parser: create NumberOfNamedArgumentsError
And commit `brew man` changes
This commit is contained in:
parent
f295e36a22
commit
01e894e9c6
@ -605,8 +605,11 @@ module Homebrew
|
|||||||
:subcommand
|
:subcommand
|
||||||
end.compact.uniq
|
end.compact.uniq
|
||||||
|
|
||||||
exception = if @min_named_args && args.size < @min_named_args
|
exception = if @min_named_args && @max_named_args && @min_named_args == @max_named_args &&
|
||||||
MinNamedArgumentsError.new(@min_named_args, types: types, exact: @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
|
elsif @max_named_args && args.size > @max_named_args
|
||||||
MaxNamedArgumentsError.new(@max_named_args, types: types)
|
MaxNamedArgumentsError.new(@max_named_args, types: types)
|
||||||
end
|
end
|
||||||
@ -701,15 +704,26 @@ module Homebrew
|
|||||||
class MinNamedArgumentsError < UsageError
|
class MinNamedArgumentsError < UsageError
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
sig { params(minimum: Integer, types: T::Array[Symbol], exact: T::Boolean).void }
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
def initialize(minimum, types: [], exact: false)
|
def initialize(minimum, types: [])
|
||||||
number_phrase = exact ? "exactly" : "at least"
|
|
||||||
|
|
||||||
types << :named if types.empty?
|
types << :named if types.empty?
|
||||||
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
.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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -455,11 +455,11 @@ describe Homebrew::CLI::Parser do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't accept fewer than the passed number of arguments" do
|
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
|
end
|
||||||
|
|
||||||
it "doesn't accept more than the passed number of arguments" do
|
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
|
end
|
||||||
|
|
||||||
it "accepts the passed number of arguments" do
|
it "accepts the passed number of arguments" do
|
||||||
@ -489,7 +489,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
named_args number: 2
|
named_args number: 2
|
||||||
end
|
end
|
||||||
expect { parser.parse([]) }.to raise_error(
|
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
|
end
|
||||||
|
|
||||||
@ -507,7 +507,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
named_args %w[on off], number: 1
|
named_args %w[on off], number: 1
|
||||||
end
|
end
|
||||||
expect { parser.parse([]) }.to raise_error(
|
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
|
end
|
||||||
|
|
||||||
@ -542,7 +542,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't allow less than the specified number of arguments" do
|
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
|
end
|
||||||
|
|
||||||
it "treats a symbol as a single argument of the specified type" do
|
it "treats a symbol as a single argument of the specified type" do
|
||||||
@ -550,12 +550,12 @@ describe Homebrew::CLI::Parser do
|
|||||||
named :formula
|
named :formula
|
||||||
end
|
end
|
||||||
expect { formula_parser.parse([]) }.to raise_error(
|
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
|
end
|
||||||
|
|
||||||
it "doesn't allow more than the specified number of arguments" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -898,6 +898,8 @@ _brew_extract() {
|
|||||||
return
|
return
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
__brew_complete_formulae
|
||||||
|
__brew_complete_tapped
|
||||||
}
|
}
|
||||||
|
|
||||||
_brew_fetch() {
|
_brew_fetch() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user