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.



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

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.



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

def opts
  @opts
end

#symbols_usedObject (readonly)

Returns the value of attribute symbols_used.



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

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



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

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



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

def options_files
  @options_files ||= OptionsFiles.new
end

#results(argv = default_argv) ⇒ Hash

Returns:

  • (Hash)


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

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