| 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: @options = Util.default_options
19:
20: @options.merge!(defaults)
21: end
These options will be added to all cli tools
# File lib/mcollective/optionparser.rb, line 96
96: def add_common_options
97: @parser.separator ""
98: @parser.separator "Common Options"
99:
100: @parser.on('-T', '--target COLLECTIVE', 'Target messages to a specific sub collective') do |f|
101: @options[:collective] = f
102: end
103:
104: @parser.on('-c', '--config FILE', 'Load configuratuion from file rather than default') do |f|
105: @options[:config] = f
106: end
107:
108: @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t|
109: @options[:disctimeout] = t
110: end
111:
112: @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t|
113: @options[:timeout] = t
114: end
115:
116: @parser.on('-q', '--quiet', 'Do not be verbose') do |v|
117: @options[:verbose] = false
118: end
119:
120: @parser.on('-v', '--verbose', 'Be verbose') do |v|
121: @options[:verbose] = v
122: end
123:
124: @parser.on('-h', '--help', 'Display this screen') do
125: puts @parser
126: exit! 1
127: end
128: end
These options will be added if you pass ‘filter’ into the include list of the constructor.
# File lib/mcollective/optionparser.rb, line 61
61: def add_filter_options
62: @parser.separator ""
63: @parser.separator "Host Filters"
64:
65: @parser.on('-W', '--with FILTER', 'Combined classes and facts filter') do |f|
66: f.split(" ").each do |filter|
67: fact_parsed = parse_fact(filter)
68: if fact_parsed
69: @options[:filter]["fact"] << fact_parsed
70: else
71: @options[:filter]["cf_class"] << filter
72: end
73: end
74: end
75:
76: @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f|
77: fact_parsed = parse_fact(f)
78:
79: @options[:filter]["fact"] << fact_parsed if fact_parsed
80: end
81:
82: @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f|
83: @options[:filter]["cf_class"] << f
84: end
85:
86: @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a|
87: @options[:filter]["agent"] << a
88: end
89:
90: @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a|
91: @options[:filter]["identity"] << a
92: end
93: 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
}
Users can set default options that get parsed in using the MCOLLECTIVE_EXTRA_OPTS environemnt variable
# File lib/mcollective/optionparser.rb, line 40
40: def parse(&block)
41: yield(@parser, @options) if block_given?
42:
43: add_common_options
44:
45: [@include].flatten.compact.each do |i|
46: options_name = "add_#{i}_options"
47: send(options_name) if respond_to?(options_name)
48: end
49:
50: @parser.environment("MCOLLECTIVE_EXTRA_OPTS")
51:
52: @parser.parse!
53:
54: @options[:collective] = Config.instance.main_collective unless @options.include?(:collective)
55:
56: @options
57: end