Class: CustomOptionParser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/common/custom_option_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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


7
8
9
10
11
# File 'lib/common/custom_option_parser.rb', line 7

def initialize(banner = nil, width = 32, indent = ' ' * 4)
  @results         = {}
  @symbols_used    = []
  super(banner, width, indent)
end

Instance Attribute Details

#symbols_usedObject (readonly)

Returns the value of attribute symbols_used


5
6
7
# File 'lib/common/custom_option_parser.rb', line 5

def symbols_used
  @symbols_used
end

Instance Method Details

#add(options) ⇒ Object

param Array(Array) or Array options


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/common/custom_option_parser.rb', line 15

def add(options)
  if options.is_a?(Array)
    if options[0].is_a?(Array)
      options.each do |option|
        add_option(option)
      end
    else
      add_option(options)
    end
  else
    raise "Options must be at least an Array, or an Array(Array). #{options.class} supplied"
  end
end

#add_option(option) ⇒ Object

param Array option


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/common/custom_option_parser.rb', line 30

def add_option(option)
  if option.is_a?(Array)
    option_symbol = CustomOptionParser::option_to_symbol(option)

    if !@symbols_used.include?(option_symbol)
      @symbols_used << option_symbol

      self.on(*option) do |arg|
        @results[option_symbol] = arg
      end
    else
      raise "The option #{option_symbol} is already used !"
    end
  else
    raise "The option must be an array, #{option.class} supplied : '#{option}'"
  end
end

#results(argv = default_argv) ⇒ Object

return Hash


49
50
51
52
53
# File 'lib/common/custom_option_parser.rb', line 49

def results(argv = default_argv)
  self.parse!(argv) if @results.empty?

  @results
end