Class: OptParseValidator::OptParser

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

Overview

TODO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OptParser.



18
19
20
21
22
23
24
# File 'lib/opt_parse_validator.rb', line 18

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.



16
17
18
# File 'lib/opt_parse_validator.rb', line 16

def opts
  @opts
end

#symbols_usedObject (readonly)

Returns the value of attribute symbols_used.



16
17
18
# File 'lib/opt_parse_validator.rb', line 16

def symbols_used
  @symbols_used
end

Instance Method Details

#add(*options) ⇒ void

This method returns an undefined value.

Parameters:



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

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

#add_option(opt) ⇒ void

This method returns an undefined value.

Parameters:



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

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

  @opts         << opt
  @symbols_used << opt.to_sym
  # Set the default option value if it exists
  @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 => e
      # Adds the long option name to the message
      # e.g --proxy Invalid Scheme format.
      raise e.class, "#{opt.to_long} #{e}"
    end
  end
end

#load_options_filesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opt_parse_validator/options_file.rb', line 11

def load_options_files
  files_data = {}

  options_files.each do |file|
    data = parse_file(file)
    files_data.merge!(data) if data
  end

  @opts.each do |opt|
    # Annoying thing: the hash returned from parse_file is a string-full {"key"=>"value"}
    # and not a ruby hash {key: value} :/ As a result, symbol.to_s has to be used
    next unless files_data.key?(opt.to_sym.to_s)

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

#options_filesObject



7
8
9
# File 'lib/opt_parse_validator/options_file.rb', line 7

def options_files
  @options_files ||= []
end

#post_processingVoid

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

Returns:

  • (Void)


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

def post_processing
  @opts.each do |opt|
    if opt.required?
      fail "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(', ')}"

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

#results(argv = default_argv) ⇒ Hash

Returns:

  • (Hash)


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

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

  @results
end