Class: Mantra::Command

Inherits:
Object
  • Object
show all
Includes:
Helpers::ObjectWithType
Defined in:
lib/mantra/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ObjectWithType

included

Constructor Details

#initialize(options) ⇒ Command

Returns a new instance of Command.



28
29
30
# File 'lib/mantra/command.rb', line 28

def initialize(options)
  @args = options[:args]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



32
33
34
# File 'lib/mantra/command.rb', line 32

def options
  @options
end

Class Method Details

.option(name, long_option, short_option, description) ⇒ Object



16
17
18
19
20
21
# File 'lib/mantra/command.rb', line 16

def self.option(name, long_option, short_option, description)
  self.option_descriptors << [name, long_option, short_option, description]
  self.send(:define_method, name) do
    @options[name.to_s]
  end
end

.option_descriptorsObject



7
8
9
# File 'lib/mantra/command.rb', line 7

def self.option_descriptors
  @options ||= []
end

.usageObject



11
12
13
14
# File 'lib/mantra/command.rb', line 11

def self.usage
  "\n\tUsage:  \tmantra <action> [options]\n" +
  "\tactions:\t#{self.subclasses.map { |s| s.type }.join(", ")}\n\n"
end

Instance Method Details

#parse_optionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mantra/command.rb', line 33

def parse_options
  @options = {}
  OptionParser.new do |options_parser|
    options_parser.banner = "Usage: mantra #{} [options]"
    self.class.option_descriptors.each do |option|
      option_name = option.shift.to_s
      options_parser.on(*option) do |value|
        @options[option_name] = value
      end
    end
  end.parse!
  @options
end

#runObject



23
24
25
26
# File 'lib/mantra/command.rb', line 23

def run
  parse_options
  perform
end