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
18 def initialize(usage_msg = nil)
19   require_relative '../../../puppet/util/command_line/trollop'
20 
21   @create_default_short_options = false
22 
23   @parser = Trollop::Parser.new do
24     banner usage_msg
25   end
26 
27 end

Public Instance Methods

ignore_invalid_options=(value) click to toggle source
   # File lib/puppet/util/command_line/puppet_option_parser.rb
36 def ignore_invalid_options=(value)
37   @parser.ignore_invalid_options = value
38 end
on(*args, &block) click to toggle source
   # File lib/puppet/util/command_line/puppet_option_parser.rb
40 def on(*args, &block)
41   # The 2nd element is an optional "short" representation.
42   if args.length == 3
43     long, desc, type = args
44   elsif args.length == 4
45     long, short, desc, type = args
46   else
47     raise ArgumentError, _("this method only takes 3 or 4 arguments. Given: %{args}") % { args: args.inspect }
48   end
49 
50   options = {
51       :long => long,
52       :short => short,
53       :required => false,
54       :callback => pass_only_last_value_on_to(block),
55       :multi => true,
56   }
57 
58   case type
59     when :REQUIRED
60       options[:type] = :string
61     when :NONE
62       options[:type] = :flag
63     else
64       raise PuppetOptionError.new(_("Unsupported type: '%{type}'") % { type: type })
65   end
66 
67   @parser.opt long.sub("^--", "").intern, desc, options
68 end
parse(*args) click to toggle source
   # File lib/puppet/util/command_line/puppet_option_parser.rb
70 def parse(*args)
71   args = args[0] if args.size == 1 and Array === args[0]
72   args_copy = args.dup
73   begin
74     @parser.parse args_copy
75   rescue Puppet::Util::CommandLine::Trollop::CommandlineError => err
76     raise PuppetOptionError.new(_("Error parsing arguments"), err)
77   end
78 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
80 def pass_only_last_value_on_to(block)
81   lambda { |values| block.call(values.is_a?(Array) ? values.last : values) }
82 end