cli_parser: Refactor required argument for flag to '='
This commit is contained in:
parent
04d1d04779
commit
48aecf2fa7
@ -34,14 +34,15 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def flag(name, description: nil, required: false)
|
def flag(name, description: nil)
|
||||||
if required
|
if name.end_with? "="
|
||||||
option_required = OptionParser::REQUIRED_ARGUMENT
|
required = OptionParser::REQUIRED_ARGUMENT
|
||||||
|
name.chomp! "="
|
||||||
else
|
else
|
||||||
option_required = OptionParser::OPTIONAL_ARGUMENT
|
required = OptionParser::OPTIONAL_ARGUMENT
|
||||||
end
|
end
|
||||||
description = option_to_description(name) if description.nil?
|
description = option_to_description(name) if description.nil?
|
||||||
@parser.on(name, description, option_required) do |option_value|
|
@parser.on(name, description, required) do |option_value|
|
||||||
@parsed_args[option_to_name(name)] = option_value
|
@parsed_args[option_to_name(name)] = option_value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -86,8 +86,8 @@ module Homebrew
|
|||||||
switch "--warn-on-publish-failure"
|
switch "--warn-on-publish-failure"
|
||||||
switch :verbose
|
switch :verbose
|
||||||
switch :debug
|
switch :debug
|
||||||
flag "--bintray-org", required: true
|
flag "--bintray-org="
|
||||||
flag "--test-bot-user", required: true
|
flag "--test-bot-user="
|
||||||
end
|
end
|
||||||
|
|
||||||
if ARGV.named.empty?
|
if ARGV.named.empty?
|
||||||
|
@ -29,8 +29,8 @@ module Homebrew
|
|||||||
switch "--online"
|
switch "--online"
|
||||||
switch :debug
|
switch :debug
|
||||||
switch :verbose
|
switch :verbose
|
||||||
flag "--only", required: true
|
flag "--only="
|
||||||
flag "--seed", required: true
|
flag "--seed="
|
||||||
end
|
end
|
||||||
|
|
||||||
HOMEBREW_LIBRARY_PATH.cd do
|
HOMEBREW_LIBRARY_PATH.cd do
|
||||||
|
@ -25,8 +25,8 @@ module Homebrew
|
|||||||
switch "--keep-tmp"
|
switch "--keep-tmp"
|
||||||
switch :verbose
|
switch :verbose
|
||||||
switch :debug
|
switch :debug
|
||||||
flag "--commit", required: true
|
flag "--commit="
|
||||||
flag "--before", required: true
|
flag "--before="
|
||||||
end
|
end
|
||||||
|
|
||||||
ENV["HOMEBREW_UPDATE_TEST"] = "1"
|
ENV["HOMEBREW_UPDATE_TEST"] = "1"
|
||||||
|
@ -51,8 +51,8 @@ describe Homebrew::CLI::Parser do
|
|||||||
describe "test long flag options" do
|
describe "test long flag options" do
|
||||||
subject(:parser) {
|
subject(:parser) {
|
||||||
described_class.new do
|
described_class.new do
|
||||||
flag "--filename", description: "Name of the file", required: true
|
flag "--filename=", description: "Name of the file"
|
||||||
comma_array "--files", description: "Comma separated filenames"
|
comma_array "--files", description: "Comma separated filenames"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ describe Homebrew::CLI::Parser do
|
|||||||
expect(args.filename).to eq "random.txt"
|
expect(args.filename).to eq "random.txt"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an exception when a flag's required arg is not passed" do
|
it "raises an exception when a flag's required value is not passed" do
|
||||||
expect { parser.parse(["--filename"]) }.to raise_error(OptionParser::MissingArgument, /--filename/)
|
expect { parser.parse(["--filename"]) }.to raise_error(OptionParser::MissingArgument, /--filename/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user