Class: PerfMonger::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/perfmonger/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



27
28
29
# File 'lib/perfmonger/cli.rb', line 27

def initialize

end

Class Method Details

.register_alias(alias_name, command_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/perfmonger/cli.rb', line 15

def self.register_alias(alias_name, command_name)
  if @@commands.nil?
    raise RuntimeError.new("No command is registered yet.")
  end

  if ! @@commands.has_key?(command_name)
    raise RuntimeError.new("Command '#{command_name}' is not registered.")
  end

  @@aliases[alias_name] = command_name
end

.register_command(command_name, klass) ⇒ Object



8
9
10
11
12
13
# File 'lib/perfmonger/cli.rb', line 8

def self.register_command(command_name, klass)
  @@commands ||= Hash.new
  @@aliases ||= Hash.new

  @@commands[command_name] = klass
end

Instance Method Details

#run(argv = ARGV) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/perfmonger/cli.rb', line 31

def run(argv = ARGV)
  parser = OptionParser.new
  parser.banner = "Usage: \#{File.basename($0)} [options] COMMAND [args]\n\n"

  ## make list of subcommands
  commands = @@commands.values.sort_by do |command|
    # important command first: sort by [priority, name]
    command_name = command.command_name
    case command_name
    when "live"
      [0, command_name]
    when "record"
      [1, command_name]
    when "play"
      [2, command_name]
    when "stat"
      [3, command_name]
    when "plot"
      [4, command_name]
    else
      [999, command_name]
    end
  end

  max_len = commands.map(&:command_name).map(&:size).max
  command_list_str = commands.map do |command|
    # pad command names
    command_name = command.command_name
    command_name = command_name + (" " * (max_len - command_name.size))

    str = "    " + command_name + "   " + command.description

    if command.aliases && command.aliases.size > 0
      str += "\n" + "    " + (" " * max_len) + "   " +
        "Aliases: " + command.aliases.join(", ")
    end

    str
  end.join("\n")

  subcommand_list = "\nCommands:\n\#{command_list_str}\n\n"

  parser.summary_indent = "  "

  parser.on('-h', '--help', 'Show this help') do
    puts(parser.help)
    puts(subcommand_list)
    exit(true)
  end

  parser.on('-v', '--version', 'Show version number') do
    puts("PerfMonger version " + PerfMonger::VERSION)
    exit(true)
  end

  parser.order!(argv)

  if argv.size == 0
    puts(parser.help)
    puts(subcommand_list)
    exit(false)
  end

  command_name = argv.shift

  if @@aliases[command_name]
    command_name = @@aliases[command_name]
  end
  command_class = @@commands[command_name]

  unless command_class
    puts("No such command: #{command_name}")
    puts(subcommand_list)
    exit(false)
  end

  command_class.new.run(argv)
end