optparse.rb: get rid of eval
* lib/optparse.rb: try Float() and Integer() instead of eval, which does too much things. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59312 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
561c253512
commit
89bf90a370
@ -1842,7 +1842,7 @@ XXX
|
|||||||
#
|
#
|
||||||
# Float number format, and converts to Float.
|
# 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
|
floatpat = %r"\A[-+]?#{float}\z"io
|
||||||
accept(Float, floatpat) {|s,| s.to_f if s}
|
accept(Float, floatpat) {|s,| s.to_f if s}
|
||||||
|
|
||||||
@ -1851,11 +1851,13 @@ XXX
|
|||||||
# for float format, and Rational for rational format.
|
# for float format, and Rational for rational format.
|
||||||
#
|
#
|
||||||
real = "[-+]?(?:#{octal}|#{float})"
|
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
|
if n
|
||||||
Rational(d, n)
|
Rational(d, n)
|
||||||
elsif s
|
elsif f
|
||||||
eval(s)
|
Float(s)
|
||||||
|
else
|
||||||
|
Integer(s)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1889,10 +1891,14 @@ XXX
|
|||||||
# integer format, Float for float format.
|
# integer format, Float for float format.
|
||||||
#
|
#
|
||||||
DecimalNumeric = floatpat # decimal integer is allowed as float also.
|
DecimalNumeric = floatpat # decimal integer is allowed as float also.
|
||||||
accept(DecimalNumeric, floatpat) {|s,|
|
accept(DecimalNumeric, floatpat) {|s, f|
|
||||||
begin
|
begin
|
||||||
eval(s)
|
if f
|
||||||
rescue SyntaxError
|
Float(s)
|
||||||
|
else
|
||||||
|
Integer(s)
|
||||||
|
end
|
||||||
|
rescue ArgumentError
|
||||||
raise OptionParser::InvalidArgument, s
|
raise OptionParser::InvalidArgument, s
|
||||||
end if s
|
end if s
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,9 @@ class TestOptionParser::Acceptable < TestOptionParser
|
|||||||
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")})
|
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")})
|
||||||
assert_equal(Rational(1, 2), @numeric)
|
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(%w"", no_error {@opt.parse!(%w"--numeric 1.2/2.3")})
|
||||||
assert_equal(Rational(12, 23), @numeric)
|
assert_equal(Rational(12, 23), @numeric)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user