class Puppet::Util::CommandLine::PuppetOptionParser
This is a command line option parser. It is intended to have an API that is very similar to
the ruby stdlib 'OptionParser' API, for ease of integration into our existing code... however, However, we've removed the OptionParser-based implementation and are only maintaining the it's implemented based on the third-party "trollop" library. This was done because there are places where the stdlib OptionParser is not flexible enough to meet our needs.
Attributes
ignore_invalid_options[R]
This parameter, if set, will tell the underlying option parser not to throw an
exception if we pass it options that weren't explicitly registered. We need this capability because we need to be able to pass all of the command-line options before we know which application/face they are going to be running, but the app/face may specify additional command-line arguments that are valid for that app/face.
Public Class Methods
new(usage_msg = nil)
click to toggle source
# File lib/puppet/util/command_line/puppet_option_parser.rb 19 def initialize(usage_msg = nil) 20 require_relative '../../../puppet/util/command_line/trollop' 21 22 @create_default_short_options = false 23 24 @parser = Trollop::Parser.new do 25 banner usage_msg 26 end 27 28 end
Public Instance Methods
ignore_invalid_options=(value)
click to toggle source
# File lib/puppet/util/command_line/puppet_option_parser.rb 37 def ignore_invalid_options=(value) 38 @parser.ignore_invalid_options = value 39 end
on(*args, &block)
click to toggle source
# File lib/puppet/util/command_line/puppet_option_parser.rb 41 def on(*args, &block) 42 # The 2nd element is an optional "short" representation. 43 if args.length == 3 44 long, desc, type = args 45 elsif args.length == 4 46 long, short, desc, type = args 47 else 48 raise ArgumentError, _("this method only takes 3 or 4 arguments. Given: %{args}") % { args: args.inspect } 49 end 50 51 options = { 52 :long => long, 53 :short => short, 54 :required => false, 55 :callback => pass_only_last_value_on_to(block), 56 :multi => true, 57 } 58 59 case type 60 when :REQUIRED 61 options[:type] = :string 62 when :NONE 63 options[:type] = :flag 64 else 65 raise PuppetOptionError.new(_("Unsupported type: '%{type}'") % { type: type }) 66 end 67 68 @parser.opt long.sub("^--", "").intern, desc, options 69 end
parse(*args)
click to toggle source
# File lib/puppet/util/command_line/puppet_option_parser.rb 71 def parse(*args) 72 args = args[0] if args.size == 1 and Array === args[0] 73 args_copy = args.dup 74 begin 75 @parser.parse args_copy 76 rescue Puppet::Util::CommandLine::Trollop::CommandlineError => err 77 raise PuppetOptionError.new(_("Error parsing arguments"), err) 78 end 79 end
Private Instance Methods
pass_only_last_value_on_to(block)
click to toggle source
# File lib/puppet/util/command_line/puppet_option_parser.rb 81 def pass_only_last_value_on_to(block) 82 lambda { |values| block.call(values.is_a?(Array) ? values.last : values) } 83 end