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.



23
24
25
26
27
28
29
# File 'lib/opt_parse_validator.rb', line 23

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.



21
22
23
# File 'lib/opt_parse_validator.rb', line 21

def opts
  @opts
end

#symbols_usedObject (readonly)

Returns the value of attribute symbols_used.



21
22
23
# File 'lib/opt_parse_validator.rb', line 21

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



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

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

#config_filesOptParseValidator::ConfigFilesLoaderMerger



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

def config_files
  @config_files ||= ConfigFilesLoaderMerger.new
end

#full_helpString

Returns The full help, with the advanced option/s listed.

Returns:

  • (String)

    The full help, with the advanced option/s listed



90
91
92
# File 'lib/opt_parse_validator.rb', line 90

def full_help
  to_s
end

#results(argv = default_argv) ⇒ Hash

Returns:

  • (Hash)


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

def results(argv = default_argv)
  load_config_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

#simple_helpString

Returns The simplified help (without any of the advanced option/s listed).

Returns:

  • (String)

    The simplified help (without any of the advanced option/s listed)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opt_parse_validator.rb', line 70

def simple_help
  help = to_s

  # Removes all advanced help messages
  @opts.select(&:advanced?).each do |opt|
    messages_pattern = //

    opt.help_messages.each do |msg|
      messages_pattern = /#{messages_pattern}\s*#{Regexp.escape(msg)}/
    end

    pattern = /\s*#{Regexp.escape(opt.option[0..1].select { |o| o[0] == '-' }.join(', '))}#{messages_pattern}/

    help.gsub!(pattern, '')
  end

  help
end