class GGem::CLI

Constants

COMMANDS
CommandExitError
InvalidCommandError

Public Class Methods

new(kernel = nil, stdout = nil, stderr = nil) click to toggle source
# File lib/ggem/cli.rb, line 24
def initialize(kernel = nil, stdout = nil, stderr = nil)
  @kernel = kernel || Kernel
  @stdout = stdout || $stdout
  @stderr = stderr || $stderr
end
run(args) click to toggle source
# File lib/ggem/cli.rb, line 20
def self.run(args)
  new.run(args)
end

Public Instance Methods

run(args) click to toggle source
# File lib/ggem/cli.rb, line 30
def run(args)
  begin
    cmd_name = args.shift
    cmd = COMMANDS[cmd_name]
    cmd.run(args)
  rescue CLIRB::HelpExit
    @stdout.puts cmd.help
  rescue CLIRB::VersionExit
    @stdout.puts GGem::VERSION
  rescue CLIRB::Error, ArgumentError, InvalidCommandError => ex
    display_debug(ex)
    @stderr.puts "#{ex.message}\n\n"
    @stdout.puts cmd.help
    @kernel.exit 1
  rescue CommandExitError
    @kernel.exit 1
  rescue => ex
    @stderr.puts "#{ex.class}: #{ex.message}"
    @stderr.puts ex.backtrace.join("\n")
    @kernel.exit 1
  end
  @kernel.exit 0
end

Private Instance Methods

display_debug(exception) click to toggle source
# File lib/ggem/cli.rb, line 56
def display_debug(exception)
  if ENV["DEBUG"]
    @stderr.puts "#{exception.class}: #{exception.message}"
    @stderr.puts exception.backtrace.join("\n")
  end
end