Skip to content

Commit 213cb03

Browse files
committed
Adjust arguments for lambda-callbacks
Rake uses [lambda] as callbacks. Calling it without omitted argument raises an `ArgumentError`. lambda: https://github.com/ruby/rake/blob/master/lib/rake/application.rb#L543
1 parent 9d53e74 commit 213cb03

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

lib/optparse.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ def order(*argv, into: nil, &nonopt)
16391639
# Non-option arguments remain in +argv+.
16401640
#
16411641
def order!(argv = default_argv, into: nil, &nonopt)
1642-
setter = ->(name, val = nil) {into[name.to_sym] = val} if into
1642+
setter = ->(name, val) {into[name.to_sym] = val} if into
16431643
parse_in_order(argv, setter, &nonopt)
16441644
end
16451645

@@ -1665,8 +1665,8 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
16651665
end
16661666
begin
16671667
opt, cb, *val = sw.parse(rest, argv) {|*exc| raise(*exc)}
1668-
val = cb.call(*val) if cb
1669-
setter.call(sw.switch_name, *val) if setter
1668+
val = callback!(cb, 1, *val) if cb
1669+
callback!(setter, 2, sw.switch_name, *val) if setter
16701670
rescue ParseError
16711671
raise $!.set_option(arg, rest)
16721672
end
@@ -1704,8 +1704,8 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
17041704
end
17051705
begin
17061706
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
1707-
val = cb.call(*val) if cb
1708-
setter.call(sw.switch_name, *val) if setter
1707+
val = callback!(cb, 1, *val) if cb
1708+
callback!(setter, 2, sw.switch_name, *val) if setter
17091709
rescue ParseError
17101710
raise $!.set_option(arg, arg.length > 2)
17111711
end
@@ -1731,6 +1731,17 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
17311731
end
17321732
private :parse_in_order
17331733

1734+
# Calls callback with _val_.
1735+
def callback!(cb, max_arity, *args) # :nodoc:
1736+
if (size = args.size) < max_arity and cb.to_proc.lambda?
1737+
(arity = cb.arity) < 0 and arity = (1-arity)
1738+
arity = max_arity if arity > max_arity
1739+
args[arity - 1] = nil if arity > size
1740+
end
1741+
cb.call(*args)
1742+
end
1743+
private :callback!
1744+
17341745
#
17351746
# Parses command line arguments +argv+ in permutation mode and returns
17361747
# list of non-option arguments. When optional +into+ keyword

test/optparse/test_optarg.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setup
1010
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
1111
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
1212
@opt.def_option("--fallback[=VAL]") do |x = "fallback"| @flag = x end
13+
@opt.def_option("--lambda[=VAL]", &->(x) {@flag = x})
1314
@reopt = nil
1415
end
1516

@@ -65,4 +66,11 @@ def test_default_argument
6566
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
6667
assert_equal("fallback", @flag)
6768
end
69+
70+
def test_lambda
71+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
72+
assert_equal("lambda1", @flag)
73+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
74+
assert_equal(nil, @flag)
75+
end
6876
end

test/optparse/test_placearg.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def setup
1414
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
1515
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
1616
@opt.def_option("--fallback [VAL]") do |x = "fallback"| @flag = x end
17+
@opt.def_option("--lambda [VAL]", &->(x) {@flag = x})
1718
end
1819

1920
def test_short
@@ -83,4 +84,13 @@ def test_default_argument
8384
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
8485
assert_equal("fallback", @flag)
8586
end
87+
88+
def test_lambda
89+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
90+
assert_equal("lambda1", @flag)
91+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda lambda2")})
92+
assert_equal("lambda2", @flag)
93+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
94+
assert_equal(nil, @flag)
95+
end
8696
end

test/optparse/test_reqarg.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def setup
66
super
77
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
88
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
9+
@opt.def_option("--lambda=VAL", &->(x) {@flag = x})
910
end
1011

1112
class Def1 < TestOptionParser
@@ -81,6 +82,11 @@ def test_hyphenize
8182
assert_equal("foo4", @flag)
8283
end
8384

85+
def test_lambda
86+
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
87+
assert_equal("lambda1", @flag)
88+
end
89+
8490
class TestOptionParser::WithPattern < TestOptionParser
8591
def test_pattern
8692
pat = num = nil

0 commit comments

Comments
 (0)