class Avalanche::CLI::Application

Public Class Methods

new(name, version) click to toggle source
# File lib/avalanche/cli/application.rb, line 4
def initialize(name, version)
  @name         = name
  @version      = version
  @commands     = {}
  @opts         = OptionParser.new

  @opts.program_name = @name

  @opts.banner = "Usage: #{@name} [--version] [--help] command [options] [args]"

  @opts.top.append("", nil, nil)
  @opts.top.append("Available #{@name} commands are:", nil, nil)

  @opts.base.append("", nil, nil)
  @opts.base.append("Common options:", nil, nil)

  @opts.on_tail("-h", "--help",    "Show this message") { print_help    }
  @opts.on_tail('-v', '--version', "Show version")      { print_version }
end

Public Instance Methods

command(usage, description, options, handler) click to toggle source
# File lib/avalanche/cli/application.rb, line 24
def command(usage, description, options, handler)
  parts   = usage.split(" ")
  command = parts.shift

  @opts.separator(sprintf("  %-10.10s %s", command, description))

  parts.unshift("#{@name}-#{command}")

  @commands[command] = Command.new(parts.join(" "), options, handler)
end
run(argv) click to toggle source
# File lib/avalanche/cli/application.rb, line 35
def run(argv)
  if argv.empty?
    print_help
  end

  command = @commands[argv.first]

  if command.nil?
    @opts.permute!(argv)
    print_help
  else
    argv.shift
    command.run(argv)
  end
rescue OptionParser::InvalidOption => e
  @opts.warn(e.message)
  print_help
rescue Interrupt
  exit 0
rescue => e
  @opts.abort("#{e.class.name}: #{e.message}")
end
to_proc() click to toggle source
# File lib/avalanche/cli/application.rb, line 58
def to_proc
  proc { |opts, args| run(args) }
end

Private Instance Methods

print_help() click to toggle source
print_version() click to toggle source