Class: Daemons::Optparse

Inherits:
Object show all
Defined in:
lib/gems/daemons-1.0.10/lib/daemons/cmdline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Optparse

Returns a new instance of Optparse.



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

def initialize(controller)
  @controller = controller
  @options = {}
  
  @opts = OptionParser.new do |opts|
    #opts.banner = "Usage: example.rb [options]"
    opts.banner = ""
    
    # Boolean switch.
#         opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
#           @options[:verbose] = v
#         end
    
    opts.on("-t", "--ontop", "Stay on top (does not daemonize)") do |t|
      @options[:ontop] = t
    end
    
    opts.on("-f", "--force", "Force operation") do |t|
      @options[:force] = t
    end
    
    #opts.separator ""
    #opts.separator "Specific options:"

    
    opts.separator ""
    opts.separator "Common options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      #puts opts
      #@usage = 
      controller.print_usage()
      
      exit
    end

    # Another typical 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

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

Instance Attribute Details

#usageObject (readonly)

Returns the value of attribute usage.



6
7
8
# File 'lib/gems/daemons-1.0.10/lib/daemons/cmdline.rb', line 6

def usage
  @usage
end

Instance Method Details

#parse(args) ⇒ Object

Return a hash describing the options.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gems/daemons-1.0.10/lib/daemons/cmdline.rb', line 71

def parse(args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  #options = {}
  
  
  ##pp args
  @opts.parse(args)
  
  return @options
end