2 # A simple helper to build cli tools that supports a uniform command line
7 # Creates a new instance of the parser, you can supply defaults and include named groups of options.
9 # Starts a parser that defaults to verbose and that includs the filter options:
11 # oparser = MCollective::Optionparser.new({:verbose => true}, "filter")
13 # Stats a parser in non verbose mode that does support discovery
15 # oparser = MCollective::Optionparser.new()
17 # Starts a parser in verbose mode that does not show the common options:
19 # oparser = MCollective::Optionparser.new({:verbose => true}, "filter", "common")
20 def initialize(defaults = {}, include_sections = nil, exclude_sections = nil)
21 @parser = ::OptionParser.new
23 @include = [include_sections].flatten
24 @exclude = [exclude_sections].flatten
26 @options = Util.default_options
28 @options.merge!(defaults)
31 # Parse the options returning the options, you can pass a block that adds additional options
32 # to the Optionparser.
34 # The sample below starts a parser that also prompts for --arguments in addition to the defaults.
35 # It also sets the description and shows a usage message specific to this app.
37 # options = oparser.parse{|parser, options|
38 # parser.define_head "Control the mcollective controller daemon"
39 # parser.banner = "Usage: sh-mcollective [options] command"
41 # parser.on('--arg', '--argument ARGUMENT', 'Argument to pass to agent') do |v|
42 # options[:argument] = v
46 # Users can set default options that get parsed in using the MCOLLECTIVE_EXTRA_OPTS environemnt
49 yield(@parser, @options) if block_given?
53 add_common_options unless @exclude.include?("common")
56 next if @exclude.include?(i)
58 options_name = "add_#{i}_options"
59 send(options_name) if respond_to?(options_name)
62 @parser.environment("MCOLLECTIVE_EXTRA_OPTS")
66 @options[:collective] = Config.instance.main_collective unless @options[:collective]
71 # These options will be added if you pass 'filter' into the include list of the
73 def add_filter_options
75 @parser.separator "Host Filters"
77 @parser.on('-W', '--with FILTER', 'Combined classes and facts filter') do |f|
78 f.split(" ").each do |filter|
80 fact_parsed = parse_fact(filter)
81 @options[:filter]["fact"] << fact_parsed
83 @options[:filter]["cf_class"] << filter
88 @parser.on('-S', '--select FILTER', 'Compound filter combining facts and classes') do |f|
89 @options[:filter]["compound"] << Matcher.create_compound_callstack(f)
92 @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f|
93 fact_parsed = parse_fact(f)
95 @options[:filter]["fact"] << fact_parsed if fact_parsed
98 @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f|
99 @options[:filter]["cf_class"] << f
102 @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a|
103 @options[:filter]["agent"] << a
106 @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a|
107 @options[:filter]["identity"] << a
111 # These options should always be present
112 def add_required_options
113 @parser.on('-c', '--config FILE', 'Load configuratuion from file rather than default') do |f|
114 @options[:config] = f
117 @parser.on('-v', '--verbose', 'Be verbose') do |v|
118 @options[:verbose] = v
121 @parser.on('-h', '--help', 'Display this screen') do
127 # These options will be added to most cli tools
128 def add_common_options
130 @parser.separator "Common Options"
132 @parser.on('-T', '--target COLLECTIVE', 'Target messages to a specific sub collective') do |f|
133 @options[:collective] = f
136 @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t|
137 @options[:disctimeout] = t
140 @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t|
141 @options[:timeout] = t
144 @parser.on('-q', '--quiet', 'Do not be verbose') do |v|
145 @options[:verbose] = false
148 @parser.on('--ttl TTL', 'Set the message validity period') do |v|
149 @options[:ttl] = v.to_i
152 @parser.on('--reply-to TARGET', 'Set a custom target for replies') do |v|
153 @options[:reply_to] = v
156 @parser.on('--dm', '--disc-method METHOD', 'Which discovery method to use') do |v|
157 raise "Discovery method is already set by a competing option" if @options[:discovery_method] && @options[:discovery_method] != v
158 @options[:discovery_method] = v
161 @parser.on('--do', '--disc-option OPTION', 'Options to pass to the discovery method') do |a|
162 @options[:discovery_options] << a
165 @parser.on("--nodes FILE", "List of nodes to address") do |v|
166 raise "Cannot mix --disc-method, --disc-option and --nodes" if @options[:discovery_method] || @options[:discovery_options].size > 0
167 raise "Cannot read the discovery file #{v}" unless File.readable?(v)
169 @options[:discovery_method] = "flatfile"
170 @options[:discovery_options] = v
175 # Parse a fact filter string like foo=bar into the tuple hash thats needed
177 Util.parse_fact_string(fact)