opt_parse_validator 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da564ea023221e95e2ef0ee45258754314e7bcd8
4
- data.tar.gz: e23c41c855f934d09205ce95b5231d4e5a781568
3
+ metadata.gz: 9f71cf9bbecec6b8c33d14c3cab01c0af1112735
4
+ data.tar.gz: d033f1ef4649cd628d415657c2375d014616a3b2
5
5
  SHA512:
6
- metadata.gz: bb975e401d8c2b44c46d0c7b242a9f6050e37ea74fff3ec3759339f6ee2f65edbedf56e67198e0a16c7a4eed8b3f1dc9387042400eb69f4309d07119917617da
7
- data.tar.gz: 1b1bce2783b48eb104edf9e35a921cbed61c3ddbd527bcbf571ae915e1fa6c356668497ce8710def3cf9efbf3e157087f6d4928626f454e7af1e9f3d4e01c7f0
6
+ metadata.gz: 63b1f0f830f821e9639b5a6c239c20a472ba9fef16850fb7c37eabf54d589ca463bd30f087a7da906d9ee458a12a23f7162976a0d033a32c06aaec9531f9645a
7
+ data.tar.gz: 66311acd02e0b17a87d653e5b12609d94a5138e7a1a915e63a401c23004e9c00499c236cf376bf33603f3135f15c38f4e40af229dcb0a4552f1131d3bed98aff
data/README.md CHANGED
@@ -26,6 +26,11 @@ OptParseValidator
26
26
  - :readable
27
27
  - :writable
28
28
  - Integer
29
+ - IntegerRange
30
+ - separator (default: '-')
31
+ - MultiChoices
32
+ - choices (mandatory)
33
+ - separator (default: ',')
29
34
  - Positive Integer
30
35
  - Path
31
36
  - :file
@@ -37,8 +42,6 @@ OptParseValidator
37
42
  - Proxy
38
43
  - :protocols
39
44
  - :default_protocol
40
- - Scope
41
- - separator (default: ',')
42
45
  - String
43
46
  - URI
44
47
  - :protocols
@@ -5,8 +5,7 @@ module OptParseValidator
5
5
  #
6
6
  # @return [ Array ]
7
7
  def validate(value)
8
- super(value)
9
- value.split(separator)
8
+ super(value).split(separator)
10
9
  end
11
10
 
12
11
  # @return [ String ] The separator used to split the string into an array
@@ -15,7 +14,7 @@ module OptParseValidator
15
14
  end
16
15
 
17
16
  # See OptBase#normalize
18
- # @param [ Array ] value
17
+ # @param [ Array ] values
19
18
  def normalize(values)
20
19
  values.each_with_index do |value, index|
21
20
  values[index] = super(value)
@@ -9,7 +9,7 @@ module OptParseValidator
9
9
  # @param [ Hash ] attrs
10
10
  # @option attrs [ Boolean ] :required
11
11
  # @option attrs [ Mixed ] :default The default value to use if the option is not supplied
12
- # @option attrs [ Boolean ] :to_sym If true, returns the symbol of the validated value
12
+ # @option attrs [ Mixed ] :value_if_empty The value to use if no arguments have been supplied
13
13
  # @option attrs [ Array<Symbol> ] :normalize See #normalize
14
14
  #
15
15
  # @note The :default and :normalize 'logics' are done in OptParseValidator::OptParser#add_option
@@ -20,12 +20,30 @@ module OptParseValidator
20
20
 
21
21
  # @return [ Boolean ]
22
22
  def required?
23
- @required || attrs[:required]
23
+ @required ||= attrs[:required]
24
+ end
25
+
26
+ # @return [ Mixed ]
27
+ def default
28
+ attrs[:default]
29
+ end
30
+
31
+ # @return [ Array<Mixed> ]
32
+ def choices
33
+ attrs[:choices]
34
+ end
35
+
36
+ # @return [ Mixed ]
37
+ def value_if_empty
38
+ attrs[:value_if_empty]
24
39
  end
25
40
 
26
41
  # @param [ String ] value
27
42
  def validate(value)
28
- fail 'Empty option value supplied' if value.nil? || value.to_s.empty?
43
+ if value.nil? || value.to_s.empty?
44
+ fail 'Empty option value supplied' if value_if_empty.nil?
45
+ return value_if_empty
46
+ end
29
47
  value
30
48
  end
31
49
 
@@ -16,8 +16,7 @@ module OptParseValidator
16
16
  # If :case_sensitive if false (or nil), the downcased value of the choice
17
17
  # will be returned
18
18
  def validate(value)
19
- value = value.to_s
20
- choices = attrs[:choices]
19
+ value = value.to_s
21
20
 
22
21
  unless attrs[:case_sensitive]
