Skip to content

Commit c5ec052

Browse files
committed
Allow non-string enum list #79
Command line arguments are strings, convert enum list elements to strings to match.
1 parent 0a0e977 commit c5ec052

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/optparse.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ def make_switch(opts, block = nil)
15031503
else
15041504
raise ArgumentError, "argument pattern given twice"
15051505
end
1506-
o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
1506+
o.each {|pat, *v| pattern[pat.to_s] = v.fetch(0) {pat}}
15071507
when Module
15081508
raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
15091509
when *ArgumentStyle.keys

test/optparse/test_placearg.rb

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def setup
88
@opt.def_option("--option [VAL]") {|x| @flag = x}
99
@opt.def_option("-T [level]", /^[0-4]$/, Integer) {|x| @topt = x}
1010
@opt.def_option("--enum [VAL]", [:Alpha, :Bravo, :Charlie]) {|x| @enum = x}
11+
@opt.def_option("--integer [VAL]", [1, 2, 3]) {|x| @integer = x}
1112
@topt = nil
1213
@opt.def_option("-n") {}
1314
@opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x}
@@ -99,4 +100,9 @@ def test_enum
99100
assert_equal([], no_error {@opt.parse!(%w"--enum=A")})
100101
assert_equal(:Alpha, @enum)
101102
end
103+
104+
def test_enum_conversion
105+
assert_equal([], no_error {@opt.parse!(%w"--integer=1")})
106+
assert_equal(1, @integer)
107+
end
102108
end

0 commit comments

Comments
 (0)