class OptParseValidator::OptParser

Validator

Attributes

opts[R]
symbols_used[R]

Public Class Methods

new(banner = nil, width = 32, indent = ' ' * 4) click to toggle source
Calls superclass method
# 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

Public Instance Methods

add(*options) click to toggle source

@param [ Array<OptBase> ] options

@return [ Self ] For chaining new.add.results

# 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_files() click to toggle source

@return [ OptParseValidator::ConfigFilesLoaderMerger ]

# File lib/opt_parse_validator.rb, line 32
def config_files
  @config_files ||= ConfigFilesLoaderMerger.new
end
full_help() click to toggle source

@return [ String ] The full help, with the advanced option/s listed

# File lib/opt_parse_validator.rb, line 90
def full_help
  to_s
end
results(argv = default_argv) click to toggle source

@return [ Hash ]

# 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_help() click to toggle source

@return [ String ] The simplified help (without any of the advanced option/s listed)

# 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

Protected Instance Methods

check_option(opt) click to toggle source

Ensures the opt given is valid

@param [ OptBase ] opt

@return [ void ]

# File lib/opt_parse_validator.rb, line 101
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_config_files() click to toggle source

@return [ Void ]

# File lib/opt_parse_validator.rb, line 125
def load_config_files
  files_data = config_files.parse

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

    begin
      @results[opt.to_sym] = opt.normalize(opt.validate(files_data[opt.to_sym].to_s))
    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
post_processing() click to toggle source

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

@return [ Void ]

# File lib/opt_parse_validator.rb, line 146
def post_processing
  @opts.each do |opt|
    raise NoRequiredOption, "The option #{opt.to_long} is required" if opt.required? && !@results.key?(opt.to_sym)

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

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

    raise NoRequiredOption, fail_msg unless opt.required_unless.any? do |sym|
      @results.key?(sym)
    end
  end
end
register_callback(opt) click to toggle source

@param [ OptBase ] opt

@return [ void ]

# File lib/opt_parse_validator.rb, line 109
def register_callback(opt)
  on(*opt.option) do |arg|
    if opt.alias?
      parse!(opt.alias_for.split)
    else
      @results[opt.to_sym] = opt.normalize(opt.validate(arg))
    end
  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