Skip to content

Commit

Permalink
Adjust arguments for lambda-callbacks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nobu committed Feb 9, 2024
1 parent 9d53e74 commit 213cb03
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ def order(*argv, into: nil, &nonopt)
# Non-option arguments remain in +argv+.
#
def order!(argv = default_argv, into: nil, &nonopt)
setter = ->(name, val = nil) {into[name.to_sym] = val} if into
setter = ->(name, val) {into[name.to_sym] = val} if into
parse_in_order(argv, setter, &nonopt)
end

Expand All @@ -1665,8 +1665,8 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
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
val = callback!(cb, 1, *val) if cb
callback!(setter, 2, sw.switch_name, *val) if setter
rescue ParseError
raise $!.set_option(arg, rest)
end
Expand Down Expand Up @@ -1704,8 +1704,8 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
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 = 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
Expand All @@ -1731,6 +1731,17 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
end
private :parse_in_order

# Calls callback with _val_.
def callback!(cb, max_arity, *args) # :nodoc:
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
args[arity - 1] = nil if arity > size
end
cb.call(*args)
end
private :callback!

#
# Parses command line arguments +argv+ in permutation mode and returns
# list of non-option arguments. When optional +into+ keyword
Expand Down
8 changes: 8 additions & 0 deletions test/optparse/test_optarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setup
@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
@opt.def_option("--lambda[=VAL]", &->(x) {@flag = x})
@reopt = nil
end

Expand Down Expand Up @@ -65,4 +66,11 @@ def test_default_argument
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
assert_equal("fallback", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
assert_equal(nil, @flag)
end
end
10 changes: 10 additions & 0 deletions test/optparse/test_placearg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def setup
@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
@opt.def_option("--lambda [VAL]", &->(x) {@flag = x})
end

def test_short
Expand Down Expand Up @@ -83,4 +84,13 @@ def test_default_argument
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
assert_equal("fallback", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda lambda2")})
assert_equal("lambda2", @flag)
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda")})
assert_equal(nil, @flag)
end
end
6 changes: 6 additions & 0 deletions test/optparse/test_reqarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def setup
super
@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("--lambda=VAL", &->(x) {@flag = x})
end

class Def1 < TestOptionParser
Expand Down Expand Up @@ -81,6 +82,11 @@ def test_hyphenize
assert_equal("foo4", @flag)
end

def test_lambda
assert_equal(%w"", no_error {@opt.parse!(%w"--lambda=lambda1")})
assert_equal("lambda1", @flag)
end

class TestOptionParser::WithPattern < TestOptionParser
def test_pattern
pat = num = nil
Expand Down

0 comments on commit 213cb03

Please sign in to comment.