Class: Daemons::Optparse

Inherits:
Object
  • Object
show all
Defined in:
lib/daemons/cmdline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Optparse

Returns a new instance of Optparse.


5
6
7
8
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
# File 'lib/daemons/cmdline.rb', line 5

def initialize(controller)
  @controller = controller
  @options = {}

  @opts = OptionParser.new do |opts|
    opts.banner = ''

    opts.on('-t', '--ontop', 'Stay on top (does not daemonize)') do |t|
      @options[:ontop] = t
    end

    opts.on('-s', '--shush', 'Silent mode (no output to the terminal)') do |t|
      @options[:shush] = t
    end

    opts.on('-f', '--force', 'Force operation') do |t|
      @options[:force] = t
    end

    opts.on('-n', '--no_wait', 'Do not wait for processes to stop') do |t|
      @options[:no_wait] = t
    end

    opts.on('-w', '--force_kill_waittime SECONDS', Integer, 'Maximum time to wait for processes to stop before force-killing') do |t|
      @options[:force_kill_waittime] = t
    end
    opts.on('--signals_and_waits STRING', String, 'which signal to use to stop and how long to wait e.g. TERM:20|KILL:20') do |t|
      @options[:signals_and_waits] = t
    end

    opts.on('--pid_delimiter STRING', 'Text used to separate process number in full process name and pid-file name') do |value|
      @options[:pid_delimiter] = value
    end

    opts.separator ''
    opts.separator 'Common options:'

    opts.on('-l', '--log_output', 'Enable input/output stream redirection') do |value|
      @options[:log_output] = value
    end

    opts.on('--logfilename FILE', String, 'Custom log file name for exceptions') do |value|
      @options[:logfilename] = value
    end

    opts.on('--output_logfilename FILE', String, 'Custom file name for input/output stream redirection log') do |value|
      @options[:output_logfilename] = value
    end

    opts.on('--log_dir DIR', String, 'Directory for log files') do |value|
      @options[:log_dir] = value
    end

    opts.on('--syslog', 'Enable output redirction into SYSLOG instead of a file') do |value|
      @options[:log_output_syslog] = value
    end

    # No argument, shows at tail.  This will print an options summary
    opts.on_tail('-h', '--help', 'Show this message') do
      controller.print_usage

      exit
    end

    # Switch to print the version.
    opts.on_tail('--version', 'Show version') do
      puts "daemons version #{Daemons::VERSION}"
      exit
    end
  end

  begin
    @usage = @opts.to_s
  rescue ::Exception # work around a bug in ruby 1.9
    @usage = <<END
        -t, --ontop                      Stay on top (does not daemonize)
        -f, --force                      Force operation
        -n, --no_wait                    Do not wait for processes to stop

    Common options:
        -h, --help                       Show this message
            --version                    Show version
END
  end
end

Instance Attribute Details

#usageObject (readonly)

Returns the value of attribute usage


3
4
5
# File 'lib/daemons/cmdline.rb', line 3

def usage
  @usage
end

Instance Method Details

#parse(args) ⇒ Object

Return a hash describing the options.


93
94
95
96
97
98
99
100
# File 'lib/daemons/cmdline.rb', line 93

def parse(args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.

  @opts.parse(args)

  @options
end