Project

General

Profile

Bug #8865 ยป optparse.rb.accept.2.patch

drbrain (Eric Hodel), 09/05/2013 09:20 AM

View differences:

lib/optparse.rb (working copy)
1637 1637
  decimal = '\d+(?:_\d+)*'
1638 1638
  binary = 'b[01]+(?:_[01]+)*'
1639 1639
  hex = 'x[\da-f]+(?:_[\da-f]+)*'
1640
  octal = "0(?:[0-7]*(?:_[0-7]+)*|#{binary}|#{hex})"
1640
  octal = "0(?:[0-7]+(?:_[0-7]+)*|#{binary}|#{hex})?"
1641 1641
  integer = "#{octal}|#{decimal}"
1642

  
1642 1643
  accept(Integer, %r"\A[-+]?(?:#{integer})"io) {|s,| Integer(s) if s}
1643 1644

  
1644 1645
  #
......
1664 1665
  #
1665 1666
  # Decimal integer format, to be converted to Integer.
1666 1667
  #
1667
  DecimalInteger = /\A[-+]?#{decimal}/io
1668
  accept(DecimalInteger) {|s,| s.to_i if s}
1668
  DecimalInteger = /\A([-+]?#{decimal})/io
1669
  accept(DecimalInteger, DecimalInteger) {|s,d|
1670
    begin
1671
      Integer(d)
1672
    rescue ArgumentError
1673
      raise OptionParser::InvalidArgument, s
1674
    end
1675
  }
1669 1676

  
1670 1677
  #
1671 1678
  # Ruby/C like octal/hexadecimal/binary integer format, to be converted to
1672 1679
  # Integer.
1673 1680
  #
1674 1681
  OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))/io
1675
  accept(OctalInteger) {|s,| s.oct if s}
1682
  accept(OctalInteger, OctalInteger) {|s,| s.oct if s}
1676 1683

  
1677 1684
  #
1678 1685
  # Decimal integer/float number format, to be converted to Integer for
1679 1686
  # integer format, Float for float format.
1680 1687
  #
1681 1688
  DecimalNumeric = floatpat     # decimal integer is allowed as float also.
1682
  accept(DecimalNumeric) {|s,| eval(s) if s}
1689
  accept(DecimalNumeric, floatpat) {|s,|
1690
    begin
1691
      eval(s)
1692
    rescue SyntaxError
1693
      raise OptionParser::InvalidArgument, s
1694
    end if s
1695
  }
1683 1696

  
1684 1697
  #
1685 1698
  # Boolean switch, which means whether it is present or not, whether it is
test/optparse/test_acceptable.rb (working copy)
1
require_relative 'test_optparse'
2

  
3
class TestOptionParser::Acceptable < TestOptionParser
4

  
5
  def setup
6
    super
7
    @opt.def_option("--integer VAL", Integer) { |v| @integer = v }
8
    @opt.def_option("--float VAL",   Float)   { |v| @float   = v }
9
    @opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
10

  
11
    @opt.def_option("--decimal-integer VAL",
12
                    OptionParser::DecimalInteger) { |i| @decimal_integer = i }
13
    @opt.def_option("--octal-integer VAL",
14
                    OptionParser::OctalInteger)   { |i| @octal_integer   = i }
15
    @opt.def_option("--decimal-numeric VAL",
16
                    OptionParser::DecimalNumeric) { |i| @decimal_numeric = i }
17
  end
18

  
19
  def test_integer
20
    assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0")})
21
    assert_equal(0, @integer)
22

  
23
    assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0b10")})
24
    assert_equal(2, @integer)
25

  
26
    assert_equal(%w"", no_error {@opt.parse!(%w"--integer 077")})
27
    assert_equal(63, @integer)
28

  
29
    assert_equal(%w"", no_error {@opt.parse!(%w"--integer 10")})
30
    assert_equal(10, @integer)
31

  
32
    assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0x3")})
33
    assert_equal(3, @integer)
34

  
35
    assert_raises(OptionParser::InvalidArgument) do
36
      @opt.parse!(%w"--integer 0b")
37
    end
