Module: Loops::CLI::Commands

Included in:
Loops::CLI
Defined in:
lib/loops/cli/commands.rb

Overview

Contains methods related to Loops commands: retrieving, instantiating, executing.

Examples:

Loops::CLI.execute(ARGV)

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
# File 'lib/loops/cli/commands.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#find_command(command_name) ⇒ Command?

Find and return an instance of Loops::Command by command name.

Parameters:

  • command_name (Symbol, String)

    a command name to register.

Returns:

  • (Command, nil)

    an instantiated command or nil, when command is not found.



106
107
108
109
110
111
112
113
114
115
# File 'lib/loops/cli/commands.rb', line 106

def find_command(command_name)
  possibilities = find_command_possibilities(command_name)
  if possibilities.size > 1 then
    raise Loops::InvalidCommandError, "Ambiguous command #{command_name} matches [#{possibilities.join(', ')}]"
  elsif possibilities.size < 1 then
    raise Loops::InvalidCommandError, "Unknown command #{command_name}"
  end

  self.class[possibilities.first]
end

#find_command_possibilities(command_name) ⇒ Array<String>

Find command possibilities (used to find command by a short name).

Parameters:

  • command_name (Symbol, String)

    a command name to register.

Returns:

  • (Array<String>)

    a list of possible commands matched to the specified short or full name.



125
126
127
128
# File 'lib/loops/cli/commands.rb', line 125

def find_command_possibilities(command_name)
  len = command_name.length
  self.class.command_names.select { |c| command_name == c[0, len] }
end

#run!Object

Run command requested.

Finds, instantiates and invokes a command.



95
96
97
# File 'lib/loops/cli/commands.rb', line 95

def run!
  @command.invoke(engine, options)
end