class Tlog::Application

Public Class Methods

new(input, output) click to toggle source
# File lib/tlog/application.rb, line 4
def initialize(input, output)
  @input = input
  @output = output
end

Public Instance Methods

run() click to toggle source
# File lib/tlog/application.rb, line 9
def run
  command_name = ""
  outcome = false
  begin
    command_name = @input.args.shift
    command = find(command_name)
    prepare_command(command)
    outcome = run_command(command)
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    @output.error($!)
    @output.error(@optparse.to_s)
  rescue Tlog::Error::CommandInvalid
    @output.error(command_name + " command invalid: " + $!.message)
    @output.error(@optparse.to_s)
  rescue Tlog::Error::CommandNotFound, OptionParser::MissingArgument
    @output.error($!)
    @output.error(@optparse.to_s)
  rescue
    @output.error($!)
  end
  return outcome
end

Private Instance Methods

find(command_name) click to toggle source
# File lib/tlog/application.rb, line 34
def find(command_name)
  commands = Tlog::Command_Suite.commands
  command = nil
  commands.each do |cmd|
    return cmd if cmd.name == command_name
  end
  command
end
prepare_command(command) click to toggle source
# File lib/tlog/application.rb, line 43
def prepare_command(command)
  if !command.nil?
    @optparse = OptionParser.new do |parser|
      command.options(parser, @input.options)
    end
    @optparse.parse!(@input.args)
  end
end
run_command(command) click to toggle source
# File lib/tlog/application.rb, line 52
def run_command(command)
  if !command.nil?
    command.execute(@input, @output)
    true
  else
    raise Tlog::Error::CommandNotFound, "Command not found, use 'tlog help' for list of commands"
  end
end