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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy