Class: Ziltoid::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ziltoid/command_parser.rb

Constant Summary collapse

ALLOWED_COMMANDS =
["watch", "start", "stop", "restart"]

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ziltoid/command_parser.rb', line 10

def self.parse(args)
  runnable = OpenStruct.new

  helptext = <<-HELP
    Available commands are :
       watch :       watches all processes
       start :       starts all processes
       stop  :       stops all processes
       restart :     restarts all processes
  HELP

  opt_parser = OptionParser.new do |opts|
    # Printing generic help at the top of commands summary
    opts.banner = "Usage: ziltoid.rb [options]"
    opts.separator ""
    opts.separator helptext
    opts.separator ""
    opts.separator "Common options :"

    # No argument, shows at tail. This will print a commands summary.
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  # Retrieves all arguments except option-like ones (e.g. '-h' or '-v')
  opt_parser.parse!(args)
  # Fetches the first argument as the intended command
  command = args.shift

  # Making sure the command is valid, otherwise print commands summary
  if command && ALLOWED_COMMANDS.include?(command)
    runnable.command = command
  else
    puts opt_parser.help
    exit
  end

  runnable
end