module Filigree::Commands

Constants

HELP_COMMAND

The default help command. This can be added to your class via add_command.

Public Instance Methods

call(overloaded) click to toggle source

This will find the appropriate command and execute it.

@param [String, Array<String>] overloaded String containing the command to be processed and its arguments

@return [Object] Result of invoking the command's block

# File lib/filigree/commands.rb, line 46
def call(overloaded)
        split_line =
        if overloaded.is_a?(String)
                overloaded.split
        elsif overloaded.is_a?(Array)
                overloaded
        else
                raise TypeError, "Expected a String or Array of Strings."
        end

        namespace, rest = self.class.get_namespace(split_line)

        if namespace == self.class.commands
                raise CommandNotFoundError, split_line.join(' ')
        end

        command = namespace[:nil]

        action =
        if command.config
                conf_obj = command.config.new(rest)
                rest     = conf_obj.rest

                -> (*args) { conf_obj.instance_exec(*args, &command.action) }
        else
                command.action
        end

        if command.action.arity < 0 or command.action.arity == rest.length
                self.instance_exec(*rest, &action)
        else
                raise ArgumentError,
                      "Wrong number of arguments for command: #{command.name}. " +
                      "Expected #{command.action.arity} but got #{rest.length}."
        end
end