Class: Ame::Options

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ame-1.0/options.rb

Overview

The options to a method in its defined state. Does the processing of options to the method and also enumerates #each of the options to the method for, for example, help output.

Defined Under Namespace

Classes: Undefined

Instance Method Summary collapse

Constructor Details

#initialize(options, ordered, options_must_precede_arguments) ⇒ Options

Returns a new instance of Options.



10
11
12
13
# File 'lib/ame-1.0/options.rb', line 10

def initialize(options, ordered, options_must_precede_arguments)
  @options, @ordered, @options_must_precede_arguments =
    options, ordered, options_must_precede_arguments
end

Instance Method Details

# {|option| ... } ⇒ Object #Enumerator<Option>

Overloads:

  • # {|option| ... } ⇒ Object

    Enumerates the options.

    Yield Parameters:

  • #Enumerator<Option>

    Returns An Enumerator over the options.

    Returns:

    • (Enumerator<Option>)

      An Enumerator over the options



54
55
56
57
58
59
60
# File 'lib/ame-1.0/options.rb', line 54

def each
  return enum_for(__method__) unless block_given?
  @ordered.each do |option|
    yield option
  end
  self
end

#process(arguments) ⇒ [Hash<String,Object>, Array<String>]

Returns The Flag#processed options as a Hash mapping the Flag#name to the parsed value or the option’s default after filtering out any Flag#ignored? options and the remaining non-option arguments.

Parameters:

  • arguments (Array<String>)

Returns:

  • ([Hash<String,Object>, Array<String>])

    The Flag#processed options as a Hash mapping the Flag#name to the parsed value or the option’s default after filtering out any Flag#ignored? options and the remaining non-option arguments

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ame-1.0/options.rb', line 23

def process(arguments)
  arguments = arguments.dup
  results = @ordered.reduce({}){ |d, o| d[o.name] = o.default; d }
  remainder = []
  until arguments.empty?
    case first = arguments.shift
    when '--'
      break
    when /\A-([^=-]{2,})\z/
      combined = $1
      until combined.empty?
        option = self['-' + combined[0].chr]
        results[option.name], combined = option.process_combined(results, arguments, $1, combined[1..-1])
      end
    when /\A(--[^=]+|-[^-])(?:=(.*))?\z/
      option = self[$1]
      results[option.name] = option.process(results, arguments, $1, $2)
    else
      remainder << first
      break if @options_must_precede_arguments
    end
  end
  [results.reject{ |n, _| self[n].ignored? }, remainder.concat(arguments)]
end