38

  
39
    assert_raises(OptionParser::InvalidArgument) do
40
      @opt.parse!(%w"--integer 09")
41
    end
42

  
43
    assert_raises(OptionParser::InvalidArgument) do
44
      @opt.parse!(%w"--integer 0x")
45
    end
46
  end
47

  
48
  def test_float
49
    assert_equal(%w"", no_error {@opt.parse!(%w"--float 0")})
50
    assert_in_epsilon(0.0, @float)
51

  
52
    assert_equal(%w"", no_error {@opt.parse!(%w"--float 0.0")})
53
    assert_in_epsilon(0.0, @float)
54

  
55
    assert_equal(%w"", no_error {@opt.parse!(%w"--float 1.2")})
56
    assert_in_epsilon(1.2, @float)
57

  
58
    assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E2")})
59
    assert_in_epsilon(100, @float)
60

  
61
    assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E-2")})
62
    assert_in_epsilon(0.01, @float)
63

  
64
    assert_raises(OptionParser::InvalidArgument) do
65
      @opt.parse!(%w"--float 0e")
66
    end
67
  end
68

  
69
  def test_numeric
70
    assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0")})
71
    assert_equal(0, @numeric)
72

  
73
    assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0/1")})
74
    assert_equal(0, @numeric)
75

  
76
    assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")})
77
    assert_equal(Rational(1, 2), @numeric)
78

  
79
    assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1.2/2.3")})
80
    assert_equal(Rational(12, 23), @numeric)
81

  
82
    assert_raises(OptionParser::InvalidArgument) do
83
      @opt.parse!(%w"--numeric 1/")
84
    end
85
  end
86

  
87
  def test_decimal_integer
88
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 0")})
89
    assert_equal(0, @decimal_integer)
90

  
91
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 10")})
92
    assert_equal(10, @decimal_integer)
93

  
94
    assert_raises(OptionParser::InvalidArgument) do
95
      @opt.parse!(%w"--decimal-integer 0b1")
96
    end
97

  
98
    e = assert_raises(OptionParser::InvalidArgument) do
99
      @opt.parse!(%w"--decimal-integer 09")
100
    end
101

  
102
    assert_equal("invalid argument: --decimal-integer 09", e.message)
103

  
104
    assert_raises(OptionParser::InvalidArgument) do
105
      @opt.parse!(%w"--decimal-integer x")
106
    end
107
  end
108

  
109
  def test_octal_integer
110
    assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 0")})
111
    assert_equal(0, @octal_integer)
112

  
113
    assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 6")})
114
    assert_equal(6, @octal_integer)
115

  
116
    assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 07")})
117
    assert_equal(7, @octal_integer)
118

  
119
    assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 10")})
120
    assert_equal(8, @octal_integer)
121

  
122
    assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 011")})
123
    assert_equal(9, @octal_integer)
124

  
125
    assert_raises(OptionParser::InvalidArgument) do
126
      @opt.parse!(%w"--octal-integer 09")
127
    end
128

  
129
    assert_raises(OptionParser::InvalidArgument) do
130
      @opt.parse!(%w"--octal-integer 0b1")
131
    end
132

  
133
    assert_raises(OptionParser::InvalidArgument) do
134
      @opt.parse!(%w"--octal-integer x")
135
    end
136
  end
137

  
138
  def test_decimal_numeric
139
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 0")})
140
    assert_equal(0, @decimal_numeric)
141

  
142
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 01")})
143
    assert_equal(1, @decimal_numeric)
144

  
145
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1.2")})
146
    assert_in_delta(1.2, @decimal_numeric)
147

  
148
    assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1E2")})
149
    assert_in_delta(100.0, @decimal_numeric)
150

  
151
    assert_raises(OptionParser::InvalidArgument) do
152
      @opt.parse!(%w"--decimal-numeric 0b1")
153
    end
154

  
155
    e = assert_raises(OptionParser::InvalidArgument) do
156
      @opt.parse!(%w"--decimal-numeric 09")
157
    end
158

  
159
    assert_equal("invalid argument: --decimal-numeric 09", e.message)
160
  end
161

  
162
end
163