class Bake::Arguments

Structured access to arguments.

Attributes

options[R]
ordered[R]

Public Class Methods

extract(recipe, arguments) click to toggle source
# File lib/bake/arguments.rb, line 29
def self.extract(recipe, arguments)
        self.new(recipe).extract(arguments)
end
new(recipe) click to toggle source
# File lib/bake/arguments.rb, line 33
def initialize(recipe)
        @recipe = recipe
        
        @types = recipe.types
        @parameters = recipe.parameters
        @arity = recipe.arity
        
        @ordered = []
        @options = {}
end

Public Instance Methods

extract(arguments) click to toggle source
# File lib/bake/arguments.rb, line 47
def extract(arguments)
        while argument = arguments.first
                if /^--(?<name>.*)$/ =~ argument
                        # Consume the argument:
                        arguments.shift
                        
                        if name.empty?
                                break
                        end
                        
                        name = normalize(name)
                        
                        # Extract the trailing arguments:
                        @options[name] = extract_arguments(name, arguments)
                elsif /^(?<name>.*?)=(?<value>.*)$/ =~ argument
                        # Consume the argument:
                        arguments.shift
                        
                        name = name.to_sym
                        
                        # Extract the single argument:
                        @options[name] = extract_argument(name, value)
                elsif @ordered.size < @arity
                        _, name = @parameters.shift
                        value = arguments.shift
                        
                        # Consume it:
                        @ordered << extract_argument(name, value)
                else
                        break
                end
        end
        
        return @ordered, @options
end

Private Instance Methods

delimiter_index(arguments) click to toggle source
# File lib/bake/arguments.rb, line 89
def delimiter_index(arguments)
        arguments.index{|argument| argument =~ /\A(--|;\z)/}
end
extract_argument(name, value) click to toggle source
# File lib/bake/arguments.rb, line 118
def extract_argument(name, value)
        if type = @types[name]
                value = type.parse(value)
        end
        
        return value
end
extract_arguments(name, arguments) click to toggle source
# File lib/bake/arguments.rb, line 93
def extract_arguments(name, arguments)
        value = nil
        type = @types[name]
        
        # Can this named parameter accept more than one input argument?
        if type&.composite?
                if count = delimiter_index(arguments)
                        value = arguments.shift(count)
                        arguments.shift if arguments.first == ';'
                else
                        value = arguments.dup
                        arguments.clear
                end
        else
                # Otherwise we just take one item:
                value = arguments.shift
        end
        
        if type
                value = type.parse(value)
        end
        
        return value
end
normalize(name) click to toggle source
# File lib/bake/arguments.rb, line 85
def normalize(name)
        name.tr('-', '_').to_sym
end