summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfatkodima <[email protected]>2024-08-05 05:28:01 +0300
committergit <[email protected]>2024-08-05 02:28:12 +0000
commita35d32486242641ccd2ac98d43211343a3712c52 (patch)
treeac670d9f747c64126664be3bef8e4e21f09b1ef6
parentcbc40bb130fb52f1990ce1fd41e834a5f3fdbcdd (diff)
[ruby/optparse] Fix parsing array arguments with `:into` option
https://github.com/ruby/optparse/commit/19700e96d8
-rw-r--r--lib/optparse.rb14
-rw-r--r--test/optparse/test_acceptable.rb2
-rw-r--r--test/optparse/test_optparse.rb13
3 files changed, 23 insertions, 6 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index 069c3e436e..d52401a31b 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1729,9 +1729,9 @@ XXX
end
end
begin
- opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
- val = callback!(cb, 1, *val) if cb
- callback!(setter, 2, sw.switch_name, *val) if setter
+ opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
+ val = callback!(cb, 1, val) if cb
+ callback!(setter, 2, sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
@@ -1761,7 +1761,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
@@ -1769,8 +1769,8 @@ XXX
end
begin
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
- val = callback!(cb, 1, *val) if cb
- callback!(setter, 2, sw.switch_name, *val) if setter
+ val = callback!(cb, 1, val) if cb
+ callback!(setter, 2, sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
end
@@ -1798,6 +1798,8 @@ XXX
# Calls callback with _val_.
def callback!(cb, max_arity, *args) # :nodoc:
+ args.compact!
+
if (size = args.size) < max_arity and cb.to_proc.lambda?
(arity = cb.arity) < 0 and arity = (1-arity)
arity = max_arity if arity > max_arity
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb
index c7ea2152fc..8b578effde 100644
--- a/test/optparse/test_acceptable.rb
+++ b/test/optparse/test_acceptable.rb
@@ -199,5 +199,7 @@ class TestOptionParserAcceptable < TestOptionParser
def test_array
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
assert_equal(%w"a b c", @array)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--array a")})
+ assert_equal(%w"a", @array)
end
end
diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb
index 8d09e0f28b..7f35cb4a8a 100644
--- a/test/optparse/test_optparse.rb
+++ b/test/optparse/test_optparse.rb
@@ -74,6 +74,7 @@ class TestOptionParser < Test::Unit::TestCase
@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
+ @opt.def_option "-a", "--array [VAL]", Array do |val| val end
result = {}
@opt.parse %w(--host localhost --port 8000 -v), into: result
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
@@ -84,6 +85,18 @@ class TestOptionParser < Test::Unit::TestCase
result = {}
@opt.parse %w(--option OPTION -v), into: result
assert_equal({verbose: true, option: "OPTION"}, result)
+ result = {}
+ @opt.parse %w(-a b,c,d), into: result
+ assert_equal({array: %w(b c d)}, result)
+ result = {}
+ @opt.parse %w(--array b,c,d), into: result
+ assert_equal({array: %w(b c d)}, result)
+ result = {}
+ @opt.parse %w(-a b), into: result
+ assert_equal({array: %w(b)}, result)
+ result = {}
+ @opt.parse %w(--array b), into: result
+ assert_equal({array: %w(b)}, result)
end
def test_require_exact