### Home Classes Methods

### In Files

- lib/mcollective/optionparser.rb

### Parent

Object

### Methods

- ::new
- #add_common_options
- #add_filter_options
- #add_required_options
- #parse

### Files

- COPYING
- README
- Rakefile
- PLACEHOLDER
- PLACEHOLDER

### Class/Module Index

Quicksearch

- MCollective
- MCollective::Agent
- MCollective::Agents
- MCollective::Aggregate
- MCollective::Aggregate::Base
- MCollective::Aggregate::Result
- MCollective::Aggregate::Result::Base
- MCollective::Aggregate::Result::CollectionResult
- MCollective::Aggregate::Result::NumericResult
- MCollective::Application
- MCollective::Applications
- MCollective::Client
- MCollective::Config
- MCollective::Connector
- MCollective::Connector::Base
- MCollective::DDL
- MCollective::DDLValidationError
- MCollective::Data
- MCollective::Data::Base
- MCollective::Data::Result
- MCollective::Discovery
- MCollective::Facts
- MCollective::Facts::Base
- MCollective::Generators
- MCollective::Generators::AgentGenerator
- MCollective::Generators::Base
- MCollective::Generators::DataGenerator
- MCollective::InvalidRPCData
- MCollective::Log
- MCollective::Logger
- MCollective::Logger::Base
- MCollective::Logger::Console_logger
- MCollective::Logger::File_logger
- MCollective::Logger::Syslog_logger
- MCollective::Matcher
- MCollective::Matcher::Parser
- MCollective::Matcher::Scanner
- MCollective::Message
- MCollective::MissingRPCData
- MCollective::MsgDoesNotMatchRequestID
- MCollective::MsgTTLExpired
- MCollective::NotTargettedAtUs
- MCollective::Optionparser
- MCollective::PluginManager
- MCollective::PluginPackager
- MCollective::PluginPackager::AgentDefinition
- MCollective::PluginPackager::StandardDefinition
- MCollective::RPC
- MCollective::RPC::ActionRunner
- MCollective::RPC::Agent
- MCollective::RPC::Audit
- MCollective::RPC::Client
- MCollective::RPC::Helpers
- MCollective::RPC::Progress
- MCollective::RPC::Reply
- MCollective::RPC::Request
- MCollective::RPC::Result
- MCollective::RPC::Stats
- MCollective::RPCAborted
- MCollective::RPCError
- MCollective::Registration
- MCollective::Registration::Base
- MCollective::Runner
- MCollective::RunnerStats
- MCollective::SSL
- MCollective::Security
- MCollective::Security::Base
- MCollective::SecurityValidationFailed
- MCollective::Shell
- MCollective::UnixDaemon
- MCollective::UnknownRPCAction
- MCollective::UnknownRPCError
- MCollective::Util
- MCollective::WindowsDaemon
- Array
- Dir
- Object
- String
- Symbol

No matching classes.

# MCollective::Optionparser

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

### Attributes

parser[R]

### Public Instance Methods

add_common_options() click to toggle source

These options will be added to most cli tools

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

add_filter_options() click to toggle source

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

    # 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_options() click to toggle source

These options should always be present

    # File lib/mcollective/optionparser.rb, line 112
    def add_required_options
      @parser.on('-c', '--config FILE', 'Load configuratuion 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(&block) click to toggle source

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

### Public Class Methods

new(defaults = {}, include_sections = nil, exclude_sections = nil) click to toggle source

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

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.