| Class | MCollective::Optionparser |
| In: |
lib/mcollective/optionparser.rb
|
| Parent: | Object |
A simple helper to build cli tools that supports a uniform command line layout.
Creates a new instance of the parser, you can supply defaults and include named groups of options.
Starts a parser that defaults to verbose and that includs the filter options:
oparser = MCollective::Optionparser.new({:verbose => true}, "filter")
Stats a parser in non verbose mode that does support discovery
oparser = MCollective::Optionparser.new()
# File lib/mcollective/optionparser.rb, line 14
14: def initialize(defaults = {}, include = nil)
15: @parser = OptionParser.new
16: @include = include
17:
18: timeout = ENV["MCOLLECTIVE_TIMEOUT"] || 5
19: dtimeout = ENV["MCOLLECTIVE_DTIMEOUT"] || 2
20:
21: # expand_path is pretty lame, it relies on HOME environment
22: # which isnt't always there so just handling all exceptions
23: # here as cant find reverting to default
24: begin
25: config = File.expand_path("~/.mcollective")
26:
27: unless File.readable?(config) && File.file?(config)
28: config = "/etc/mcollective/client.cfg"
29: end
30: rescue Exception => e
31: config = "/etc/mcollective/client.cfg"
32: end
33:
34: @options = {:disctimeout => dtimeout.to_i,
35: :timeout => timeout.to_i,
36: :verbose => false,
37: :filter => Util.empty_filter,
38: :config => config}
39:
40: @options.merge!(defaults)
41: end
These options will be added to all cli tools
# File lib/mcollective/optionparser.rb, line 106
106: def add_common_options
107: @parser.separator ""
108: @parser.separator "Common Options"
109:
110: @parser.on('-c', '--config FILE', 'Load configuratuion from file rather than default') do |f|
111: @options[:config] = f
112: end
113:
114: @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t|
115: @options[:disctimeout] = t
116: end
117:
118: @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t|
119: @options[:timeout] = t
120: end
121:
122: @parser.on('-q', '--quiet', 'Do not be verbose') do |v|
123: @options[:verbose] = false
124: end
125:
126: @parser.on('-v', '--verbose', 'Be verbose') do |v|
127: @options[:verbose] = v
128: end
129:
130: @parser.on('-h', '--help', 'Display this screen') do
131: puts @parser
132: exit! 1
133: end
134: end
These options will be added if you pass ‘filter’ into the include list of the constructor.
# File lib/mcollective/optionparser.rb, line 74
74: def add_filter_options
75: @parser.separator ""
76: @parser.separator "Host Filters"
77:
78: @parser.on('-W', '--with FILTER', 'Combined classes and facts filter') do |f|
79: f.split(" ").each do |filter|
80: if filter =~ /^(.+?)=(.+)/
81: @options[:filter]["fact"] << {:fact => $1, :value => $2}
82: else
83: @options[:filter]["cf_class"] << filter
84: end
85: end
86: end
87:
88: @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f|
89: @options[:filter]["fact"] << {:fact => $1, :value => $2} if f =~ /^(.+?)=(.+)/
90: end
91:
92: @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f|
93: @options[:filter]["cf_class"] << f
94: end
95:
96: @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a|
97: @options[:filter]["agent"] << a
98: end
99:
100: @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a|
101: @options[:filter]["identity"] << a
102: end
103: end
Parse the options returning the options, you can pass a block that adds additional options to the Optionparser.
The sample below starts a parser that also prompts for —arguments in addition to the defaults. It also sets the description and shows a usage message specific to this app.
options = oparser.parse{|parser, options|
parser.define_head "Control the mcollective controller daemon"
parser.banner = "Usage: sh-mcollective [options] command"
parser.on('--arg', '--argument ARGUMENT', 'Argument to pass to agent') do |v|
options[:argument] = v
end
}
# File lib/mcollective/optionparser.rb, line 57
57: def parse(&block)
58: yield(@parser, @options) if block_given?
59:
60: add_common_options
61:
62: [@include].flatten.compact.each do |i|
63: options_name = "add_#{i}_options"
64: send(options_name) if respond_to?(options_name)
65: end
66:
67: @parser.parse!
68:
69: @options
70: end