Merge pull request #10400 from issyl0/improve-brew-extract-args
dev-cmd/extract: Improve the usage instructions
This commit is contained in:
commit
d0e1c3dc5f
@ -599,21 +599,19 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_named_args(args)
|
def check_named_args(args)
|
||||||
exception = if @min_named_args && args.size < @min_named_args
|
types = Array(@named_args_type).map do |type|
|
||||||
if @named_args_type.present?
|
next type if type.is_a? Symbol
|
||||||
types = Array(@named_args_type)
|
|
||||||
if types.any? { |arg| arg.is_a? String }
|
:subcommand
|
||||||
MinNamedArgumentsError.new(@min_named_args)
|
end.compact.uniq
|
||||||
else
|
|
||||||
list = types.map { |type| type.to_s.tr("_", " ") }
|
exception = if @min_named_args && @max_named_args && @min_named_args == @max_named_args &&
|
||||||
list = list.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
args.size != @max_named_args
|
||||||
UsageError.new("this command requires a #{list} argument")
|
NumberOfNamedArgumentsError.new(@min_named_args, types: types)
|
||||||
end
|
elsif @min_named_args && args.size < @min_named_args
|
||||||
else
|
MinNamedArgumentsError.new(@min_named_args, types: types)
|
||||||
MinNamedArgumentsError.new(@min_named_args)
|
|
||||||
end
|
|
||||||
elsif @max_named_args && args.size > @max_named_args
|
elsif @max_named_args && args.size > @max_named_args
|
||||||
MaxNamedArgumentsError.new(@max_named_args)
|
MaxNamedArgumentsError.new(@max_named_args, types: types)
|
||||||
end
|
end
|
||||||
|
|
||||||
raise exception if exception
|
raise exception if exception
|
||||||
@ -688,13 +686,17 @@ module Homebrew
|
|||||||
class MaxNamedArgumentsError < UsageError
|
class MaxNamedArgumentsError < UsageError
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
sig { params(maximum: Integer).void }
|
sig { params(maximum: Integer, types: T::Array[Symbol]).void }
|
||||||
def initialize(maximum)
|
def initialize(maximum, types: [])
|
||||||
super case maximum
|
super case maximum
|
||||||
when 0
|
when 0
|
||||||
"This command does not take named arguments."
|
"This command does not take named arguments."
|
||||||
else
|
else
|
||||||
"This command does not take more than #{maximum} named #{"argument".pluralize(maximum)}"
|
types << :named if types.empty?
|
||||||
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
|
"This command does not take more than #{maximum} #{arg_types} #{"argument".pluralize(maximum)}."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -702,9 +704,26 @@ module Homebrew
|
|||||||
class MinNamedArgumentsError < UsageError
|
class MinNamedArgumentsError < UsageError
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
sig { params(minimum: Integer).void }
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
def initialize(minimum)
|
def initialize(minimum, types: [])
|
||||||
super "This command requires at least #{minimum} named #{"argument".pluralize(minimum)}."
|
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 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
|
||||||
|
|||||||
@ -83,6 +83,7 @@ module Homebrew
|
|||||||
sig { returns(CLI::Parser) }
|
sig { returns(CLI::Parser) }
|
||||||
def extract_args
|
def extract_args
|
||||||
Homebrew::CLI::Parser.new do
|
Homebrew::CLI::Parser.new do
|
||||||
|
usage_banner "`extract` [<--version>`=`] [<--force>] <formula> <tap>"
|
||||||
description <<~EOS
|
description <<~EOS
|
||||||
Look through repository history to find the most recent version of <formula> and
|
Look through repository history to find the most recent version of <formula> and
|
||||||
create a copy in <tap>`/Formula/`<formula>`@`<version>`.rb`. If the tap is not
|
create a copy in <tap>`/Formula/`<formula>`@`<version>`.rb`. If the tap is not
|
||||||
@ -95,7 +96,7 @@ module Homebrew
|
|||||||
switch "-f", "--force",
|
switch "-f", "--force",
|
||||||
description: "Overwrite the destination formula if it already exists."
|
description: "Overwrite the destination formula if it already exists."
|
||||||
|
|
||||||
named_args :formula, number: 2
|
named_args [:formula, :tap], number: 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -47,7 +47,7 @@ describe Cask::Cmd::Create, :cask do
|
|||||||
it "raises an exception when more than one Cask is given" do
|
it "raises an exception when more than one Cask is given" do
|
||||||
expect {
|
expect {
|
||||||
described_class.run("additional-cask", "another-cask")
|
described_class.run("additional-cask", "another-cask")
|
||||||
}.to raise_error(UsageError, /Only one cask can be created at a time\./)
|
}.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an exception when the Cask already exists" do
|
it "raises an exception when the Cask already exists" do
|
||||||
|
|||||||
@ -21,7 +21,7 @@ describe Cask::Cmd::Edit, :cask do
|
|||||||
it "raises an error when given more than one argument" do
|
it "raises an error when given more than one argument" do
|
||||||
expect {
|
expect {
|
||||||
described_class.new("local-caffeine", "local-transmission")
|
described_class.new("local-caffeine", "local-transmission")
|
||||||
}.to raise_error(UsageError, /Only one cask can be edited at a time\./)
|
}.to raise_error(Homebrew::CLI::NumberOfNamedArgumentsError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an exception when the Cask doesn't exist" do
|
it "raises an exception when the Cask doesn't exist" do
|
||||||
|
|||||||
@ -6,7 +6,7 @@ shared_examples "a command that requires a Cask token" do
|
|||||||
it "raises an exception " do
|
it "raises an exception " do
|
||||||
expect {
|
expect {
|
||||||
described_class.run
|
described_class.run
|
||||||
}.to raise_error(UsageError, /this command requires a .*cask.* argument/)
|
}.to raise_error(UsageError, /This command requires .*cask.* argument/)
|
||||||
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
|
||||||
@ -475,18 +475,58 @@ describe Homebrew::CLI::Parser do
|
|||||||
expect { parser_none.parse([]) }.not_to raise_error
|
expect { parser_none.parse([]) }.not_to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "displays the correct error message with no arg types and min" do
|
||||||
|
parser = described_class.new do
|
||||||
|
named_args min: 2
|
||||||
|
end
|
||||||
|
expect { parser.parse([]) }.to raise_error(
|
||||||
|
Homebrew::CLI::MinNamedArgumentsError, /This command requires at least 2 named arguments/
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "displays the correct error message with no arg types and number" do
|
||||||
|
parser = described_class.new do
|
||||||
|
named_args number: 2
|
||||||
|
end
|
||||||
|
expect { parser.parse([]) }.to raise_error(
|
||||||
|
Homebrew::CLI::NumberOfNamedArgumentsError, /This command requires exactly 2 named arguments/
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "displays the correct error message with no arg types and max" do
|
||||||
|
parser = described_class.new do
|
||||||
|
named_args max: 1
|
||||||
|
end
|
||||||
|
expect { parser.parse(%w[foo bar]) }.to raise_error(
|
||||||
|
Homebrew::CLI::MaxNamedArgumentsError, /This command does not take more than 1 named argument/
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
it "displays the correct error message with an array of strings" do
|
it "displays the correct error message with an array of strings" do
|
||||||
parser = described_class.new do
|
parser = described_class.new do
|
||||||
named_args %w[on off], min: 1
|
named_args %w[on off], number: 1
|
||||||
end
|
end
|
||||||
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError)
|
expect { parser.parse([]) }.to raise_error(
|
||||||
|
Homebrew::CLI::NumberOfNamedArgumentsError, /This command requires exactly 1 subcommand/
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "displays the correct error message with an array of symbols" do
|
it "displays the correct error message with an array of symbols" do
|
||||||
parser = described_class.new do
|
parser = described_class.new do
|
||||||
named_args [:formula, :cask], min: 1
|
named_args [:formula, :cask], min: 1
|
||||||
end
|
end
|
||||||
expect { parser.parse([]) }.to raise_error(UsageError, /this command requires a formula or cask argument/)
|
expect { parser.parse([]) }.to raise_error(
|
||||||
|
Homebrew::CLI::MinNamedArgumentsError, /This command requires at least 1 formula or cask argument/
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "displays the correct error message with an array of symbols and max" do
|
||||||
|
parser = described_class.new do
|
||||||
|
named_args [:formula, :cask], max: 1
|
||||||
|
end
|
||||||
|
expect { parser.parse(%w[foo bar]) }.to raise_error(
|
||||||
|
Homebrew::CLI::MaxNamedArgumentsError, /This command does not take more than 1 formula or cask argument/
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -502,18 +542,20 @@ 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
|
||||||
formula_parser = described_class.new do
|
formula_parser = described_class.new do
|
||||||
named :formula
|
named :formula
|
||||||
end
|
end
|
||||||
expect { formula_parser.parse([]) }.to raise_error(UsageError, /this command requires a formula argument/)
|
expect { formula_parser.parse([]) }.to raise_error(
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@ -899,6 +899,7 @@ _brew_extract() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
__brew_complete_formulae
|
__brew_complete_formulae
|
||||||
|
__brew_complete_tapped
|
||||||
}
|
}
|
||||||
|
|
||||||
_brew_fetch() {
|
_brew_fetch() {
|
||||||
|
|||||||
@ -1019,7 +1019,7 @@ or open the Homebrew repository for editing if no formula is provided.
|
|||||||
* `--cask`:
|
* `--cask`:
|
||||||
Treat all named arguments as casks.
|
Treat all named arguments as casks.
|
||||||
|
|
||||||
### `extract` [*`--version`*`=`] [*`--force`*] *`formula`* ...
|
### `extract` [*`--version`*`=`] [*`--force`*] *`formula`* *`tap`*
|
||||||
|
|
||||||
Look through repository history to find the most recent version of *`formula`* and
|
Look through repository history to find the most recent version of *`formula`* and
|
||||||
create a copy in *`tap`*`/Formula/`*`formula`*`@`*`version`*`.rb`. If the tap is not
|
create a copy in *`tap`*`/Formula/`*`formula`*`@`*`version`*`.rb`. If the tap is not
|
||||||
|
|||||||
@ -1417,7 +1417,7 @@ Treat all named arguments as formulae\.
|
|||||||
\fB\-\-cask\fR
|
\fB\-\-cask\fR
|
||||||
Treat all named arguments as casks\.
|
Treat all named arguments as casks\.
|
||||||
.
|
.
|
||||||
.SS "\fBextract\fR [\fI\-\-version\fR\fB=\fR] [\fI\-\-force\fR] \fIformula\fR \.\.\."
|
.SS "\fBextract\fR [\fI\-\-version\fR\fB=\fR] [\fI\-\-force\fR] \fIformula\fR \fItap\fR"
|
||||||
Look through repository history to find the most recent version of \fIformula\fR and create a copy in \fItap\fR\fB/Formula/\fR\fIformula\fR\fB@\fR\fIversion\fR\fB\.rb\fR\. If the tap is not installed yet, attempt to install/clone the tap before continuing\. To extract a formula from a tap that is not \fBhomebrew/core\fR use its fully\-qualified form of \fIuser\fR\fB/\fR\fIrepo\fR\fB/\fR\fIformula\fR\.
|
Look through repository history to find the most recent version of \fIformula\fR and create a copy in \fItap\fR\fB/Formula/\fR\fIformula\fR\fB@\fR\fIversion\fR\fB\.rb\fR\. If the tap is not installed yet, attempt to install/clone the tap before continuing\. To extract a formula from a tap that is not \fBhomebrew/core\fR use its fully\-qualified form of \fIuser\fR\fB/\fR\fIrepo\fR\fB/\fR\fIformula\fR\.
|
||||||
.
|
.
|
||||||
.TP
|
.TP
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user