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.



94
95
96
97
98
99
100
101
102
103
# File 'lib/loops/cli/commands.rb', line 94

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.



113
114
115
116
# File 'lib/loops/cli/commands.rb', line 113

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.



83
84
85
# File 'lib/loops/cli/commands.rb', line 83

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