class Minitar::CLI::Commander

The command dispatcher for Minitar::CLI. This will be replaced in a future version by one of the better-executed CLI application frameworks like GLI, after Ruby 1.8 and 1.9 support have been dropped.

Attributes

commands[R]
default_command[R]
ioe[R]

Public Class Methods

new(ioe) click to toggle source
# File lib/minitar/cli/commander.rb, line 11
def initialize(ioe)
  @ioe = default_ioe(ioe)
  @commands = {}
  @default_command = nil
end

Public Instance Methods

[](command)
Alias for: command
command(command) click to toggle source
# File lib/minitar/cli/commander.rb, line 49
def command(command)
  if command?(command)
    commands[command]
  else
    default_command
  end
end
Also aliased as: []
command?(command) click to toggle source
# File lib/minitar/cli/commander.rb, line 45
def command?(command)
  commands.key?(command)
end
default_command=(command) click to toggle source
# File lib/minitar/cli/commander.rb, line 31
def default_command=(command)
  command = command.new if command.kind_of?(Class)

  @default_command =
    if command.kind_of?(Minitar::CLI::Command)
      register(command) unless command?(command.name)
      command
    elsif command?(command)
      commands[command]
    else
      raise UnknownCommandError
    end
end
default_ioe(ioe = {}) click to toggle source
# File lib/minitar/cli/commander.rb, line 58
def default_ioe(ioe = {})
  ioe[:input]   ||= $stdin
  ioe[:output]  ||= $stdout
  ioe[:error]   ||= $stderr
  ioe
end
register(command) click to toggle source
# File lib/minitar/cli/commander.rb, line 17
def register(command)
  command = command.new(self) if command.kind_of?(Class)
  raise CommandAlreadyExists if command.commander != self
  raise CommandAlreadyExists if command?(command.name)

  commands[command.name] = command

  # rubocop:disable Style/GuardClause
  if command.respond_to?(:altname)
    commands[command.altname] = command unless command?(command.altname)
  end
  # rubocop:enable Style/GuardClause
end