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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- M2JlZjg3YjFkYzA5N2QzNjYwNWZmMWQ4ODkwZTc5NjRlZmZhOWNkYw==
4
+ NDY2ZTNiM2Q0MjhkYTMyMWU3MDA1MzFjY2M2YjhmNTIzNWY3OGQ4OA==
5
5
  data.tar.gz: !binary |-
6
- MjE4NmNlNDMzYzhkNTQ5NDUxYWExNDU1ZGNkYTc4MmFkOGNlZmEwNA==
6
+ YzVmYjdmN2MwMDUxMGRmZTIyNzU4NGExOWIxM2E3NzI0ZGJjZGEwMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGI2ZmRjODA3NjU5MmUxYzc1MTMwZTRlZGM4ZmY3YWZmN2YyMDFhNjA2ZmQ3
10
- NTlhZTEwYTJiODM1NGJlN2VjMGQ3NTJmNzAxMjk0NjI2NmJmZmI4Zjg1NDdm
11
- MjU3N2U0ODdjZDQxMTRiZmQzNGZiMmEyM2M1MTFmNmY2ODgxOGI=
9
+ ZDUzY2FhOWQ3ZTkzODM0ZDcyNGJlNDQyMjZkZjBmYTNlN2Y5NWU1MjA4Nzky
10
+ ODgzM2Y0N2UxMWNlY2YzMjBlNTM0YjQ4ZmUzODYzMmY0NjFjYzM5MDEzYjQ3
11
+ Y2M1MDBhNmFhNjU1YjViNTgzODgxY2I4YjlhM2M4MTgwYjE4MmM=
12
12
  data.tar.gz: !binary |-
13
- Y2YzYmVkMzM1MWU4NzMwMDY2N2RiNjYzZmEzOGMyZGNhNDA0ZDQzYTNkY2Rj
14
- MWRhNWNjZGIyOTI5NzJmYmExNzVmMzY4MThhZDk2OGJhNTA3OTMwZGZkZjRl
15
- NzlmMmI1MWJhMDYyZTc1MzRhMWQ2MmYyNzE4NDI5ZWEwNjIwYmQ=
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.first > 0 || nargs.last > 0
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.last
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
- parse_nargs_range(num)
143
- when Array
144
- parse_nargs_array(num)
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
- parse_nargs_obj(num)
147
- end
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
@@ -1,3 +1,13 @@
1
1
  module Opt
2
- VERSION = "0.1.1"
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ PATCH = 0
6
+ STAGE = nil
7
+ STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
8
+
9
+ def self.to_s
10
+ STRING
11
+ end
12
+ end
3
13
  end
data/lib/opt.rb CHANGED
@@ -9,6 +9,13 @@ module Opt
9
9
  def self.new
10
10
  Program.new.tap{|p| yield p if block_given? }
11
11
  end
12
+
13
+ module Constants
14
+ Inf = 1.0 / 0.0
15
+ Infinity = 1.0 / 0.0
16
+ end
17
+
18
+ include Constants
12
19
  end
13
20
 
14
21
  # #
@@ -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..6)).to eq 2..6
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 flip negative range' do
81
- expect(described_class.parse_nargs(6..2)).to eq 2..6
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
@@ -15,7 +15,7 @@ describe Opt do
15
15
 
16
16
  it 'should detect double-dash' do
17
17
  opt.option '-v', name: :version
18
- opt.option 'rest', nargs: '+'
18
+ opt.option 'rest', nargs: 1..Opt::Inf
19
19
 
20
20
  result = opt.parse %w(-v -- -h)
21
21
  expect(result.version?).to be true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen