Class MCollective::Optionparser
In: lib/mcollective/optionparser.rb
Parent: Object

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

Methods

Public Class methods

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

[Source]

    # 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

Public Instance methods

These options will be added to all cli tools

[Source]

     # File lib/mcollective/optionparser.rb, line 94
 94:         def add_common_options
 95:             @parser.separator ""
 96:             @parser.separator "Common Options"
 97: 
 98:             @parser.on('-c', '--config FILE', 'Load configuratuion from file rather than default') do |f|
 99:                 @options[:config] = f
100:             end
101: 
102:             @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t|
103:                 @options[:disctimeout] = t
104:             end
105: 
106:             @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t|
107:                 @options[:timeout] = t
108:             end
109: 
110:             @parser.on('-q', '--quiet', 'Do not be verbose') do |v|
111:                 @options[:verbose] = false
112:             end
113: 
114:             @parser.on('-v', '--verbose', 'Be verbose') do |v|
115:                 @options[:verbose] = v
116:             end
117: 
118:             @parser.on('-h', '--help', 'Display this screen') do
119:                 puts @parser
120:                 exit! 1
121:             end
122:         end

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

[Source]

    # File lib/mcollective/optionparser.rb, line 59
59:         def add_filter_options
60:             @parser.separator ""
61:             @parser.separator "Host Filters"
62: 
63:             @parser.on('-W', '--with FILTER', 'Combined classes and facts filter') do |f|
64:                 f.split(" ").each do |filter|
65:                     fact_parsed = parse_fact(filter)
66:                     if fact_parsed
67:                         @options[:filter]["fact"] << fact_parsed
68:                     else
69:                         @options[:filter]["cf_class"] << filter
70:                     end
71:                 end
72:             end
73: 
74:             @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f|
75:                 fact_parsed = parse_fact(f)
76: 
77:                 @options[:filter]["fact"] << fact_parsed if fact_parsed
78:             end
79: 
80:             @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f|
81:                 @options[:filter]["cf_class"] << f
82:             end
83: 
84:             @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a|
85:                 @options[:filter]["agent"] << a
86:             end
87: 
88:             @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a|
89:                 @options[:filter]["identity"] << a
90:             end
91:         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

[Source]

    # 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
55:         end

[Validate]