Class: OptParseValidator::OptParser

Inherits:
OptionParser show all
Defined in:
lib/opt_parse_validator.rb

Overview

Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(banner = nil, width = 32, indent = ' ' * 4) ⇒ OptParser

Returns a new instance of OptParser.



20
21
22
23
24
25
26
# File 'lib/opt_parse_validator.rb', line 20

def initialize(banner = nil, width = 32, indent = ' ' * 4)
  @results      = {}
  @symbols_used = []
  @opts         = []

  super(banner, width, indent)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/opt_parse_validator.rb', line 18

def opts
  @opts
end

#symbols_usedObject (readonly)

Returns the value of attribute symbols_used.



18
19
20
# File 'lib/opt_parse_validator.rb', line 18

def symbols_used
  @symbols_used
end

Instance Method Details

#add(*options) ⇒ void

This method returns an undefined value.

Parameters:



31
32
33
# File 'lib/opt_parse_validator.rb', line 31

def add(*options)
  options.each { |option| add_option(option) }
end

#add_option(opt) ⇒ void

This method returns an undefined value.

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/opt_parse_validator.rb', line 38

def add_option(opt)
  check_option(opt)

  @opts << opt
  @symbols_used << opt.to_sym
  # Set the default option value if it exists
  # The default value is not validated as provided by devs
  # and should be set to the correct format/value directly
  @results[opt.to_sym] = opt.default unless opt.default.nil?

  on(*opt.option) do |arg|
    begin
      @results[opt.to_sym] = opt.normalize(opt.validate(arg))
    rescue StandardError => e
      # Adds the long option name to the message
      # And raises it as an OptParseValidator::Error if not already one
      # e.g --proxy Invalid Scheme format.
      raise e.is_a?(Error) ? e.class : Error, "#{opt.to_long} #{e}"
    end
  end
end

#check_option(opt) ⇒ void

This method returns an undefined value.

Ensures the opt given is valid

Parameters:

Raises:



65
66
67
68
# File 'lib/opt_parse_validator.rb', line 65

def check_option(opt)
  raise Error, "The option is not an OptBase, #{opt.class} supplied" unless opt.is_a?(OptBase)
  raise Error, "The option #{opt.to_sym} is already used !" if @symbols_used.include?(opt.to_sym)
end

#load_options_filesVoid

Returns:

  • (Void)


83
84
85
86
87
88
89
90
91
# File 'lib/opt_parse_validator.rb', line 83

def load_options_files
  files_data = options_files.parse

  @opts.each do |opt|
    next unless files_data.key?(opt.to_sym)

    @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s))
  end
end

#options_filesOptParseValidator::OptionsFiles



94
95
96
# File 'lib/opt_parse_validator.rb', line 94

def options_files
  @options_files ||= OptionsFiles.new
end

#post_processingVoid

Ensure that all required options are supplied Should be overriden to modify the behavior

Returns:

  • (Void)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/opt_parse_validator.rb', line 102

def post_processing
  @opts.each do |opt|
    if opt.required?
      raise NoRequiredOption, "The option #{opt} is required" unless @results.key?(opt.to_sym)
    end

    next if opt.required_unless.empty?
    next if @results.key?(opt.to_sym)

    fail_msg = "One of the following options is required: #{opt}, #{opt.required_unless.join(', ')}"

    raise NoRequiredOption, fail_msg unless opt.required_unless.any? do |sym|
      @results.key?(sym)
    end
  end
end

#results(argv = default_argv) ⇒ Hash

Returns:

  • (Hash)


71
72
73
74
75
76
77
78
79
80
# File 'lib/opt_parse_validator.rb', line 71

def results(argv = default_argv)
  load_options_files
  parse!(argv)
  post_processing

  @results
rescue StandardError => e
  # Raise it as an OptParseValidator::Error if not already one
  raise e.is_a?(Error) ? e.class : Error, e.message
end