summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/optparse.rb20
-rw-r--r--test/optparse/test_acceptable.rb3
2 files changed, 16 insertions, 7 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index b4c44545bb..b99b0a41cd 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1842,7 +1842,7 @@ XXX
#
# Float number format, and converts to Float.
#
- float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
+ float = "(?:#{decimal}(?=(.)?)(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
floatpat = %r"\A[-+]?#{float}\z"io
accept(Float, floatpat) {|s,| s.to_f if s}
@@ -1851,11 +1851,13 @@ XXX
# for float format, and Rational for rational format.
#
real = "[-+]?(?:#{octal}|#{float})"
- accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n|
+ accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, f, n,|
if n
Rational(d, n)
- elsif s
- eval(s)
+ elsif f
+ Float(s)
+ else
+ Integer(s)
end
}
@@ -1889,10 +1891,14 @@ XXX
# integer format, Float for float format.
#
DecimalNumeric = floatpat # decimal integer is allowed as float also.
- accept(DecimalNumeric, floatpat) {|s,|
+ accept(DecimalNumeric, floatpat) {|s, f|
begin
- eval(s)
- rescue SyntaxError
+ if f
+ Float(s)
+ else
+ Integer(s)
+ end
+ rescue ArgumentError
raise OptionParser::InvalidArgument, s
end if s
}
diff --git a/test/optparse/test_acceptable.rb b/test/optparse/test_acceptable.rb
index dd38f8ec7a..5c3fbdb10c 100644
--- a/test/optparse/test_acceptable.rb
+++ b/test/optparse/test_acceptable.rb
@@ -85,6 +85,9 @@ class TestOptionParser::Acceptable < TestOptionParser
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")})
assert_equal(Rational(1, 2), @numeric)
+ assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 010")})
+ assert_equal(8, @numeric)
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1.2/2.3")})
assert_equal(Rational(12, 23), @numeric)