[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:
Nobuyoshi Nakada 2024-02-09 12:59:43 +09:00
parent 5e12b75716
commit 2c6767b71e
5 changed files with 45 additions and 9 deletions

View File

@ -697,6 +697,11 @@ class OptionParser
q.object_group(self) {pretty_print_contents(q)}
end
def omitted_argument(val) # :nodoc:
val.pop if val.size == 3 and val.last.nil?
val
end
#
# Switch that takes no arguments.
#
@ -755,7 +760,7 @@ class OptionParser
if arg
conv_arg(*parse_arg(arg, &error))
else
conv_arg(arg)
omitted_argument conv_arg(arg)
end
end
@ -774,13 +779,14 @@ class OptionParser
#
def parse(arg, argv, &error)
if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
return nil, block, nil
return nil, block
end
opt = (val = parse_arg(val, &error))[1]
val = conv_arg(*val)
if opt and !arg
argv.shift
else
omitted_argument val
val[0] = nil
end
val
@ -1633,7 +1639,7 @@ XXX
# Non-option arguments remain in +argv+.
#
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)
end
@ -1658,9 +1664,9 @@ XXX
raise $!.set_option(arg, true)
end
begin
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
val = cb.call(*val) if cb
setter.call(sw.switch_name, *val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
@ -1690,7 +1696,7 @@ XXX
raise $!.set_option(arg, true)
end
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
raise $!.set_option(arg, arg.length > 2)
else
@ -1698,8 +1704,8 @@ XXX
end
begin
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
val = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter
val = cb.call(*val) if cb
setter.call(sw.switch_name, *val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
end

View File

@ -8,6 +8,7 @@ class TestOptionParserAcceptable < TestOptionParser
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
@opt.def_option("--float VAL", Float) { |v| @float = 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",
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
@ -195,4 +196,8 @@ class TestOptionParserAcceptable < TestOptionParser
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

View File

@ -9,6 +9,7 @@ class TestOptionParserOptArg < TestOptionParser
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
@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("--fallback[=VAL]") do |x = "fallback"| @flag = x end
@reopt = nil
end
@ -57,4 +58,11 @@ class TestOptionParserOptArg < TestOptionParser
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
assert_equal("foo4", @flag)
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

View File

@ -73,10 +73,17 @@ class TestOptionParser < Test::Unit::TestCase
@opt.def_option "-p", "--port=PORT", "port", Integer
@opt.def_option "-v", "--verbose" do @verbose = true end
@opt.def_option "-q", "--quiet" do @quiet = true end
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
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
def test_require_exact

View File

@ -13,6 +13,7 @@ class TestOptionParserPlaceArg < TestOptionParser
@reopt = nil
@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("--fallback [VAL]") do |x = "fallback"| @flag = x end
end
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(1, @topt)
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