Class: Bicho::CLI::Command

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/bicho/cli/command.rb

Overview

Bicho allows to easily add commands to the command line interface.

In order to create a command, add a class under Bicho::CLI::Commands. Then you need to:

  • Add options, using a Trollop syntax

  • Implement do(global_opts, options, args)

You can use t.say to talk to the terminal including all HighLine features.

<tt> class Bicho::CLI::Commands::Hello < ::Bicho::CLI::Command

options do
  opt :monkey, "Use monkey mode", :default => true
  opt :text, "Name", :type => :string
end

def do(global_opts, opts, args)
  t.say("Hello")
end

end </tt>

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Constructor Details

#initializeCommand

Returns a new instance of Command



65
66
67
# File 'lib/bicho/cli/command.rb', line 65

def initialize
  @t = HighLine.new
end

Class Attribute Details

.parserObject

Returns the value of attribute parser



61
62
63
# File 'lib/bicho/cli/command.rb', line 61

def parser
  @parser
end

Instance Attribute Details

#tObject

Returns the value of attribute t



63
64
65
# File 'lib/bicho/cli/command.rb', line 63

def t
  @t
end

Class Method Details

.opt(*args) ⇒ Object

Gateway to Trollop



70
71
72
73
# File 'lib/bicho/cli/command.rb', line 70

def self.opt(*args)
  self.parser = Trollop::Parser.new if not self.parser
  self.parser.opt(*args)
end

.optionsObject

DSL method to describe a command's option



76
77
78
# File 'lib/bicho/cli/command.rb', line 76

def self.options
  yield
end

Instance Method Details

#do(opts, args) ⇒ Object

Raises:

  • (RuntimeError)


93
94
95
# File 'lib/bicho/cli/command.rb', line 93

def do(opts, args)
  raise RuntimeError, "No implementation for #{self.class}" if self.class =~ /CommandTemplate/
end

#parse_optionsObject

Called by the cli to get the options with current ARGV



82
83
84
85
86
87
# File 'lib/bicho/cli/command.rb', line 82

def parse_options
  self.class.parser = Trollop::Parser.new if not self.class.parser
  opts = Trollop::with_standard_exception_handling(self.class.parser) do
    o = self.class.parser.parse ARGV
  end
end

#parserObject



89
90
91
# File 'lib/bicho/cli/command.rb', line 89

def parser
  self.class.parser
end