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.



42
43
44
# File 'lib/mantra/command.rb', line 42

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



46
47
48
# File 'lib/mantra/command.rb', line 46

def options
  @options
end

Class Method Details

.description(description = nil) ⇒ Object



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

def self.description(description=nil)
  description.nil? ? full_description : (@description = description)
end

.full_descriptionObject



26
27
28
# File 'lib/mantra/command.rb', line 26

def self.full_description
  [@description, aliases.empty? ? nil : "(aliases: #{aliases.join(", ")})"].compact.join(" ")
end

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



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

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
15
16
17
18
19
20
# File 'lib/mantra/command.rb', line 11

def self.usage
  "Mantra is Manifest Transformation tool to ease work with BOSH manifest.\n" +
  "\nUsage:\tmantra <command> [options]" +
  "\n\nCommands:\n" + 
  self.subclasses.map { |s| "#{" "*6}#{s.type}#{" "*20}\t#{s.description}" }.join("\n") +
  "\n\n" +
  "For help on any command run:\n" +
  ["mantra <command> -h", "mantra <command> help", "mantra <command> --help"].map { |s| " " * 6 + s }.join("\n") +
  "\n\n"
end

Instance Method Details

#parse_optionsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mantra/command.rb', line 47

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



37
38
39
40
# File 'lib/mantra/command.rb', line 37

def run
  parse_options
  perform
end