Class: Switchboard::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/switchboard/commands/command.rb

Class Method Summary collapse

Class Method Details

.description(description = nil) ⇒ Object



29
30
31
32
# File 'lib/switchboard/commands/command.rb', line 29

def self.description(description = nil)
  @description = description if description
  @description
end

.helpObject



34
35
36
# File 'lib/switchboard/commands/command.rb', line 34

def self.help
  "No help is available for this command (#{self.to_command_name})."
end

.options(opts) ⇒ Object

TODO consider accepting a block in subclasses



39
40
41
42
43
# File 'lib/switchboard/commands/command.rb', line 39

def self.options(opts)
  @options = opts
  @options.banner = "Usage: #{opts.program_name} [options] #{self.to_command_name} [options] [args]"
  @options
end

.run!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/switchboard/commands/command.rb', line 45

def self.run!
  puts self.options(OptionParser.new).help
  commands = Switchboard.commands(self)

  if commands.any?
    puts
    puts "Available commands:"
    commands.each do |name, command|
      puts "   #{command.to_command.ljust(15)}#{command.description}"
      command.options(OptionParser.new).summarize do |line|
        puts " " * 16 + line
      end
    end
    puts
    puts "See '#{@options.program_name} help COMMAND' for more information on a specific command."
  end
end

.to_commandObject



63
64
65
# File 'lib/switchboard/commands/command.rb', line 63

def self.to_command
  self.name.gsub("Switchboard::Commands::", "").split("::").last.downcase
end

.to_command_name(delimiter = " ") ⇒ Object



67
68
69
# File 'lib/switchboard/commands/command.rb', line 67

def self.to_command_name(delimiter = " ")
  self.name.gsub("Switchboard::Commands::", "").split("::").map { |c| c.downcase } * delimiter
end