23
22
  value.downcase!
@@ -0,0 +1,21 @@
1
+ module OptParseValidator
2
+ # Implementation of the Integer Range Option
3
+ class OptIntegerRange < OptBase
4
+ # @param [ String ] value
5
+ #
6
+ # @return [ Range ]
7
+ def validate(value)
8
+ a = super(value).split(separator)
9
+
10
+ fail "Incorrect number of ranges found: #{a.size}, should be 2" unless a.size == 2
11
+ fail 'Argument is not a valid integer range' unless a.first.to_i.to_s == a.first && a.last.to_i.to_s == a.last
12
+
13
+ (a.first.to_i..a.last.to_i)
14
+ end
15
+
16
+ # @return [ String ]
17
+ def separator
18
+ attrs[:separator] || '-'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,52 @@
1
+ module OptParseValidator
2
+ # Implementation of the MultiChoices Option
3
+ class OptMultiChoices < OptArray
4
+ # @param [ Array ] option See OptBase#new
5
+ # @param [ Hash ] attrs
6
+ # @option attrs [ Hash ] :choices
7
+ # @options attrs [ String ] :separator See OptArray#new
8
+ def initialize(option, attrs = {})
9
+ fail 'The :choices attribute is mandatory' unless attrs.key?(:choices)
10
+ fail 'The :choices attribute must be a hash' unless attrs[:choices].is_a?(Hash)
11
+
12
+ super(option, attrs)
13
+ end
14
+
15
+ # @param [ String ] value
16
+ #
17
+ # @return [ Hash ]
18
+ def validate(value)
19
+ results = {}
20
+
21
+ super(value).each do |item|
22
+ opt = choices[item.to_sym]
23
+
24
+ if opt
25
+ opt_value = opt.value_if_empty.nil? ? true : opt.value_if_empty
26
+ else
27
+ opt, opt_value = value_from_pattern(item)
28
+ end
29
+
30
+ results[opt.to_sym] = opt.normalize(opt.validate(opt_value))
31
+ end
32
+
33
+ results
34
+ end
35
+
36
+ # @return [ Array ]
37
+ def value_from_pattern(item)
38
+ choices.each do |key, opt|
39
+ next unless item.match(/\A#{key.to_s}(.*)\z/)
40
+
41
+ return [opt, Regexp.last_match[1]]
42
+ end
43
+
44
+ fail "Unknown choice: #{item}"
45
+ end
46
+
47
+ # No normalization
48
+ def normalize(value)
49
+ value
50
+ end
51
+ end
52
+ end
@@ -1,6 +1,6 @@
1
1
  %w(
2
2
  base string integer positive_integer choice boolean uri url proxy credentials
3
- path file_path directory_path array
3
+ path file_path directory_path array integer_range multi_choices
4
4
  ).each do |opt|
5
5
  require 'opt_parse_validator/opts/' + opt
6
6
  end
@@ -1,4 +1,4 @@
1
1
  # Gem Version
2
2
  module OptParseValidator
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
@@ -40,7 +40,7 @@ module OptParseValidator
40
40
  @opts << opt
41
41
  @symbols_used << opt.to_sym
42
42
  # Set the default option value if it exists
43
- @results[opt.to_sym] = opt.attrs[:default] if opt.attrs.key?(:default)
43
+ @results[opt.to_sym] = opt.default unless opt.default.nil?
44
44
 
45
45
  on(*opt.option) do |arg|
46
46
  begin
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'rake', '~> 10.4'
27
27
  s.add_development_dependency 'rspec', '~> 3.2'
28
28
  s.add_development_dependency 'rspec-its', '~> 1.1'
29
- s.add_development_dependency 'bundler', '~> 1.7'
30
- s.add_development_dependency 'rubocop', '~> 0.28'
29
+ s.add_development_dependency 'bundler', '~> 1.6'
30
+ s.add_development_dependency 'rubocop', '~> 0.29'
31
31
  s.add_development_dependency 'simplecov', '~> 0.9'
32
32
  end
@@ -5,9 +5,23 @@ describe OptParseValidator::OptArray do
5
5
  let(:attrs) { {} }
6
6
 
7
7
  describe '#validate' do
8
- context 'when an empty value is given' do
9
- it 'raises an error' do
10
- expect { opt.validate('') }.to raise_error('Empty option value supplied')
8
+ context 'when an empty or nil value is given' do
9
+ context 'when no value_if_empty attribute' do
10
+ it 'raises an error' do
11
+ [nil, ''].each do |value|
12
+ expect { opt.validate(value) }.to raise_error('Empty option value supplied')
13
+ end
14
+ end
15
+ end
16
+
17
+ context 'when value_if_empty attribute' do
18
+ let(:attrs) { super().merge(value_if_empty: 'a,b') }
19
+
20
+ it 'returns the expected array' do
21
+ [nil, ''].each do |value|
22
+ expect(opt.validate(value)).to eql %w(a b)
23
+ end
24
+ end
11
25
  end
12
26
  end
13
27
 
@@ -6,7 +6,7 @@ describe OptParseValidator::OptBase do
6
6
  let(:attrs) { {} }
7
7
 
8
8
  describe '#to_long' do
9
- after { expect(described_class.new(@option).to_long).to eq @expected }
9
+ after { expect(described_class.new(@option).to_long).to eql @expected }
10
10
 
11
11
  context 'when not found' do
12
12
  it 'returns nil' do
@@ -28,7 +28,7 @@ describe OptParseValidator::OptBase do
28
28
  if @exception
29
29
  expect { described_class.new(@option).to_sym }.to raise_error(@exception)
30
30
  else
31
- expect(described_class.new(@option).to_sym).to eq(@expected)
31
+ expect(described_class.new(@option).to_sym).to eql(@expected)
32
32
  end
33
33
  end
34
34
 
@@ -112,7 +112,7 @@ describe OptParseValidator::OptBase do
112
112
  end
113
113
 
114
114
  describe '#normalize' do
115
- after { expect(opt.normalize(@value)).to eq @expected }
115
+ after { expect(opt.normalize(@value)).to eql @expected }
116
116
 
117
117
  context 'when no :normalize attribute' do
118
118
  it 'returns the value' do
@@ -150,18 +150,38 @@ describe OptParseValidator::OptBase do
150
150
  end
151
151
 
152
152
  describe '#validate' do
153
- context 'when an empty or nil value' do
154
- it 'raises an error' do
155
- [nil, ''].each do |value|
156
- expect { opt.validate(value) }
157
- .to raise_error('Empty option value supplied')
153
+ context 'when no value_if_empty attribute' do
154
+ context 'when an empty or nil value' do
155
+ it 'raises an error' do
156
+ [nil, ''].each do |value|
157
+ expect { opt.validate(value) }
158
+ .to raise_error('Empty option value supplied')
159
+ end
160
+ end
161
+ end
162
+
163
+ context 'when a valid value' do
164
+ it 'returns it' do
165
+ expect(opt.validate('testing')).to eql 'testing'
158
166
  end
159
167
  end
160
168
  end
161
169
 
162
- context 'when a valid value' do
163
- it 'returns it' do
164
- expect(opt.validate('testing')).to eq 'testing'
170
+ context 'when value_if_empty attribute' do
171
+ let(:attrs) { super().merge(value_if_empty: 'it works') }
172
+
173
+ context 'when nil or empty value' do
174
+ it 'returns the value from the value_if_empty attribute' do
175
+ [nil, ''].each do |value|
176
+ expect(opt.validate(value)).to eql attrs[:value_if_empty]
177
+ end
178
+ end
179
+ end
180
+
181
+ context 'when a valid value' do
182
+ it 'returns it' do
183
+ expect(opt.validate('tt')).to eql 'tt'
184
+ end
165
185
  end
166
186
  end
167
187
  end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe OptParseValidator::OptIntegerRange do
4
+ subject(:opt) { described_class.new(['--range RANGE'], attrs) }
5
+ let(:attrs) { {} }
6
+
7
+ describe '#validate' do
8
+ context 'when incorrect number of ranges given' do
9
+ it 'raises an error' do
10
+ expect { opt.validate('1-2-3') }.to raise_error('Incorrect number of ranges found: 3, should be 2')
11
+ end
12
+ end
13
+
14
+ context 'when not an integer range' do
15
+ it 'raises an error' do
16
+ expect { opt.validate('a-e') }.to raise_error('Argument is not a valid integer range')
17
+ end
18
+ end
19
+
20
+ context 'when a valid range' do
21
+ it 'returns the range' do
22
+ expect(opt.validate('1-5')).to eql((1..5))
23
+ end
24
+
25
+ context 'when another separator' do
26
+ let(:attrs) { super().merge(separator: ':') }
27
+
28
+ it 'returns the range' do
29
+ expect(opt.validate('0:10')).to eql((0..10))
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'when nil or "" supplied' do
35
+ context 'when no value_if_empty attribute' do
36
+ it 'raises an error' do
37
+ [nil, ''].each do |value|
38
+ expect { opt.validate(value) }.to raise_error 'Empty option value supplied'
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'when value_if_empty attribute' do
44
+ let(:attrs) { super().merge(value_if_empty: '0-2') }
45
+
46
+ it 'returns the value_if_empty value' do
47
+ [nil, ''].each do |value|
48
+ expect(opt.validate(value)).to eql((0..2))
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe OptParseValidator::OptMultiChoices do
4
+ subject(:opt) { described_class.new(['--enumerate [CHOICES]'], attrs) }
5
+ let(:attrs) do
6
+ {
7
+ choices: {
8
+ vp: OptParseValidator::OptBoolean.new(['--vulenrable-plugins']),
9
+ u: OptParseValidator::OptIntegerRange.new(['--users'], value_if_empty: '1-10')
10
+ }
11
+ }
12
+ end
13
+
14
+ describe '#new' do
15
+ context 'when no choices attribute' do
16
+ let(:attrs) { {} }
17
+
18
+ it 'raises an error' do
19
+ expect { opt }.to raise_error 'The :choices attribute is mandatory'
20
+ end
21
+ end
22
+
23
+ context 'when choices attribute' do
24
+ context 'when not a hash' do
25
+ let(:attrs) { { choices: 'invalid' } }
26
+
27
+ it 'raises an error' do
28
+ expect { opt }.to raise_error 'The :choices attribute must be a hash'
29
+ end
30
+ end
31
+
32
+ context 'when a hash' do
33
+ it 'does not raise any error' do
34
+ expect { opt }.to_not raise_error
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#validate' do
41
+ context 'when an unknown choice is given' do
42
+ it 'raises an error' do
43
+ expect { opt.validate('vp,n') }.to raise_error 'Unknown choice: n'
44
+ end
45
+ end
46
+
47
+ context 'when nil or empty value' do
48
+ context 'when no value_if_empty attribute' do
49
+ it 'raises an error' do
50
+ [nil, ''].each do |value|
51
+ expect { opt.validate(value) }.to raise_error 'Empty option value supplied'
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'when value_if_empty attribute' do
57
+ let(:attrs) { super().merge(value_if_empty: 'vp,u') }
58
+
59
+ it 'returns the expected hash' do
60
+ [nil, ''].each do |value|
61
+ expect(opt.validate(value)).to eql(vulenrable_plugins: true, users: (1..10))
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'when value' do
68
+ it 'returns the expected hash' do
69
+ expect(opt.validate('u2-5')).to eql(users: (2..5))
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#normalize' do
75
+ it 'returns the same value (no normalization)' do
76
+ expect(opt.normalize('a')).to eql 'a'
77
+ end
78
+ end
79
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt_parse_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - WPScanTeam - Erwan le Rousseau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -72,28 +72,28 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.7'
75
+ version: '1.6'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.7'
82
+ version: '1.6'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.28'
89
+ version: '0.29'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.28'
96
+ version: '0.29'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: simplecov
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +136,8 @@ files:
136
136
  - lib/opt_parse_validator/opts/directory_path.rb
137
137
  - lib/opt_parse_validator/opts/file_path.rb
138
138
  - lib/opt_parse_validator/opts/integer.rb
139
+ - lib/opt_parse_validator/opts/integer_range.rb
140
+ - lib/opt_parse_validator/opts/multi_choices.rb
139
141
  - lib/opt_parse_validator/opts/path.rb
140
142
  - lib/opt_parse_validator/opts/positive_integer.rb
141
143
  - lib/opt_parse_validator/opts/proxy.rb
@@ -158,7 +160,9 @@ files:
158
160
  - spec/lib/opt_parse_validator/opts/credentials_spec.rb
159
161
  - spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb
160
162
  - spec/lib/opt_parse_validator/opts/file_path_spec.rb
163
+ - spec/lib/opt_parse_validator/opts/integer_range_spec.rb
161
164
  - spec/lib/opt_parse_validator/opts/integer_spec.rb
165
+ - spec/lib/opt_parse_validator/opts/multi_choices_spec.rb
162
166
  - spec/lib/opt_parse_validator/opts/path_spec.rb
163
167
  - spec/lib/opt_parse_validator/opts/positive_integer_spec.rb
164
168
  - spec/lib/opt_parse_validator/opts/proxy_spec.rb
@@ -206,7 +210,9 @@ test_files:
206
210
  - spec/lib/opt_parse_validator/opts/credentials_spec.rb
207
211
  - spec/lib/opt_parse_validator/opts/direcyory_path_spec.rb
208
212
  - spec/lib/opt_parse_validator/opts/file_path_spec.rb
213
+ - spec/lib/opt_parse_validator/opts/integer_range_spec.rb
209
214
  - spec/lib/opt_parse_validator/opts/integer_spec.rb
215
+ - spec/lib/opt_parse_validator/opts/multi_choices_spec.rb
210
216
  - spec/lib/opt_parse_validator/opts/path_spec.rb
211
217
  - spec/lib/opt_parse_validator/opts/positive_integer_spec.rb
212
218
  - spec/lib/opt_parse_validator/opts/proxy_spec.rb