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 +4 -4
- data/lib/opt_parse_validator/opts/base.rb +10 -0
- data/lib/opt_parse_validator/version.rb +1 -1
- data/lib/opt_parse_validator.rb +11 -2
- data/spec/lib/opt_parse_validator_spec.rb +34 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e79a03f62b4755570f5b2639381b31b9900c92f4
|
4
|
+
data.tar.gz: 2f35d0102777f6ba8d069d01a52850ee7e8d2209
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/opt_parse_validator.rb
CHANGED
@@ -68,9 +68,18 @@ module OptParseValidator
|
|
68
68
|
# @return [ Void ]
|
69
69
|
def post_processing
|
70
70
|
@opts.each do |opt|
|
71
|
-
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|