File: ctrl_optionparser.rb

package info (click to toggle)
ruby-daemons 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 388 kB
  • sloc: ruby: 2,133; makefile: 7
file content (41 lines) | stat: -rw-r--r-- 965 bytes parent folder | download | duplicates (2)
1
2
3
4
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
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib'))

if File.exist?(File.join(lib_dir, 'daemons.rb'))
  $LOAD_PATH.unshift lib_dir
else
  begin; require 'rubygems'; rescue ::Exception; end
end

require 'daemons'
require 'optparse'
require 'logger'
require 'ostruct'

class MyApp < Logger::Application
  def initialize(args)
    super(self.class)
    @options = OpenStruct.new(:daemonize => true)
    opts = OptionParser.new do |opts|
      opts.banner = 'Usage: myapp [options]'
      opts.separator ''
      opts.on('-N', '--no-daemonize', "Don't run as a daemon") do
        @options.daemonize = false
      end
    end
    @args = opts.parse!(args)
  end

  def run
    Daemons.run_proc('myapp', :ARGV => @args, :ontop => !@options.daemonize) do
      puts "@options.daemonize: #{@options.daemonize}"
      $stdout.sync = true
      loop do
        print '.'
        sleep(2)
      end
    end
  end
end

myapp = MyApp.new(ARGV)
myapp.run