Skip to content

Commit 9d53e74

Browse files
committed
Respect default values in block parameters
Fix #55
1 parent 4e346ad commit 9d53e74

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

lib/optparse.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,11 @@ def pretty_print(q) # :nodoc:
697697
q.object_group(self) {pretty_print_contents(q)}
698698
end
699699

700+
def omitted_argument(val) # :nodoc:
701+
val.pop if val.size == 3 and val.last.nil?
702+
val
703+
end
704+
700705
#
701706
# Switch that takes no arguments.
702707
#
@@ -755,7 +760,7 @@ def parse(arg, argv, &error)
755760
if arg
756761
conv_arg(*parse_arg(arg, &error))
757762
else
758-
conv_arg(arg)
763+
omitted_argument conv_arg(arg)
759764
end
760765
end
761766

@@ -774,13 +779,14 @@ class PlacedArgument < self
774779
#
775780
def parse(arg, argv, &error)
776781
if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
777-
return nil, block, nil
782+
return nil, block
778783
end
779784
opt = (val = parse_arg(val, &error))[1]
780785
val = conv_arg(*val)
781786
if opt and !arg
782787
argv.shift
783788
else
789+
omitted_argument val
784790
val[0] = nil
785791
end
786792
val
@@ -1633,7 +1639,7 @@ def order(*argv, into: nil, &nonopt)
16331639
# Non-option arguments remain in +argv+.
16341640
#
16351641
def order!(argv = default_argv, into: nil, &nonopt)
1636-
setter = ->(name, val) {into[name.to_sym] = val} if into
1642+
setter = ->(name, val = nil) {into[name.to_sym] = val} if into
16371643
parse_in_order(argv, setter, &nonopt)
16381644
end
16391645

@@ -1658,9 +1664,9 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
16581664
raise $!.set_option(arg, true)
16591665
end
16601666
begin
1661-
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
1662-
val = cb.call(val) if cb
1663-
setter.call(sw.switch_name, val) if setter
1667+
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
16641670
rescue ParseError
16651671
raise $!.set_option(arg, rest)
16661672
end
@@ -1690,16 +1696,16 @@ def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
16901696
raise $!.set_option(arg, true)
16911697
end
16921698
begin
1693-
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
1699+
opt, cb, *val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
16941700
rescue ParseError
16951701
raise $!.set_option(arg, arg.length > 2)
16961702
else
16971703
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
16981704
end
16991705
begin
17001706
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
1701-
val = cb.call(val) if cb
1702-
setter.call(sw.switch_name, val) if setter
1707+
val = cb.call(*val) if cb
1708+
setter.call(sw.switch_name, *val) if setter
17031709
rescue ParseError
17041710
raise $!.set_option(arg, arg.length > 2)
17051711
end

test/optparse/test_acceptable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def setup
88
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
99
@opt.def_option("--float VAL", Float) { |v| @float = v }
1010
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
11+
@opt.def_option("--array VAL", Array) { |v| @array = v }
1112

1213
@opt.def_option("--decimal-integer VAL",
1314
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
@@ -195,4 +196,8 @@ def test_decimal_numeric
195196
end
196197
end
197198

199+
def test_array
200+
assert_equal(%w"", no_error {@opt.parse!(%w"--array a,b,c")})
201+
assert_equal(%w"a b c", @array)
202+
end
198203
end

test/optparse/test_optarg.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def setup
99
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
1010
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
1111
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
12+
@opt.def_option("--fallback[=VAL]") do |x = "fallback"| @flag = x end
1213
@reopt = nil
1314
end
1415

@@ -57,4 +58,11 @@ def test_hyphenize
5758
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
5859
assert_equal("foo4", @flag)
5960
end
61+
62+
def test_default_argument
63+
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
64+
assert_equal("val1", @flag)
65+
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
66+
assert_equal("fallback", @flag)
67+
end
6068
end

test/optparse/test_optparse.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ def test_into
7373
@opt.def_option "-p", "--port=PORT", "port", Integer
7474
@opt.def_option "-v", "--verbose" do @verbose = true end
7575
@opt.def_option "-q", "--quiet" do @quiet = true end
76+
@opt.def_option "-o", "--option [OPT]" do |opt| @option = opt end
7677
result = {}
7778
@opt.parse %w(--host localhost --port 8000 -v), into: result
7879
assert_equal({host: "localhost", port: 8000, verbose: true}, result)
7980
assert_equal(true, @verbose)
81+
result = {}
82+
@opt.parse %w(--option -q), into: result
83+
assert_equal({quiet: true, option: nil}, result)
84+
result = {}
85+
@opt.parse %w(--option OPTION -v), into: result
86+
assert_equal({verbose: true, option: "OPTION"}, result)
8087
end
8188

8289
def test_require_exact

test/optparse/test_placearg.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def setup
1313
@reopt = nil
1414
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
1515
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
16+
@opt.def_option("--fallback [VAL]") do |x = "fallback"| @flag = x end
1617
end
1718

1819
def test_short
@@ -73,4 +74,13 @@ def test_conv
7374
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
7475
assert_equal(1, @topt)
7576
end
77+
78+
def test_default_argument
79+
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback=val1")})
80+
assert_equal("val1", @flag)
81+
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback val2")})
82+
assert_equal("val2", @flag)
83+
assert_equal(%w"", no_error {@opt.parse!(%w"--fallback")})
84+
assert_equal("fallback", @flag)
85+
end
7686
end

0 commit comments

Comments
 (0)