Files changed (31) hide show
  1. checksums.yaml +5 -5
  2. data/lib/opt_parse_validator.rb +15 -6
  3. data/lib/opt_parse_validator/config_files_loader_merger.rb +20 -5
  4. data/lib/opt_parse_validator/config_files_loader_merger/base.rb +3 -2
  5. data/lib/opt_parse_validator/config_files_loader_merger/json.rb +3 -2
  6. data/lib/opt_parse_validator/config_files_loader_merger/yml.rb +4 -5
  7. data/lib/opt_parse_validator/errors.rb +2 -0
  8. data/lib/opt_parse_validator/hacks.rb +2 -0
  9. data/lib/opt_parse_validator/opts.rb +2 -0
  10. data/lib/opt_parse_validator/opts/alias.rb +2 -0
  11. data/lib/opt_parse_validator/opts/array.rb +2 -0
  12. data/lib/opt_parse_validator/opts/base.rb +3 -1
  13. data/lib/opt_parse_validator/opts/boolean.rb +2 -0
  14. data/lib/opt_parse_validator/opts/choice.rb +3 -1
  15. data/lib/opt_parse_validator/opts/credentials.rb +2 -0
  16. data/lib/opt_parse_validator/opts/directory_path.rb +2 -0
  17. data/lib/opt_parse_validator/opts/file_path.rb +2 -0
  18. data/lib/opt_parse_validator/opts/headers.rb +2 -0
  19. data/lib/opt_parse_validator/opts/integer.rb +2 -0
  20. data/lib/opt_parse_validator/opts/integer_range.rb +2 -0
  21. data/lib/opt_parse_validator/opts/multi_choices.rb +4 -2
  22. data/lib/opt_parse_validator/opts/path.rb +2 -0
  23. data/lib/opt_parse_validator/opts/positive_integer.rb +2 -0
  24. data/lib/opt_parse_validator/opts/proxy.rb +2 -0
  25. data/lib/opt_parse_validator/opts/regexp.rb +2 -0
  26. data/lib/opt_parse_validator/opts/smart_list.rb +2 -0
  27. data/lib/opt_parse_validator/opts/string.rb +2 -0
  28. data/lib/opt_parse_validator/opts/uri.rb +5 -3
  29. data/lib/opt_parse_validator/opts/url.rb +2 -0
  30. data/lib/opt_parse_validator/version.rb +3 -1
  31. metadata +26 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
