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 100 100: def add_common_options 101: @parser.separator "" 102: @parser.separator "Common Options" 103: 104: @parser.on('-T', '--target COLLECTIVE', 'Target messages to a specific sub collective') do |f| 105: @options[:collective] = f 106: end 107: 108: @parser.on('-c', '--config FILE', 'Load configuratuion from file rather than default') do |f| 109: @options[:config] = f 110: end 111: 112: @parser.on('--dt', '--discovery-timeout SECONDS', Integer, 'Timeout for doing discovery') do |t| 113: @options[:disctimeout] = t 114: end 115: 116: @parser.on('-t', '--timeout SECONDS', Integer, 'Timeout for calling remote agents') do |t| 117: @options[:timeout] = t 118: end 119: 120: @parser.on('-q', '--quiet', 'Do not be verbose') do |v| 121: @options[:verbose] = false 122: end 123: 124: @parser.on('-v', '--verbose', 'Be verbose') do |v| 125: @options[:verbose] = v 126: end 127: 128: @parser.on('-h', '--help', 'Display this screen') do 129: puts @parser 130: exit! 1 131: end 132: 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: begin 68: fact_parsed = parse_fact(filter) 69: @options[:filter]["fact"] << fact_parsed 70: rescue 71: @options[:filter]["cf_class"] << filter 72: end 73: end 74: end 75: 76: @parser.on('-S', '--select FILTER', 'Compound filter combining facts and classes') do |f| 77: @options[:filter]["compound"] << MCollective::Matcher::Parser.new(f).execution_stack 78: end 79: 80: @parser.on('-F', '--wf', '--with-fact fact=val', 'Match hosts with a certain fact') do |f| 81: fact_parsed = parse_fact(f) 82: 83: @options[:filter]["fact"] << fact_parsed if fact_parsed 84: end 85: 86: @parser.on('-C', '--wc', '--with-class CLASS', 'Match hosts with a certain config management class') do |f| 87: @options[:filter]["cf_class"] << f 88: end 89: 90: @parser.on('-A', '--wa', '--with-agent AGENT', 'Match hosts with a certain agent') do |a| 91: @options[:filter]["agent"] << a 92: end 93: 94: @parser.on('-I', '--wi', '--with-identity IDENT', 'Match hosts with a certain configured identity') do |a| 95: @options[:filter]["identity"] << a 96: end 97: 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