diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-09-05 05:02:17 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-09-05 05:02:17 +0000 |
commit | ee155140e85e4e12ae54a246c43d0c200c8099be (patch) | |
tree | c65bc2fdb1bfc76bc26766e95fa3825302007f8e /lib | |
parent | 6fd900007616801f4c8adcc0228fe06bd6f1f873 (diff) | |
download | ruby-ee155140e85e4e12ae54a246c43d0c200c8099be.tar.gz |
* lib/optparse.rb: The Integer acceptable now allows binary and
hexadecimal numbers per the documentation. [ruby-trunk - Bug #8865]
DecimalInteger, OctalInteger, DecimalNumeric now validate their input
before converting to a number. [ruby-trunk - Bug #8865]
* test/optparse/test_acceptable.rb: Tests for the above, tests for all
numeric acceptables for existing behavior.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42844 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/optparse.rb | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb index ef07cc2fca..d9ff1df8cd 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1637,15 +1637,22 @@ XXX decimal = '\d+(?:_\d+)*' binary = 'b[01]+(?:_[01]+)*' hex = 'x[\da-f]+(?:_[\da-f]+)*' - octal = "0(?:[0-7]*(?:_[0-7]+)*|#{binary}|#{hex})" + octal = "0(?:[0-7]+(?:_[0-7]+)*|#{binary}|#{hex})?" integer = "#{octal}|#{decimal}" - accept(Integer, %r"\A[-+]?(?:#{integer})"io) {|s,| Integer(s) if s} + + accept(Integer, %r"\A[-+]?(?:#{integer})\z"io) {|s,| + begin + Integer(s) + rescue ArgumentError + raise OptionParser::InvalidArgument, s + end if s + } # # Float number format, and converts to Float. # float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?" - floatpat = %r"\A[-+]?#{float}"io + floatpat = %r"\A[-+]?#{float}\z"io accept(Float, floatpat) {|s,| s.to_f if s} # @@ -1653,7 +1660,7 @@ XXX # for float format, and Rational for rational format. # real = "[-+]?(?:#{octal}|#{float})" - accept(Numeric, /\A(#{real})(?:\/(#{real}))?/io) {|s, d, n| + accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n| if n Rational(d, n) elsif s @@ -1664,22 +1671,40 @@ XXX # # Decimal integer format, to be converted to Integer. # - DecimalInteger = /\A[-+]?#{decimal}/io - accept(DecimalInteger) {|s,| s.to_i if s} + DecimalInteger = /\A[-+]?#{decimal}\z/io + accept(DecimalInteger, DecimalInteger) {|s,| + begin + Integer(s) + rescue ArgumentError + raise OptionParser::InvalidArgument, s + end if s + } # # Ruby/C like octal/hexadecimal/binary integer format, to be converted to # Integer. # - OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))/io - accept(OctalInteger) {|s,| s.oct if s} + OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))\z/io + accept(OctalInteger, OctalInteger) {|s,| + begin + Integer(s, 8) + rescue ArgumentError + raise OptionParser::InvalidArgument, s + end if s + } # # Decimal integer/float number format, to be converted to Integer for # integer format, Float for float format. # DecimalNumeric = floatpat # decimal integer is allowed as float also. - accept(DecimalNumeric) {|s,| eval(s) if s} + accept(DecimalNumeric, floatpat) {|s,| + begin + eval(s) + rescue SyntaxError + raise OptionParser::InvalidArgument, s + end if s + } # # Boolean switch, which means whether it is present or not, whether it is |