---
2
- SHA1:
3
- metadata.gz: 315b683b0c51adbc5afc6087639680d8fdd7af7c
4
- data.tar.gz: 015f3118b80dbf817fafbbfd2f96479a05fa8223
2
+ SHA256:
3
+ metadata.gz: e37a9c3438b53cfe9453653062ef294af7b37c99acfe77937bd9ee1d8bebaca8
4
+ data.tar.gz: 1defdfeb5e06c3f37742fb54f19d38f3f0399d8564fcc2ffbe965ca476ab8bc5
5
5
SHA512:
6
- metadata.gz: 6dbf1eaf46c391923531d1eab92d0090c0ac574e617b2499c38ae5d5c55578621fd75bd2f9b62602d9c53eadf0c36588041c38d039896218b7b71002312f95be
7
- data.tar.gz: 8a6b2b370acc963c084e356edef00f64197d3ffa51263a3780898adb449202e789c299197f5259a629e9014747b52b19d30e5a19d3f5c5946b361cb4644d310a
6
+ metadata.gz: 7b6297d0b83ca482b02796dea0830b569adc28bdf226822091bd74649712ca08494347cf18fc1ec4f07126eb0819723858418be1d37c7e00b046860f644a9c8b
7
+ data.tar.gz: 61a03033ff7bf52add8ea3886936e79ae188149c9b01dbe24f75706f30a4d577bcc0a61f0e02830be13272c4c35bca09dfe743f5dded863627696353717acebe
data/lib/opt_parse_validator.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
# Gems
2
4
require 'addressable/uri'
3
5
require 'active_support/inflector'
@@ -27,8 +29,8 @@ module OptParseValidator
27
29
end
28
30
29
31
# @return [ OptParseValidator::ConfigFilesLoaderMerger ]
30
- def options_files
31
- @options_files ||= ConfigFilesLoaderMerger.new
32
+ def config_files
33
+ @config_files ||= ConfigFilesLoaderMerger.new
32
34
end
33
35
34
36
# @param [ Array<OptBase> ] options
@@ -54,7 +56,7 @@ module OptParseValidator
54
56
55
57
# @return [ Hash ]
56
58
def results(argv = default_argv)
57
- load_options_files
59
+ load_config_files
58
60
parse!(argv)
59
61
post_processing
60
62
@@ -122,13 +124,20 @@ module OptParseValidator
122
124
end
123
125
124
126
# @return [ Void ]
125
- def load_options_files
126
- files_data = options_files.parse(symbolize_keys: true)
127
+ def load_config_files
128
+ files_data = config_files.parse
127
129
128
130
@opts.each do |opt|
129
131
next unless files_data.key?(opt.to_sym)
130
132
131
- @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s))
133
+ begin
134
+ @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s))
135
+ rescue StandardError => e
136
+ # Adds the long option name to the message
137
+ # And raises it as an OptParseValidator::Error if not already one
138
+ # e.g --proxy Invalid Scheme format.
139
+ raise e.is_a?(Error) ? e.class : Error, "#{opt.to_long} #{e}"
140
+ end
132
141
end
133
142
end
134
143
data/lib/opt_parse_validator/config_files_loader_merger.rb CHANGED
@@ -1,7 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
require_relative 'config_files_loader_merger/base'
2
4
require_relative 'config_files_loader_merger/json'
3
5
require_relative 'config_files_loader_merger/yml'
4
6
7
+ # :nocov:
8
+ # @param [ String ] path The path of the file to load
9
+ def yaml_safe_load(path)
10
+ if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1') # Ruby 2.6
11
+ YAML.safe_load(File.read(path), permitted_classes: [Regexp]) || {}
12
+ else
13
+ YAML.safe_load(File.read(path), [Regexp]) || {}
14
+ end
15
+ end
16
+ # :nocov:
17
+
5
18
module OptParseValidator
6
19
# Options Files container
7
20
class ConfigFilesLoaderMerger < Array
@@ -15,6 +28,8 @@ module OptParseValidator
15
28
extensions.map { |sym| sym.to_s.downcase }
16
29
end
17
30
31
+ attr_accessor :result_key
32
+
18
33
# @param [ String ] file_path
19
34
#
20
35
# @return [ Self ]
@@ -29,16 +44,16 @@ module OptParseValidator
29
44
end
30
45
31
46
# @params [ Hash ] opts
32
- # @option opts [ Boolean ] :symbolize_keys Whether or not to symbolize keys in the returned hash
33
- # @option opts [ Array ] :yaml_arguments See https://ruby-doc.org/stdlib-2.3.1/libdoc/psych/rdoc/Psych.html#method-c-safe_load
34
47
#
35
48
# @return [ Hash ]
36
- def parse(opts = {})
49
+ def parse
37
50
result = {}
38
51
39
- each { |option_file| result.deep_merge!(option_file.parse(opts)) }
52
+ each { |config_file| result.deep_merge!(config_file.parse) }
53
+
54
+ result = result.dig(result_key) || {} if result_key
40
55
41
- opts[:symbolize_keys] ? result.deep_symbolize_keys : result
56
+ result.deep_symbolize_keys
42
57
end
43
58
end
44
59
end
data/lib/opt_parse_validator/config_files_loader_merger/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
class ConfigFilesLoaderMerger < Array
3
5
module ConfigFile
@@ -10,9 +12,8 @@ module OptParseValidator
10
12
@path = path
11
13
end
12
14
13
- # @params [ Hash ] opts
14
15
# @return [ Hash ] a { 'key' => value } hash
15
- def parse(_opts = {})
16
+ def parse
16
17
raise NotImplementedError
17
18
end
18
19
data/lib/opt_parse_validator/config_files_loader_merger/json.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
require 'json'
2
4
3
5
module OptParseValidator
@@ -5,9 +7,8 @@ module OptParseValidator
5
7
module ConfigFile
6
8
# Json Implementation
7
9
class JSON < Base
8
- # @params [ Hash ] opts
9
10
# @return [ Hash ] a { 'key' => value } hash
10
- def parse(_opts = {})
11
+ def parse
11
12
::JSON.parse(File.read(path))
12
13
end
13
14
end
data/lib/opt_parse_validator/config_files_loader_merger/yml.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
require 'yaml'
2
4
3
5
module OptParseValidator
@@ -5,12 +7,9 @@ module OptParseValidator
5
7
module ConfigFile
6
8
# Yaml Implementation
7
9
class YML < Base
8
- # @params [ Hash ] opts
9
- # @option opts [ Array ] :yaml_arguments See https://ruby-doc.org/stdlib-2.3.1/libdoc/psych/rdoc/Psych.html#method-c-safe_load
10
- #
11
10
# @return [ Hash ] a { 'key' => value } hash
12
- def parse(opts = {})
13
- YAML.safe_load(File.read(path), *opts[:yaml_arguments]) || {}
11
+ def parse
12
+ yaml_safe_load(path)
14
13
end
15
14
end
16
15
end
data/lib/opt_parse_validator/errors.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
class Error < RuntimeError
3
5
end
data/lib/opt_parse_validator/hacks.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
class OptionParser
2
4
# Hack to suppress the completion (expect for the -h/--help) which was leading to
3
5
# unwanted behaviours
data/lib/opt_parse_validator/opts.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
%w[
2
4
base string integer positive_integer choice boolean uri url proxy credentials
3
5
path file_path directory_path array integer_range multi_choices regexp headers
data/lib/opt_parse_validator/opts/alias.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Alias Option
3
5
class OptAlias < OptBase
data/lib/opt_parse_validator/opts/array.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Array Option
3
5
class OptArray < OptBase
data/lib/opt_parse_validator/opts/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Base Option
3
5
# This Option should not be called, children should be used.
@@ -113,7 +115,7 @@ module OptParseValidator
113
115
# @return [ String ] The raw long option (e.g: --proxy)
114
116
def to_long
115
117
option.each do |option_attr|
116
- if option_attr =~ /^--/
118
+ if /^--/.match?(option_attr)
117
119
return option_attr.gsub(/ .*#x2F;, '')
118
120
.gsub(/\[[^\]]+\]/, '')
119
121
end
data/lib/opt_parse_validator/opts/boolean.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Boolean Option
3
5
class OptBoolean < OptBase
data/lib/opt_parse_validator/opts/choice.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Choice Option
3
5
class OptChoice < OptBase
@@ -23,7 +25,7 @@ module OptParseValidator
23
25
# If :case_sensitive if false (or nil), the downcased value of the choice
24
26
# will be returned
25
27
def validate(value)
26
- value = value.to_s
28
+ value = +value.to_s
27
29
28
30
unless attrs[:case_sensitive]
29
31
value.downcase!
data/lib/opt_parse_validator/opts/credentials.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Credentials Option
3
5
class OptCredentials < OptBase
data/lib/opt_parse_validator/opts/directory_path.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implemetantion of the DirectoryPath Option
3
5
class OptDirectoryPath < OptPath
data/lib/opt_parse_validator/opts/file_path.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the FilePath Option
3
5
class OptFilePath < OptPath
data/lib/opt_parse_validator/opts/headers.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Headers Option
3
5
class OptHeaders < OptBase
data/lib/opt_parse_validator/opts/integer.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Integer Option
3
5
class OptInteger < OptBase
data/lib/opt_parse_validator/opts/integer_range.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Integer Range Option
3
5
class OptIntegerRange < OptBase
data/lib/opt_parse_validator/opts/multi_choices.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the MultiChoices Option
3
5
class OptMultiChoices < OptArray
@@ -37,7 +39,7 @@ module OptParseValidator
37
39
end
38
40
39
41
def help_message_for_default
40
- msg = ''
42
+ msg = +''
41
43
42
44
default.each do |key, value|
43
45
msg << if value == true
@@ -92,7 +94,7 @@ module OptParseValidator
92
94
# @return [ Array ]
93
95
def value_from_pattern(item)
94
96
choices.each do |key, opt|
95
- next unless item =~ /\A#{key.to_s}(.*)\z/
97
+ next unless item =~ /\A#{key}(.*)\z/
96
98
97
99
return [opt, Regexp.last_match[1]]
98
100
end
data/lib/opt_parse_validator/opts/path.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Path Option
3
5
class OptPath < OptBase
data/lib/opt_parse_validator/opts/positive_integer.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Positive Integer Option
3
5
class OptPositiveInteger < OptInteger
data/lib/opt_parse_validator/opts/proxy.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Proxy Option
3
5
class OptProxy < OptURI
data/lib/opt_parse_validator/opts/regexp.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the Regexp Option
3
5
# See http://ruby-doc.org/core-2.2.1/Regexp.html#method-c-new for expected values in :options
data/lib/opt_parse_validator/opts/smart_list.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the SmartList Option
3
5
# Such option allow users to supply a list like
data/lib/opt_parse_validator/opts/string.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Alias of OptBase
3
5
# Used for convenience
data/lib/opt_parse_validator/opts/uri.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the URI Option
3
5
class OptURI < OptBase
@@ -11,12 +13,12 @@ module OptParseValidator
11
13
12
14
# @return [ Array<String> ]
13
15
def allowed_protocols
14
- @allowed_protocols ||= [*attrs[:protocols]]
16
+ @allowed_protocols ||= [*attrs[:protocols]].map(&:downcase)
15
17
end
16
18
17
19
# The default protocol (or scheme) to use if none was given
18
20
def default_protocol
19
- @default_protocol ||= attrs[:default_protocol]
21
+ @default_protocol ||= attrs[:default_protocol]&.downcase
20
22
end
21
23
22
24
# @param [ String ] value
@@ -27,7 +29,7 @@ module OptParseValidator
27
29
28
30
uri = Addressable::URI.parse("#{default_protocol}://#{value}") if !uri.scheme && default_protocol
29
31
30
- unless allowed_protocols.empty? || allowed_protocols.include?(uri.scheme)
32
+ unless allowed_protocols.empty? || allowed_protocols.include?(uri.scheme&.downcase)
31
33
# For future refs: will have to check if the uri.scheme exists,
32
34
# otherwise it means that the value was empty
33
35
raise Addressable::URI::InvalidURIError
data/lib/opt_parse_validator/opts/url.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
module OptParseValidator
2
4
# Implementation of the URL Option
3
5
class OptURL < OptURI
data/lib/opt_parse_validator/version.rb CHANGED
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
# Gem Version
2
4
module OptParseValidator
3
- VERSION = '0.0.17.0'.freeze
5
+ VERSION = '1.8.0'
4
6
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.17.0
4
+ version: 1.8.0
5
5
platform: ruby
6
6
authors:
7
7
- WPScanTeam
8
8
autorequire:
9
9
bindir: bin
10
10
cert_chain: []
11
- date: 2019-03-02 00:00:00.000000000 Z
11
+ date: 2019-09-11 00:00:00.000000000 Z
12
12
dependencies:
13
13
- !ruby/object:Gem::Dependency
14
14
name: activesupport
@@ -19,7 +19,7 @@ dependencies:
19
19
version: '4.2'
20
20
- - "<"
21
21
- !ruby/object:Gem::Version
22
- version: 5.3.0
22
+ version: 6.1.0
23
23
type: :runtime
24
24
prerelease: false
25
25
version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
version: '4.2'
30
30
- - "<"
31
31
- !ruby/object:Gem::Version
32
- version: 5.3.0
32
+ version: 6.1.0
33
33
- !ruby/object:Gem::Dependency
34
34
name: addressable
35
35
requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
version: '2.5'
40
40
- - "<"
41
41
- !ruby/object:Gem::Version
42
- version: '2.7'
42
+ version: '2.8'
43
43
type: :runtime
44
44
prerelease: false
45
45
version_requirements: !ruby/object:Gem::Requirement
@@ -49,7 +49,7 @@ dependencies:
49
49
version: '2.5'
50
50
- - "<"
51
51
- !ruby/object:Gem::Version
52
- version: '2.7'
52
+ version: '2.8'
53
53
- !ruby/object:Gem::Dependency
54
54
name: bundler
55
55
requirement: !ruby/object:Gem::Requirement
@@ -112,28 +112,42 @@ dependencies:
112
112
requirements:
113
113
- - "~>"
114
114
- !ruby/object:Gem::Version
115
- version: 1.2.0
115
+ version: 1.3.0
116
116
type: :development
117
117
prerelease: false
118
118
version_requirements: !ruby/object:Gem::Requirement
119
119
requirements:
120
120
- - "~>"
121
121
- !ruby/object:Gem::Version
122
- version: 1.2.0
122
+ version: 1.3.0
123
123
- !ruby/object:Gem::Dependency
124
124
name: rubocop
125
125
requirement: !ruby/object:Gem::Requirement
126
126
requirements:
127
127
- - "~>"
128
128
- !ruby/object:Gem::Version
129
- version: 0.65.0
129
+ version: 0.74.0
130
130
type: :development
131
131
prerelease: false
132
132
version_requirements: !ruby/object:Gem::Requirement
133
133
requirements:
134
134
- - "~>"
135
135
- !ruby/object:Gem::Version
136
- version: 0.65.0
136
+ version: 0.74.0
137
+ - !ruby/object:Gem::Dependency
138
+ name: rubocop-performance
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: 1.4.0
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: 1.4.0
137
151
- !ruby/object:Gem::Dependency
138
152
name: simplecov
139
153
requirement: !ruby/object:Gem::Requirement
@@ -199,15 +213,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
213
requirements:
200
214
- - ">="
201
215
- !ruby/object:Gem::Version
202
- version: '2.3'
216
+ version: '2.4'
203
217
required_rubygems_version: !ruby/object:Gem::Requirement
204
218
requirements:
205
219
- - ">="
206
220
- !ruby/object:Gem::Version
207
221
version: '0'
208
222
requirements: []
209
- rubyforge_project:
223
+ rubygems_version: 3.0.3
210
- rubygems_version: 2.6.10
211
224
signing_key:
212
225
specification_version: 4
213
226
summary: Ruby OptionParser Validators