Class: MCollective::Optionparser

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/optionparser.rb

Overview

A simple helper to build cli tools that supports a uniform command line layout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}, include_sections = nil, exclude_sections = nil) ⇒ Optionparser

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()

Starts a parser in verbose mode that does not show the common options:

oparser = MCollective::Optionparser.new({:verbose => true}, "filter", "common")


20
21
22
23
24
25
26
27
28
29
# File 'lib/mcollective/optionparser.rb', line 20

def initialize(defaults = {}, include_sections = nil, exclude_sections = nil)
  @parser = ::OptionParser.new

  @include = [include_sections].flatten
  @exclude = [exclude_sections].flatten

  @options = Util.default_options

  @options.merge!(defaults)
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



5
6
7
# File 'lib/mcollective/optionparser.rb', line 5

def parser
  @parser
end

Instance Method Details

#add_common_optionsObject

These options will be added to most cli tools



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mcollective/optionparser.rb', line 128

def add_common_options
  @parser.separator ""
  @parser.separator "Common Options"

  @parser.on('-T', '--target COLLECTIVE', 'Target messages to a specific sub collective') do |f|
    @options[:collective] = f
  end

  @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t|
    @options[:disctimeout] = t
  end

  @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t|
    @options[:timeout] = t
  end

  @parser.on('-q', '--quiet', 'Do not be verbose') do |v|
    @options[:verbose] = false
  end

  @parser.on('--ttl TTL', 'Set the message validity period') do |v|
    @options[:ttl] = v.to_i
  end

  @parser.on('--reply-to TARGET', 'Set a custom target for replies') do |v|
    @options[:reply_to] = v
  end

  @parser.on('--dm', '--disc-method METHOD', 'Which discovery method to use') do |v|
    raise "Discovery method is already set by a competing option" if @options[:discovery_method] && @options[:discovery_method] != v
    @options[:discovery_method] = v
  end

  @parser.on('--do', '--disc-option OPTION', 'Options to pass to the discovery method') do |a|
    @options[:discovery_options] << a
  end

  @parser.on("--nodes FILE", "List of nodes to address") do |v|
    raise "Cannot mix --disc-method, --disc-option and --nodes" if @options[:discovery_method] || @options[:discovery_options].size > 0
    raise "Cannot read the discovery file #{v}" unless File.readable?(v)

    @options[:discovery_method] = "flatfile"
    @options[:discovery_options] << v
  end

  @parser.on("--publish_timeout TIMEOUT", Integer, "Timeout for publishing requests to remote agents.") do |pt|
    @options[:publish_timeout] = pt
  end

  @parser.on("--threaded", "Start publishing requests and receiving responses in threaded mode.") do |v|
    @options[:threaded] = true
  end

  @parser.on("--sort", "Sort the output of an RPC call before processing.") do |v|
    @options[:sort] = true
  end

  @parser.on("--connection-timeout TIMEOUT", Integer, "Set the timeout for establishing a connection to the middleware") do |v|
    @options[:connection_timeout] = Integer(v)
  end
end

#add_filter_optionsObject

These options will be added if you pass ‘filter’ into the include list of the constructor.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mcollective/optionparser.rb', line 73

def add_filter_options
  @parser.separator ""
  @parser.separator "Host Filters"

  @parser.on('-W', '--with FILTER', 'Combined classes and facts filter') do |f|
    f.split(" ").each do |filter|
      begin
        fact_parsed = parse_fact(filter)
        @options[:filter]["fact"] << fact_parsed
      rescue
        @options[:filter]["cf_class"] << filter
      end
    end
  end

  @parser.on('-S', '--select FILTER', 'Compound filter combining facts and classes') do |f|
    @options[:filter]["compound"] << Matcher.create_compound_callstack(f)
  end

  @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f|
    fact_parsed = parse_fact(f)

    @options[:filter]["fact"] << fact_parsed if fact_parsed
  end

  @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f|
    @options[:filter]["cf_class"] << f
  end

  @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a|
    @options[:filter]["agent"] << a
  end

  @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a|
    @options[:filter]["identity"] << a
  end
end

#add_required_optionsObject

These options should always be present



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mcollective/optionparser.rb', line 112

def add_required_options
  @parser.on('-c', '--config FILE', 'Load configuration from file rather than default') do |f|
    @options[:config] = f
  end

  @parser.on('-v', '--verbose', 'Be verbose') do |v|
    @options[:verbose] = v
  end

  @parser.on('-h', '--help', 'Display this screen') do
    puts @parser
    exit! 1
  end
end

#parse {|@parser, @options| ... } ⇒ Object

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

Yields:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mcollective/optionparser.rb', line 48

def parse(&block)
  yield(@parser, @options) if block_given?

  add_required_options

  add_common_options unless @exclude.include?("common")

  @include.each do |i|
    next if @exclude.include?(i)

    options_name = "add_#{i}_options"
    send(options_name)  if respond_to?(options_name)
  end

  @parser.environment("MCOLLECTIVE_EXTRA_OPTS")

  @parser.parse!

  @options[:collective] = Config.instance.main_collective unless @options[:collective]

  @options
end