[ruby/optparse] Respect default values in block parameters
Fix https://github.com/ruby/optparse/pull/55 https://github.com/ruby/optparse/commit/9d53e74aa4
This commit is contained in:
parent
5e12b75716
commit
2c6767b71e
@ -697,6 +697,11 @@ class OptionParser
|
|||||||
q.object_group(self) {pretty_print_contents(q)}
|
q.object_group(self) {pretty_print_contents(q)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def omitted_argument(val) # :nodoc:
|
||||||
|
val.pop if val.size == 3 and val.last.nil?
|
||||||
|
val
|
||||||
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Switch that takes no arguments.
|
# Switch that takes no arguments.
|
||||||
#
|
#
|
||||||
@ -755,7 +760,7 @@ class OptionParser
|
|||||||
if arg
|
if arg
|
||||||
conv_arg(*parse_arg(arg, &error))
|
conv_arg(*parse_arg(arg, &error))
|
||||||
else
|
else
|
||||||
conv_arg(arg)
|
omitted_argument conv_arg(arg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -774,13 +779,14 @@ class OptionParser
|
|||||||
#
|
#
|
||||||
def parse(arg, argv, &error)
|
def parse(arg, argv, &error)
|
||||||
if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
|
if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
|
||||||
return nil, block, nil
|
return nil, block
|
||||||
end
|
end
|
||||||
opt = (val = parse_arg(val, &error))[1]
|
opt = (val = parse_arg(val, &error))[1]
|
||||||
val = conv_arg(*val)
|
val = conv_arg(*val)
|
||||||
if opt and !arg
|
if opt and !arg
|
||||||
argv.shift
|
argv.shift
|
||||||
else
|
else
|
||||||
|
omitted_argument val
|
||||||
val[0] = nil
|
val[0] = nil
|
||||||
end
|
end
|
||||||
val
|
val
|
||||||
@ -1633,7 +1639,7 @@ XXX
|
|||||||
# Non-option arguments remain in +argv+.
|
# Non-option arguments remain in +argv+.
|
||||||
#
|
#
|
||||||
def order!(argv = default_argv, into: nil, &nonopt)
|
def order!(argv = default_argv, into: nil, &nonopt)
|
||||||
setter = ->(name, val) {into[name.to_sym] = val} if into
|
setter = ->(name, val = nil) {into[name.to_sym] = val} if into
|
||||||
parse_in_order(argv, setter, &nonopt)
|
parse_in_order(argv, setter, &nonopt)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1658,9 +1664,9 @@ XXX
|
|||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
||||||
val = cb.call(val) if cb
|
val = cb.call(*val) if cb
|
||||||
setter.call(sw.switch_name, val) if setter
|
setter.call(sw.switch_name, *val) if setter
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, rest)
|
raise $!.set_option(arg, rest)
|
||||||
end
|
end
|
||||||
@ -1690,7 +1696,7 @@ XXX
|
|||||||
raise $!.set_option(arg, true)
|
raise $!.set_option(arg, true)
|
||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
opt, cb, *val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, arg.length > 2)
|
raise $!.set_option(arg, arg.length > 2)
|
||||||
else
|
else
|
||||||
@ -1698,8 +1704,8 @@ XXX
|
|||||||
end
|
end
|
||||||
begin
|
begin
|
||||||
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
||||||
val = cb.call(val) if cb
|
val = cb.call(*val) if cb
|
||||||
setter.call(sw.switch_name, val) if setter
|
setter.call(sw.switch_name, *val) if setter
|
||||||
rescue ParseError
|
rescue ParseError
|
||||||
raise $!.set_option(arg, arg.length > 2)
|
raise $!.set_option(arg, arg.length > 2)
|
||||||
end
|
end
|
||||||
|
@ -8,6 +8,7 @@ class TestOptionParserAcceptable < TestOptionParser
|
|||||||
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
|
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
|
||||||
@opt.def_option("--float VAL", Float) { |v| @float = v }
|
@opt.def_option("--float VAL", Float) { |v| @float = v }
|
||||||
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
|
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
|
||||||
|
@opt.def_option("--array VAL", Array) { |v| @array = v }
|
||||||
|
|
||||||
@opt.def_option("--decimal-integer VAL",
|
@opt.def_option("--decimal-integer VAL",
|
||||||
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
|
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
|
||||||
@ -195,4 +196,8 @@ class TestOptionParserAcceptable < TestOptionParser
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_array
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
|
||||||
|
assert_equal(%w"a b c", @array)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -9,6 +9,7 @@ class TestOptionParserOptArg < TestOptionParser
|
|||||||
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
|
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
|
||||||
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
|
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
|
||||||
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
|
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
|
||||||
|
@opt.def_option("--fallback[=VAL]") do |x = "fallback"| @flag = x end
|
||||||
@reopt = nil
|
@reopt = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,4 +58,11 @@ class TestOptionParserOptArg < TestOptionParser
|
|||||||
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
|
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
|
||||||
assert_equal("foo4", @flag)
|
assert_equal("foo4", @flag)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_default_argument
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
|
||||||
|
assert_equal("val1", @flag)
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
|
||||||
|
assert_equal("fallback", @flag)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -73,10 +73,17 @@ class TestOptionParser < Test::Unit::TestCase
|
|||||||
@opt.def_option "-p", "--port=PORT", "port", Integer
|
@opt.def_option "-p", "--port=PORT", "port", Integer
|
||||||
@opt.def_option "-v", "--verbose" do @verbose = true end
|
@opt.def_option "-v", "--verbose" do @verbose = true end
|
||||||
@opt.def_option "-q", "--quiet" do @quiet = true end
|
@opt.def_option "-q", "--quiet" do @quiet = true end
|
||||||
|
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
|
||||||
result = {}
|
result = {}
|
||||||
@opt.parse %w(--host localhost --port 8000 -v), into: result
|
@opt.parse %w(--host localhost --port 8000 -v), into: result
|
||||||
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
|
||||||
assert_equal(true, @verbose)
|
assert_equal(true, @verbose)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(--option -q), into: result
|
||||||
|
assert_equal({quiet: true, option: nil}, result)
|
||||||
|
result = {}
|
||||||
|
@opt.parse %w(--option OPTION -v), into: result
|
||||||
|
assert_equal({verbose: true, option: "OPTION"}, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_require_exact
|
def test_require_exact
|
||||||
|
@ -13,6 +13,7 @@ class TestOptionParserPlaceArg < TestOptionParser
|
|||||||
@reopt = nil
|
@reopt = nil
|
||||||
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
|
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
|
||||||
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
|
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
|
||||||
|
@opt.def_option("--fallback [VAL]") do |x = "fallback"| @flag = x end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_short
|
def test_short
|
||||||
@ -73,4 +74,13 @@ class TestOptionParserPlaceArg < TestOptionParser
|
|||||||
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
|
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
|
||||||
assert_equal(1, @topt)
|
assert_equal(1, @topt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_default_argument
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
|
||||||
|
assert_equal("val1", @flag)
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback val2")})
|
||||||
|
assert_equal("val2", @flag)
|
||||||
|
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
|
||||||
|
assert_equal("fallback", @flag)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user