opt_parse_validator 0.0.14.1 → 0.0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '0286c5b4aeeef23b862af3aa5f029908b5965bd1'
4
- data.tar.gz: 625e42c2b8e97c9f448aec2612a6893ad0c02539
3
+ metadata.gz: 4781af5c07e14acc18ea3aeb507e30a695fb76f1
4
+ data.tar.gz: d30c05cf7536638a26646750bbf1ec35e8add56a
5
5
  SHA512:
6
- metadata.gz: f7d1bd2b8315771f040b1467cd27d475151d860c898ee90b8872f16efca592de1d73f14cbe502e4556e6cc5d9438250335800cca71e8b67fa9f5ae95676cbeea
7
- data.tar.gz: 59e2147c23f7ce56a749c3c124a49b7b8508d86be105a7ab7a537830c71afe8dc98da23836cec2819770e17d87b2da9ddc100dec889db2fb6fb1bbfaac4b4452
6
+ metadata.gz: cb916606bb5d3eee896ca9adfa54cfcaa7d77710c5d4a0a7defb2602a89b5b8ba2d20f09b386084193a77f2b22858edb8db7789f594b0e4ad8e182447b57f36d
7
+ data.tar.gz: '094c7bb730917e6a488490380417a5e4d3e92681c132116956150724020247c6afdbb8f44dc5ea7268c2e3922c89c4ab9df3c50a4aae653dd5121830418a884e'
data/README.md CHANGED
@@ -4,7 +4,7 @@ OptParseValidator
4
4
  [![Gem Version](https://badge.fury.io/rb/opt_parse_validator.svg)](https://badge.fury.io/rb/opt_parse_validator)
5
5
  [![Build Status](https://img.shields.io/travis/wpscanteam/OptParseValidator.svg)](https://travis-ci.org/wpscanteam/OptParseValidator)
6
6
  [![Coverage Status](https://img.shields.io/coveralls/wpscanteam/OptParseValidator.svg)](https://coveralls.io/r/wpscanteam/OptParseValidator?branch=master)
7
- [![Code Climate](https://img.shields.io/codeclimate/github/wpscanteam/OptParseValidator.svg)](https://codeclimate.com/github/wpscanteam/OptParseValidator)
7
+ [![Code Climate](https://api.codeclimate.com/v1/badges/56f0307bbbda6d41b99a/maintainability)](https://codeclimate.com/github/wpscanteam/OptParseValidator/maintainability)
8
8
  [![Dependency Status](https://img.shields.io/gemnasium/wpscanteam/OptParseValidator.svg)](https://gemnasium.com/wpscanteam/OptParseValidator)
9
9
 
10
10
 
@@ -19,18 +19,17 @@ OptParseValidator
19
19
 
20
20
  require 'opt_parse_validator'
21
21
 
22
- # For contructor options, such as setting a banner, the summary width and indent,
23
- # see http://ruby-doc.org/stdlib-2.4.2/libdoc/optparse/rdoc/OptionParser.html#method-c-new
24
- parser = OptParseValidator::OptParser.new
25
-
26
- parser.add(
27
- OptParseValidator::OptString.new(['-m', '--mandatory PARAM', 'A Mandatory CLI option'], required: true),
28
- OptParseValidator::OptBoolean.new(['--test', '-t', 'Option Helper Message']),
29
- OptParseValidator::OptFilePath.new(['-o', '--output FILE', 'Output to FILE'], writable: true, exists: false)
30
- )
31
-
32
22
  begin
33
- p parser.results
23
+ # For constructor options, such as setting a banner, the summary width and indent,
24
+ # see http://ruby-doc.org/stdlib-2.4.2/libdoc/optparse/rdoc/OptionParser.html#method-c-new
25
+ parsed_cli_options = OptParseValidator::OptParser.new.add(
26
+ OptParseValidator::OptString.new(['-m', '--mandatory PARAM', 'A Mandatory CLI option'], required: true),
27
+ OptParseValidator::OptBoolean.new(['--test', '-t', 'Option Helper Message']),
28
+ OptParseValidator::OptFilePath.new(['-o', '--output FILE', 'Output to FILE'], writable: true, exists: false),
29
+ OptParseValidator::OptAlias.new(['--alias', '-a'], alias_for: '--test -o file.txt')
30
+ ).results
31
+
32
+ p parsed_cli_options
34
33
  rescue OptParseValidator::Error => e
35
34
  puts 'Parsing Error: ' + e.message
36
35
  end
@@ -41,11 +40,13 @@ Then have a play with
41
40
  ```ruby test.rb -m hh -t```
42
41
  ```ruby test.rb -t```
43
42
 
43
+ It is worth to note that when aliases are used, it is recommended to provide them first in the CLI. Otherwise, they might override user-suplied cli options. For example, using the options above, ```ruby test.rb -m aa -o override.txt --alias``` won't have the desired effect (to have --output as override.txt), but ```ruby test.rb --alias -m aa -o override.txt``` will.
44
+
44
45
  For more option examples, see
45
46
  - https://github.com/wpscanteam/CMSScanner/blob/master/app/controllers/core/cli_options.rb
46
47
  - https://github.com/wpscanteam/wpscan-v3/blob/master/app/controllers/enumeration/cli_options.rb
47
48
 
48
- Please Feel free to send Pull Requests to improve this Readme
49
+ Please feel free to send Pull Requests to improve this Readme
49
50
 
50
51
  ### Global Attributes
51
52
 
@@ -56,6 +57,8 @@ Some attributes are available for all Validators:
56
57
  - :value_if_empty (Value to use if no argument has been supplied for the related option)
57
58
 
58
59
  ### Available Validators & Associated Attributes:
60
+ - Alias:
61
+ - :alias_for (mandatory)
59
62
  - Array
60
63
  - :separator (default: ',')
61
64
  - Boolean
@@ -0,0 +1,26 @@
1
+ module OptParseValidator
2
+ # Implementation of the Alias Option
3
+ class OptAlias < OptBase
4
+ def initialize(option, attrs = {})
5
+ raise Error, 'The :alias_for attribute is required' unless attrs.key?(:alias_for)
6
+
7
+ super(option, attrs)
8
+ end
9
+
10
+ def append_help_messages
11
+ super
12
+
13
+ option << "Alias for #{alias_for}"
14
+ end
15
+
16
+ # @return [ String ]
17
+ def alias_for
18
+ @alias_for ||= attrs[:alias_for]
19
+ end
20
+
21
+ # @return [ Boolean ]
22
+ def alias?
23
+ true
24
+ end
25
+ end
26
+ end
@@ -55,6 +55,11 @@ module OptParseValidator
55
55
  attrs[:value_if_empty]
56
56
  end
57
57
 
58
+ # @return [ Boolean ]
59
+ def alias?
60
+ false
61
+ end
62
+
58
63
  # @param [ String ] value
59
64
  def validate(value)
60
65
  if value.nil? || value.to_s.empty?
@@ -3,9 +3,8 @@ module OptParseValidator
3
3
  class OptCredentials < OptBase
4
4
  # @return [ Hash ] A hash containing the :username and :password
5
5
  def validate(value)
6
- unless value.index(':')
7
- raise Error, 'Incorrect credentials format, username:password expected'
8
- end
6
+ raise Error, 'Incorrect credentials format, username:password expected' unless value.index(':')
7
+
9
8
  creds = value.split(':', 2)
10
9
 
11
10
  { username: creds[0], password: creds[1] }
@@ -25,9 +25,7 @@ module OptParseValidator
25
25
  def validate(value)
26
26
  uri = Addressable::URI.parse(value)
27
27
 
28
- if !uri.scheme && default_protocol
29
- uri = Addressable::URI.parse("#{default_protocol}://#{value}")
30
- end
28
+ uri = Addressable::URI.parse("#{default_protocol}://#{value}") if !uri.scheme && default_protocol
31
29
 
32
30
  unless allowed_protocols.empty? || allowed_protocols.include?(uri.scheme)
33
31
  # For future refs: will have to check if the uri.scheme exists,
@@ -1,7 +1,7 @@
1
1
  %w[
2
2
  base string integer positive_integer choice boolean uri url proxy credentials
3
3
  path file_path directory_path array integer_range multi_choices regexp headers
4
- smart_list
4
+ smart_list alias
5
5
  ].each do |opt|
6
6
  require 'opt_parse_validator/opts/' + opt
7
7
  end
@@ -1,4 +1,4 @@
1
1
  # Gem Version
2
2
  module OptParseValidator
3
- VERSION = '0.0.14.1'.freeze
3
+ VERSION = '0.0.15.0'.freeze
4
4
  end
@@ -25,46 +25,30 @@ module OptParseValidator
25
25
  super(banner, width, indent)
26
26
  end
27
27
 
28
- # @param [ OptBase ] options
29
- #
30
- # @return [ void ]
31
- def add(*options)
32
- options.each { |option| add_option(option) }
28
+ # @return [ OptParseValidator::OptionsFiles ]
29
+ def options_files
30
+ @options_files ||= OptionsFiles.new
33
31
  end
34
32
 
35
- # @param [ OptBase ] opt
33
+ # @param [ Array<OptBase> ] options
36
34
  #
37
- # @return [ void ]
38
- def add_option(opt)
39
- check_option(opt)
35
+ # @return [ Self ] For chaining #new.add.results
36
+ def add(*options)
37
+ options.each do |option|
38
+ check_option(option)
40
39
 
41
- @opts << opt
42
- @symbols_used << opt.to_sym
43
- # Set the default option value if it exists
44
- # The default value is not validated as provided by devs
45
- # and should be set to the correct format/value directly
46
- @results[opt.to_sym] = opt.default unless opt.default.nil?
40
+ @opts << option
41
+ @symbols_used << option.to_sym
47
42
 
48
- on(*opt.option) do |arg|
49
- begin
50
- @results[opt.to_sym] = opt.normalize(opt.validate(arg))
51
- rescue StandardError => e
52
- # Adds the long option name to the message
53
- # And raises it as an OptParseValidator::Error if not already one
54
- # e.g --proxy Invalid Scheme format.
55
- raise e.is_a?(Error) ? e.class : Error, "#{opt.to_long} #{e}"
56
- end
43
+ # Set the default option value if it exists
44
+ # The default value is not validated as it is provided by devs
45
+ # and should be set to the correct format/value directly
46
+ @results[option.to_sym] = option.default unless option.default.nil?
47
+
48
+ register_callback(option)
57
49
  end
58
- end
59
50
 
60
- # Ensures the opt given is valid
61
- #
62
- # @param [ OptBase ] opt
63
- #
64
- # @return [ void ]
65
- def check_option(opt)
66
- raise Error, "The option is not an OptBase, #{opt.class} supplied" unless opt.is_a?(OptBase)
67
- raise Error, "The option #{opt.to_sym} is already used !" if @symbols_used.include?(opt.to_sym)
51
+ self
68
52
  end
69
53
 
70
54
  # @return [ Hash ]
@@ -79,6 +63,38 @@ module OptParseValidator
79
63
  raise e.is_a?(Error) ? e.class : Error, e.message
80
64
  end
81
65
 
66
+ protected
67
+
68
+ # Ensures the opt given is valid
69
+ #
70
+ # @param [ OptBase ] opt
71
+ #
72
+ # @return [ void ]
73
+ def check_option(opt)
74
+ raise Error, "The option is not an OptBase, #{opt.class} supplied" unless opt.is_a?(OptBase)
75
+ raise Error, "The option #{opt.to_sym} is already used !" if @symbols_used.include?(opt.to_sym)
76
+ end
77
+
78
+ # @param [ OptBase ] opt
79
+ #
80
+ # @return [ void ]
81
+ def register_callback(opt)
82
+ on(*opt.option) do |arg|
83
+ begin
84
+ if opt.alias?
85
+ parse!(opt.alias_for.split(' '))
86
+ else
87
+ @results[opt.to_sym] = opt.normalize(opt.validate(arg))
88
+ end
89
+ rescue StandardError => e
90
+ # Adds the long option name to the message
91
+ # And raises it as an OptParseValidator::Error if not already one
92
+ # e.g --proxy Invalid Scheme format.
93
+ raise e.is_a?(Error) ? e.class : Error, "#{opt.to_long} #{e}"
94
+ end
95
+ end
96
+ end
97
+
82
98
  # @return [ Void ]
83
99
  def load_options_files
84
100
  files_data = options_files.parse
@@ -90,23 +106,15 @@ module OptParseValidator
90
106
  end
91
107
  end
92
108
 
93
- # @return [ OptParseValidator::OptionsFiles ]
94
- def options_files
95
- @options_files ||= OptionsFiles.new
96
- end
97
-
98
109
  # Ensure that all required options are supplied
99
110
  # Should be overriden to modify the behavior
100
111
  #
101
112
  # @return [ Void ]
102
113
  def post_processing
103
114
  @opts.each do |opt|
104
- if opt.required?
105
- raise NoRequiredOption, "The option #{opt} is required" unless @results.key?(opt.to_sym)
106
- end
115
+ raise NoRequiredOption, "The option #{opt} is required" if opt.required? && !@results.key?(opt.to_sym)
107
116
 
108
- next if opt.required_unless.empty?
109
- next if @results.key?(opt.to_sym)
117
+ next if opt.required_unless.empty? || @results.key?(opt.to_sym)
110
118
 
111
119
  fail_msg = "One of the following options is required: #{opt}, #{opt.required_unless.join(', ')}"
112
120
 
@@ -41,6 +41,6 @@ Gem::Specification.new do |s|
41
41
  s.add_development_dependency 'rake', '~> 12.0'
42
42
  s.add_development_dependency 'rspec', '~> 3.7.0'
43
43
  s.add_development_dependency 'rspec-its', '~> 1.2.0'
44
- s.add_development_dependency 'rubocop', '~> 0.51.0'
44
+ s.add_development_dependency 'rubocop', '~> 0.52.0'
45
45
  s.add_development_dependency 'simplecov', '~> 0.14.0' # Can't update to 0.15 as it breaks coveralls dep
46
46
  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.14.1
4
+ version: 0.0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - WPScanTeam
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: 0.51.0
117
+ version: 0.52.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: 0.51.0
124
+ version: 0.52.0
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: simplecov
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -155,6 +155,7 @@ files:
155
155
  - lib/opt_parse_validator/options_file/yml.rb
156
156
  - lib/opt_parse_validator/options_files.rb
157
157
  - lib/opt_parse_validator/opts.rb
158
+ - lib/opt_parse_validator/opts/alias.rb
158
159
  - lib/opt_parse_validator/opts/array.rb
159
160
  - lib/opt_parse_validator/opts/base.rb
160
161
  - lib/opt_parse_validator/opts/boolean.rb