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 = "    Available commands are :\n       watch :       watches all processes\n       start :       starts all processes\n       stop  :       stops all processes\n       restart :     restarts all processes\n  HELP\n\n  opt_parser = OptionParser.new do |opts|\n    # Printing generic help at the top of commands summary\n    opts.banner = \"Usage: ziltoid.rb [options]\"\n    opts.separator \"\"\n    opts.separator helptext\n    opts.separator \"\"\n    opts.separator \"Common options :\"\n\n    # No argument, shows at tail. This will print a commands summary.\n    opts.on_tail(\"-h\", \"--help\", \"Show this message\") do\n      puts opts\n      exit\n    end\n  end\n\n  # Retrieves all arguments except option-like ones (e.g. '-h' or '-v')\n  opt_parser.parse!(args)\n  # Fetches the first argument as the intended command\n  command = args.shift\n\n  # Making sure the command is valid, otherwise print commands summary\n  if command && ALLOWED_COMMANDS.include?(command)\n    runnable.command = command\n  else\n    puts opt_parser.help\n    exit\n  end\n\n  runnable\nend\n"