Class List Method List File List

# Class: CustomOptionParser

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

## Instance Attribute Summary (collapse)

- - (Object) symbols_used readonly

  Returns the value of attribute symbols_used.

## Class Method Summary (collapse)

- + (Object) option_to_symbol(option) protected

  param Array option.

## Instance Method Summary (collapse)

- - (Object) add(options)

  param Array(Array) or Array options.

- - (Object) add_option(option)

  param Array option.

- - (CustomOptionParser) initialize(banner = nil, width = 32, indent = ' ' * 4) constructor

  A new instance of CustomOptionParser.

- - (Object) results(argv = default_argv)

  return Hash.

## Constructor Details

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

A new instance of 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

### - (`Object`) symbols_used (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

## Class Method Details

### + (`Object`) option_to_symbol(option) (protected)

param Array option

    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    # File 'lib/common/custom_option_parser.rb', line 57

    def self.option_to_symbol(option)
      option_name = nil

      option.each do |option_attr|
        if option_attr =~ /^--/
          option_name = option_attr
          break
        end
      end

      if option_name
        option_name = option_name.gsub(/^--/, '').gsub(/-/, '_').gsub(/ .*$/, '')
        :#{option_name}"
      else
        raise "Could not find the option name for #{option}"
      end
    end

## Instance Method Details

### - (`Object`) add(options)

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

### - (`Object`) add_option(option)

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

### - (`Object`) results(argv = default_argv)

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