Class: VTools::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vtools/options.rb

Overview

Takes care about passed via ARGV options

Class Method Summary collapse

Class Method Details

.parse!(args) ⇒ Object

parse config data



9
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
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
# File 'lib/vtools/options.rb', line 9

def parse! args
  # slice options afer "--" sign if given
  # catch help & version calls
  case
  when(args.include? '--')
    argv = args[ ( args.index('--') + 1)..-1]
  when(args.include? '-h')
    argv = ['-h']
  when(args.include? '-v')
    argv = ['-v']
  else
    return
  end

  # parse passed options
  OptionParser.new do |opts|

    dot = ' ' * 4
    opts.banner = "Usage: vtools <command> <daemon options> -- <options>\n" +
      "\n" +
      "Commands:\n" +
      "#{dot}start         start an instance of the application\n" +
      "#{dot}stop          stop all instances of the application\n" +
      "#{dot}restart       stop all instances and restart them afterwards\n" +
      "#{dot}reload        send a SIGHUP to all instances of the application\n" +
      "#{dot}run           start the application and stay on top\n" +
      "#{dot}zap           set the application to a stopped state\n" +
      "#{dot}status        show status (PID) of application instances\n" +
      "\n" +
      "Daemon options:\n" +

      "#{dot}-t, --ontop                      Stay on top (does not daemonize)\n" +
      "#{dot}-f, --force                      Force operation\n" +
      "#{dot}-n, --no_wait                    Do not wait for processes to stop"

    opts.separator ""
    opts.separator "Options:"

    # add config file
    opts.on("-c", "--config-file FILE", "Use configuration file") do |f|
      CONFIG[:config_file] = f
    end

    # add log file
    opts.on("-l", "--log-file [FILE]", "Log process into file (default STDOUT)") do |l|
      CONFIG[:logging]  = true
      CONFIG[:log_file] = "#{CONFIG[:PWD]}/#{l}" if l
    end

    # include path
    opts.on("-I", "--include PATH",
            "specify $LOAD_PATH (may be used more than once)") do |path|
      $LOAD_PATH.unshift(*path.split(/:/))
    end

    # connect additional library
    opts.on("-r", "--require LIBRARY",
            "require the library, before daemon starts") do |library|
      CONFIG[:library] << library
    end

    opts.separator ""

    # VTools version
    opts.on_tail("-v", "--version", "Show current version") do |operator|
      puts VERSION.join(',')
      exit
    end

    # options help
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # do parse!
    begin
      if argv.empty?
        argv = ["-h"]
        STDERR.puts "ERROR: No command given\n\n"
      end

      opts.parse!(argv)
      Hook.exec :config_parsed # callback
    rescue OptionParser::ParseError => e
      STDERR.puts "ERROR: #{e.message}\n\n", opts
      exit(-1)
    end
  end # OptionParser.new
end