opt_parse_validator 0.0.8 → 0.0.9

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: f77666c01d6e2405f194bb9571c77ffb90945dd0
4
- data.tar.gz: 006b7e8a8f1497da7c30d32537c579669630b9ad
3
+ metadata.gz: e79a03f62b4755570f5b2639381b31b9900c92f4
4
+ data.tar.gz: 2f35d0102777f6ba8d069d01a52850ee7e8d2209
5
5
  SHA512:
6
- metadata.gz: 5f088679e26508b674b9bcc70c8fc036d29f9c501939df67cd6838aa341673af287e026117e5b23133fe24715ad29155d49d08a90e435b45a346106acb97a6bc
7
- data.tar.gz: ad09a370941caa0b52d3ec7f74b2de5a9122cb5b31cff987e9e645fead9519f3550fb47ca8f1b43c74faf2729c1529c87151eefea5fbed13e9a5d2cbf2a84122
6
+ metadata.gz: e8e2380fe532f69f49df35aa7f38112118a47f97fc70304eb32b2c0a668f002317c6444afde1dd9f0f0a15def4a3e888468093a4fea86515e7082e051ae2ac0a
7
+ data.tar.gz: bfbd27858ac145c2e80979639e5356fefb53ae4f989ee23a30f4316315effa653f096bb99bc4997b97d899df17f0df7b2bd542dcc40389ff10ec2ecf2f6279c9
@@ -8,6 +8,7 @@ module OptParseValidator
8
8
  # @param [ Array ] option See OptionParser#on
9
9
  # @param [ Hash ] attrs
10
10
  # @option attrs [ Boolean ] :required
11
+ # @options attrs [ Array<Symbol>, Symbol ] :required_unless
11
12
  # @option attrs [ Mixed ] :default The default value to use if the option is not supplied
12
13
  # @option attrs [ Mixed ] :value_if_empty The value to use if no arguments have been supplied
13
14
  # @option attrs [ Array<Symbol> ] :normalize See #normalize
@@ -23,6 +24,10 @@ module OptParseValidator
23
24
  @required ||= attrs[:required]
24
25
  end
25
26
 
27
+ def required_unless
28
+ @required_unless ||= [*attrs[:required_unless]]
29
+ end
30
+
26
31
  # @return [ Mixed ]
27
32
  def default
28
33
  attrs[:default]
@@ -88,5 +93,10 @@ module OptParseValidator
88
93
  end
89
94
  nil
90
95
  end
96
+
97
+ # @return [ String ]
98
+ def to_s
99
+ to_sym.to_s
100
+ end
91
101
  end
92
102
  end
@@ -1,4 +1,4 @@
1
1
  # Gem Version
2
2
  module OptParseValidator
3
- VERSION = '0.0.8'
3
+ VERSION = '0.0.9'
4
4
  end
@@ -68,9 +68,18 @@ module OptParseValidator
68
68
  # @return [ Void ]
69
69
  def post_processing
70
70
  @opts.each do |opt|
71
- next unless opt.required? && !@results.key?(opt.to_sym)
71
+ if opt.required?
72
+ fail "The option #{opt} is required" unless @results.key?(opt.to_sym)
73
+ end
74
+
75
+ next if opt.required_unless.empty?
76
+ next if @results.key?(opt.to_sym)
72
77
 
73
- fail "The option #{opt.to_sym} is required"
78
+ fail_msg = "One of the following options is required: #{opt}, #{opt.required_unless.join(', ')}"
79
+
80
+ fail fail_msg unless opt.required_unless.any? do |sym|
81
+ @results.key?(sym)
82
+ end
74
83
  end
75
84
  end
76
85
  end
@@ -97,6 +97,40 @@ describe OptParseValidator::OptParser do
97
97
  end
98
98
  end
99
99
 
100
+ context 'when :requied_unless' do
101
+ let(:url_opt) { OptParseValidator::OptURL.new(['--url URL'], required_unless: :update) }
102
+ let(:update_opt) { OptParseValidator::OptBoolean.new(['--update'], required_unless: [:url]) }
103
+ let(:options) { [url_opt, update_opt, verbose_opt] }
104
+
105
+ context 'when none supplied' do
106
+ it 'raises an error' do
107
+ @exception = 'One of the following options is required: url, update'
108
+ @argv = %w(-v)
109
+ end
110
+ end
111
+
112
+ context 'when --url' do
113
+ it 'returns the expected value' do
114
+ @expected = { url: 'http://www.g.com' }
115
+ @argv = %w(--url http://www.g.com)
116
+ end
117
+ end
118
+
119
+ context 'when --update' do
120
+ it 'returns the expected value' do
121
+ @expected = { update: true }
122
+ @argv = %w(--update)
123
+ end
124
+ end
125
+
126
+ context 'when --url and --update' do
127
+ it 'returns the expected values' do
128
+ @expected = { url: 'http://www.g.com', update: true, verbose: true }
129
+ @argv = %w(--url http://www.g.com --update -v)
130
+ end
131
+ end
132
+ end
133
+
100
134
  context 'when the default attribute is used' do
101
135
  let(:options) { [verbose_opt, default_opt] }
102
136
  let(:default_opt) { OptParseValidator::OptBase.new(['--default VALUE'], default: false) }
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.8
4
+ version: 0.0.9
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-18 00:00:00.000000000 Z
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable