Bug #8865 » optparse.rb.accept.patch
lib/optparse.rb (working copy) | ||
---|---|---|
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}
|
||
#
|
||
... | ... | |
#
|
||
# Decimal integer format, to be converted to Integer.
|
||
#
|
||
DecimalInteger = /\A[-+]?#{decimal}/io
|
||
accept(DecimalInteger) {|s,| s.to_i if s}
|
||
DecimalInteger = /\A([-+]?#{decimal})/io
|
||
accept(DecimalInteger, DecimalInteger) {|s,d|
|
||
begin
|
||
Integer(d)
|
||
rescue ArgumentError
|
||
raise OptionParser::InvalidArgument, s
|
||
end
|
||
}
|
||
#
|
||
# 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}
|
||
accept(OctalInteger, OctalInteger) {|s,| s.oct 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
|