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) ⇒ Self

Returns For chaining #new.add.results.

Parameters:

Returns:

  • (Self)

    For chaining #new.add.results



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/opt_parse_validator.rb', line 36

def add(*options)
  options.each do |option|
    check_option(option)

    @opts << option
    @symbols_used << option.to_sym

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

    register_callback(option)
  end

  self
end

#options_filesOptParseValidator::OptionsFiles



29
30
31
# File 'lib/opt_parse_validator.rb', line 29

def options_files
  @options_files ||= OptionsFiles.new
end

#results(argv = default_argv) ⇒ Hash

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
# File 'lib/opt_parse_validator.rb', line 55

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