opt 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +1 -1
- data/lib/opt/option.rb +17 -54
- data/lib/opt/version.rb +11 -1
- data/lib/opt.rb +7 -0
- data/spec/opt/option_spec.rb +5 -23
- data/spec/opt_spec.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDY2ZTNiM2Q0MjhkYTMyMWU3MDA1MzFjY2M2YjhmNTIzNWY3OGQ4OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzVmYjdmN2MwMDUxMGRmZTIyNzU4NGExOWIxM2E3NzI0ZGJjZGEwMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDUzY2FhOWQ3ZTkzODM0ZDcyNGJlNDQyMjZkZjBmYTNlN2Y5NWU1MjA4Nzky
|
10
|
+
ODgzM2Y0N2UxMWNlY2YzMjBlNTM0YjQ4ZmUzODYzMmY0NjFjYzM5MDEzYjQ3
|
11
|
+
Y2M1MDBhNmFhNjU1YjViNTgzODgxY2I4YjlhM2M4MTgwYjE4MmM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTA3ZjNkMzE1NTg4NGQxMGQ0ZGQ3MzA0YzkxMjAzODIxZjRhY2Q1MzE2Yjk0
|
14
|
+
MTIwOTNjZWIxODY1Y2FhYmZmZjg2MDBhNzI3NGYwM2IyZDE4NmRlMGQyYjlj
|
15
|
+
Mzc4NTU3ODJiNzEwMmUwMmY3ZTFiNmNjZmVmZDRlZjA2YWIzNmQ=
|
data/README.md
CHANGED
@@ -36,7 +36,7 @@ You can also specify subcommands, the number of arguments for a switch or a text
|
|
36
36
|
opt = Opt.new
|
37
37
|
opt.command 'merge' do |cmd|
|
38
38
|
cmd.option '--out, -O', nargs: 1
|
39
|
-
cmd.option 'file', name: :files, nargs:
|
39
|
+
cmd.option 'file', name: :files, nargs: 0..Opt::Infinity
|
40
40
|
end
|
41
41
|
|
42
42
|
result = opt.parse %w(merge --out out.txt file1.txt file2.txt)
|
data/lib/opt/option.rb
CHANGED
@@ -57,7 +57,7 @@ module Opt
|
|
57
57
|
@switches = Set.new
|
58
58
|
@name = options.fetch(:name, definition).to_s.freeze
|
59
59
|
|
60
|
-
unless nargs.
|
60
|
+
unless nargs.min > 0 || nargs.max > 0
|
61
61
|
raise 'A text option must consist of at least one argument.'
|
62
62
|
end
|
63
63
|
else
|
@@ -115,7 +115,7 @@ module Opt
|
|
115
115
|
else
|
116
116
|
args = []
|
117
117
|
if argv.any? && argv.first.text?
|
118
|
-
while argv.any? && argv.first.text? && args.size < nargs.
|
118
|
+
while argv.any? && argv.first.text? && args.size < nargs.max
|
119
119
|
args << argv.shift.value
|
120
120
|
end
|
121
121
|
elsif argv.any? && argv.first.short?
|
@@ -139,59 +139,22 @@ module Opt
|
|
139
139
|
def parse_nargs(num)
|
140
140
|
case num
|
141
141
|
when Range
|
142
|
-
|
143
|
-
|
144
|
-
|
142
|
+
if num.min && num.max
|
143
|
+
if num.min >= 0
|
144
|
+
num
|
145
|
+
else
|
146
|
+
raise ArgumentError.new \
|
147
|
+
'Argument number must not be less than zero.'
|
148
|
+
end
|
149
|
+
else
|
150
|
+
raise ArgumentError.new \
|
151
|
+
'Range must be ordered.'
|
152
|
+
end
|
153
|
+
when Numeric
|
154
|
+
parse_nargs num..num
|
145
155
|
else
|
146
|
-
|
147
|
-
|
148
|
-
end
|
149
|
-
|
150
|
-
def parse_nargs_obj(obj)
|
151
|
-
case obj.to_s.downcase
|
152
|
-
when '+'
|
153
|
-
1..Float::INFINITY
|
154
|
-
when '*', 'inf', 'infinity'
|
155
|
-
0..Float::INFINITY
|
156
|
-
else
|
157
|
-
i = Integer(obj.to_s)
|
158
|
-
parse_nargs_range i..i
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def parse_nargs_range(range)
|
163
|
-
if range.first > range.last
|
164
|
-
if range.exclude_end?
|
165
|
-
range = Range.new(range.last + 1, range.first)
|
166
|
-
else
|
167
|
-
range = Range.new(range.last, range.first)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
if range.first < 0
|
172
|
-
raise RuntimeError.new 'Argument number must not be less than zero.'
|
173
|
-
end
|
174
|
-
|
175
|
-
range
|
176
|
-
end
|
177
|
-
|
178
|
-
def parse_nargs_array(obj)
|
179
|
-
if obj.size == 2
|
180
|
-
parse_nargs_range Range.new(parse_nargs_array_obj(obj[0]),
|
181
|
-
parse_nargs_array_obj(obj[1]))
|
182
|
-
else
|
183
|
-
|
184
|
-
raise ArgumentError.new \
|
185
|
-
'Argument number array count must be exactly two.'
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def parse_nargs_array_obj(obj)
|
190
|
-
case obj.to_s.downcase
|
191
|
-
when '*', 'inf', 'infinity'
|
192
|
-
Float::INFINITY
|
193
|
-
else
|
194
|
-
Integer(obj.to_s)
|
156
|
+
i = Integer(num.to_s)
|
157
|
+
parse_nargs i..i
|
195
158
|
end
|
196
159
|
end
|
197
160
|
end
|
data/lib/opt/version.rb
CHANGED
data/lib/opt.rb
CHANGED
data/spec/opt/option_spec.rb
CHANGED
@@ -44,41 +44,23 @@ describe Opt::Option do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'should parse range' do
|
47
|
-
expect(described_class.parse_nargs(2..
|
47
|
+
expect(described_class.parse_nargs(2..Opt::Inf)).to eq 2..Float::INFINITY
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'should parse fixnum string' do
|
51
51
|
expect(described_class.parse_nargs('12')).to eq 12..12
|
52
52
|
end
|
53
53
|
|
54
|
-
it 'should parse special infinity string' do
|
55
|
-
expect(described_class.parse_nargs('*')).to eq 0..Float::INFINITY
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'should parse special infinity symbol (I)' do
|
59
|
-
expect(described_class.parse_nargs(:inf)).to eq 0..Float::INFINITY
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'should parse special infinity symbol (II)' do
|
63
|
-
expect(described_class.parse_nargs(:infinity)).to eq 0..Float::INFINITY
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should parse array (I)' do
|
67
|
-
expect(described_class.parse_nargs([3, :inf])).to eq 3..Float::INFINITY
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should parse array (II)' do
|
71
|
-
expect(described_class.parse_nargs([3, 100])).to eq 3..100
|
72
|
-
end
|
73
|
-
|
74
54
|
it 'should reject invalid string' do
|
75
55
|
expect do
|
76
56
|
described_class.parse_nargs('a12')
|
77
57
|
end.to raise_error(/invalid value for Integer/)
|
78
58
|
end
|
79
59
|
|
80
|
-
it 'should
|
81
|
-
expect
|
60
|
+
it 'should reject invalid range' do
|
61
|
+
expect do
|
62
|
+
described_class.parse_nargs(6..2)
|
63
|
+
end.to raise_error(/Range must be ordered./)
|
82
64
|
end
|
83
65
|
|
84
66
|
it 'should reject negative range' do
|
data/spec/opt_spec.rb
CHANGED