class Puppet::Interface::Option

This represents an option on an action or face (to be globally applied to its actions). Options should be constructed by calling {Puppet::Interface::OptionManager#option}, which is available on {Puppet::Interface}, and then calling methods of {Puppet::Interface::OptionBuilder} in the supplied block. @api public

Attributes

after_action[RW]
aliases[R]
before_action[RW]
name[R]
optparse[R]
parent[R]
required[RW]

Public Class Methods

new(parent, *declaration, &block) click to toggle source

@api private

   # File lib/puppet/interface/option.rb
11 def initialize(parent, *declaration, &block)
12   @parent   = parent
13   @optparse = []
14   @default  = nil
15 
16   # Collect and sort the arguments in the declaration.
17   dups = {}
18   declaration.each do |item|
19     if item.is_a? String and item.to_s =~ /^-/ then
20       unless item =~ /^-[a-z]\b/ or item =~ /^--[^-]/ then
21         raise ArgumentError, _("%{option}: long options need two dashes (--)") % { option: item.inspect }
22       end
23       @optparse << item
24 
25       # Duplicate checking...
26       # for our duplicate checking purpose, we don't make a check with the
27       # translated '-' -> '_'. Right now, we do that on purpose because of
28       # a duplicated option made publicly available on certificate and ca
29       # faces for dns alt names. Puppet defines 'dns_alt_names', those
30       # faces include 'dns-alt-names'.  We can't get rid of 'dns-alt-names'
31       # yet, so we need to do our duplicate checking on the untranslated
32       # version of the option.
33       # jeffweiss 17 april 2012
34       name = optparse_to_optionname(item)
35       if Puppet.settings.include? name then
36         raise ArgumentError, _("%{option}: already defined in puppet") % { option: item.inspect }
37       end
38       dup = dups[name]
39       if dup
40         raise ArgumentError, _("%{option}: duplicates existing alias %{duplicate} in %{parent}") %
41             { option: item.inspect, duplicate: dup.inspect, parent: @parent }
42       else
43         dups[name] = item
44       end
45     else
46       raise ArgumentError, _("%{option} is not valid for an option argument") % { option: item.inspect }
47     end
48   end
49 
50   if @optparse.empty? then
51     raise ArgumentError, _("No option declarations found while building")
52   end
53 
54   # Now, infer the name from the options; we prefer the first long option as
55   # the name, rather than just the first option.
56   @name = optparse_to_name(@optparse.find do |a| a =~ /^--/ end || @optparse.first)
57   @aliases = @optparse.map { |o| optparse_to_name(o) }
58 
59   # Do we take an argument?  If so, are we consistent about it, because
60   # incoherence here makes our life super-difficult, and we can more easily
61   # relax this rule later if we find a valid use case for it. --daniel 2011-03-30
62   @argument = @optparse.any? { |o| o =~ /[ =]/ }
63   if @argument and not @optparse.all? { |o| o =~ /[ =]/ } then
64     raise ArgumentError, _("Option %{name} is inconsistent about taking an argument") % { name: @name }
65   end
66 
67   # Is our argument optional?  The rules about consistency apply here, also,
68   # just like they do to taking arguments at all. --daniel 2011-03-30
69   @optional_argument = @optparse.any? { |o| o=~/[ =]\[/ }
70   if @optional_argument
71     raise ArgumentError, _("Options with optional arguments are not supported")
72   end
73   if @optional_argument and not @optparse.all? { |o| o=~/[ =]\[/ } then
74     raise ArgumentError, _("Option %{name} is inconsistent about the argument being optional") % { name: @name }
75   end
76 end

Public Instance Methods

__decoration_name(type) click to toggle source
    # File lib/puppet/interface/option.rb
166 def __decoration_name(type)
167   if @parent.is_a? Puppet::Interface::Action then
168     :"option #{name} from #{parent.name} #{type} decoration"
169   else
170     :"option #{name} #{type} decoration"
171   end
172 end
after_action=(proc) click to toggle source
    # File lib/puppet/interface/option.rb
156 def after_action=(proc)
157   unless proc.is_a? Proc
158     #TRANSLATORS 'proc' is a Ruby block of code
159     raise ArgumentError, _("after action hook for %{name} is a %{class_name}, not a proc") %
160         { name: self, class_name: proc.class.name.inspect }
161   end
162   @after_action =
163     @parent.__send__(:__add_method, __decoration_name(:after), proc)
164 end
before_action=(proc) click to toggle source
    # File lib/puppet/interface/option.rb
145 def before_action=(proc)
146   unless proc.is_a? Proc
147     #TRANSLATORS 'proc' is a Ruby block of code
148     raise ArgumentError, _("before action hook for %{name} is a %{class_name}, not a proc") %
149         { name: self, class_name: proc.class.name.inspect }
150   end
151   @before_action =
152     @parent.__send__(:__add_method, __decoration_name(:before), proc)
153 end
default() click to toggle source
    # File lib/puppet/interface/option.rb
131 def default
132   @default and @default.call
133 end
default=(proc) click to toggle source
    # File lib/puppet/interface/option.rb
119 def default=(proc)
120   if required
121     raise ArgumentError, _("%{name} can't be optional and have a default value") % { name: self }
122   end
123   unless proc.is_a? Proc
124     #TRANSLATORS 'proc' is a Ruby block of code
125     raise ArgumentError, _("default value for %{name} is a %{class_name}, not a proc") %
126         { name: self, class_name: proc.class.name.inspect }
127   end
128   @default = proc
129 end
has_default?() click to toggle source
    # File lib/puppet/interface/option.rb
115 def has_default?
116   !!@default
117 end
optional_argument?() click to toggle source
    # File lib/puppet/interface/option.rb
108 def optional_argument?
109   !!@optional_argument
110 end
optparse_to_name(declaration) click to toggle source

@api private

    # File lib/puppet/interface/option.rb
 96 def optparse_to_name(declaration)
 97   name = optparse_to_optionname(declaration).tr('-', '_')
 98   unless name.to_s =~ /^[a-z]\w*$/
 99     raise _("%{name} is an invalid option name") % { name: name.inspect }
100   end
101   name.to_sym
102 end
optparse_to_optionname(declaration) click to toggle source

@api private

   # File lib/puppet/interface/option.rb
87 def optparse_to_optionname(declaration)
88   found = declaration.match(/^-+(?:\[no-\])?([^ =]+)/)
89   unless found
90     raise ArgumentError, _("Can't find a name in the declaration %{declaration}") % { declaration: declaration.inspect }
91   end
92   found.captures.first
93 end
required=(value) click to toggle source
    # File lib/puppet/interface/option.rb
137 def required=(value)
138   if has_default?
139     raise ArgumentError, _("%{name} can't be optional and have a default value") % { name: self }
140   end
141   @required = value
142 end
required?() click to toggle source
    # File lib/puppet/interface/option.rb
111 def required?
112   !!@required
113 end
takes_argument?() click to toggle source
    # File lib/puppet/interface/option.rb
105 def takes_argument?
106   !!@argument
107 end
to_s() click to toggle source

to_s and optparse_to_name are roughly mirrored, because they are used to transform options to name symbols, and vice-versa. This isn't a full bidirectional transformation though. –daniel 2011-04-07

   # File lib/puppet/interface/option.rb
82 def to_s
83   @name.to_s.tr('_', '-')
84 end