Class Daemons::Optparse
In: lib/daemons/cmdline.rb
Parent: Object

Methods

new   parse  

Attributes

usage  [R] 

Public Class methods

[Source]

    # File lib/daemons/cmdline.rb, line 8
 8:     def initialize(controller)
 9:       @controller = controller
10:       @options = {}
11:       
12:       @opts = OptionParser.new do |opts|
13:         #opts.banner = "Usage: example.rb [options]"
14:         opts.banner = ""
15:         
16:         # Boolean switch.
17: #         opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
18: #           @options[:verbose] = v
19: #         end
20:         
21:         opts.on("-t", "--ontop", "Stay on top (does not daemonize)") do |t|
22:           @options[:ontop] = t
23:         end
24:         
25:         opts.on("-f", "--force", "Force operation") do |t|
26:           @options[:force] = t
27:         end
28:         
29:         #opts.separator ""
30:         #opts.separator "Specific options:"
31: 
32:         
33:         opts.separator ""
34:         opts.separator "Common options:"
35: 
36:         # No argument, shows at tail.  This will print an options summary.
37:         # Try it and see!
38:         opts.on_tail("-h", "--help", "Show this message") do
39:           #puts opts
40:           #@usage = 
41:           controller.print_usage()
42:           
43:           exit
44:         end
45: 
46:         # Another typical switch to print the version.
47:         opts.on_tail("--version", "Show version") do
48:           puts "daemons version #{Daemons::VERSION}"
49:           exit
50:         end
51:       end  
52:       
53:       begin
54:         @usage = @opts.to_s
55:       rescue ::Exception # work around a bug in ruby 1.9
56:         @usage = "            -t, --ontop                      Stay on top (does not daemonize)\n            -f, --force                      Force operation\n\n        Common options:\n            -h, --help                       Show this message\n                --version                    Show version\n"
57:       end
58:     end

Public Instance methods

Return a hash describing the options.

[Source]

    # File lib/daemons/cmdline.rb, line 72
72:     def parse(args)
73:       # The options specified on the command line will be collected in *options*.
74:       # We set default values here.
75:       #options = {}
76:       
77:       
78:       ##pp args
79:       @opts.parse(args)
80:       
81:       return @options
82:     end

[Validate]