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 Optimist 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

Constructor Details

#initializeCommand

Returns a new instance of Command.



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

def initialize
  @t = HighLine.new
end

Class Attribute Details

.parserObject

Returns the value of attribute parser.



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

def parser
  @parser
end

Instance Attribute Details

#tObject

Returns the value of attribute t.



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

def t
  @t
end

Class Method Details

.opt(*args) ⇒ Object

Gateway to Optimist



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

def self.opt(*args)
  self.parser = Optimist::Parser.new unless parser
  parser.opt(*args)
end

.optionsObject

DSL method to describe a command’s option



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

def self.options
  yield
end

Instance Method Details

#do(_opts, _args) ⇒ Object



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

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

#parse_optionsObject

Called by the cli to get the options with current ARGV



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

def parse_options
  self.class.parser = Optimist::Parser.new unless self.class.parser
  Optimist.with_standard_exception_handling(self.class.parser) do
    self.class.parser.parse ARGV
  end
end

#parserObject



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

def parser
  self.class.parser
end