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[R]
aliases[R]
before_action[R]
name[R]
optparse[R]
parent[R]
required[R]
Public Class Methods
new(parent, *declaration, &block)
click to toggle source
@api private
# File lib/puppet/interface/option.rb 12 def initialize(parent, *declaration, &block) 13 @parent = parent 14 @optparse = [] 15 @default = nil 16 17 # Collect and sort the arguments in the declaration. 18 dups = {} 19 declaration.each do |item| 20 if item.is_a? String and item.to_s =~ /^-/ then 21 unless item =~ /^-[a-z]\b/ or item =~ /^--[^-]/ then 22 raise ArgumentError, _("%{option}: long options need two dashes (--)") % { option: item.inspect } 23 end 24 @optparse << item 25 26 # Duplicate checking... 27 # for our duplicate checking purpose, we don't make a check with the 28 # translated '-' -> '_'. Right now, we do that on purpose because of 29 # a duplicated option made publicly available on certificate and ca 30 # faces for dns alt names. Puppet defines 'dns_alt_names', those 31 # faces include 'dns-alt-names'. We can't get rid of 'dns-alt-names' 32 # yet, so we need to do our duplicate checking on the untranslated 33 # version of the option. 34 # jeffweiss 17 april 2012 35 name = optparse_to_optionname(item) 36 if Puppet.settings.include? name then 37 raise ArgumentError, _("%{option}: already defined in puppet") % { option: item.inspect } 38 end 39 dup = dups[name] 40 if dup 41 raise ArgumentError, _("%{option}: duplicates existing alias %{duplicate} in %{parent}") % 42 { option: item.inspect, duplicate: dup.inspect, parent: @parent } 43 else 44 dups[name] = item 45 end 46 else 47 raise ArgumentError, _("%{option} is not valid for an option argument") % { option: item.inspect } 48 end 49 end 50 51 if @optparse.empty? then 52 raise ArgumentError, _("No option declarations found while building") 53 end 54 55 # Now, infer the name from the options; we prefer the first long option as 56 # the name, rather than just the first option. 57 @name = optparse_to_name(@optparse.find do |a| a =~ /^--/ end || @optparse.first) 58 @aliases = @optparse.map { |o| optparse_to_name(o) } 59 60 # Do we take an argument? If so, are we consistent about it, because 61 # incoherence here makes our life super-difficult, and we can more easily 62 # relax this rule later if we find a valid use case for it. --daniel 2011-03-30 63 @argument = @optparse.any? { |o| o =~ /[ =]/ } 64 if @argument and not @optparse.all? { |o| o =~ /[ =]/ } then 65 raise ArgumentError, _("Option %{name} is inconsistent about taking an argument") % { name: @name } 66 end 67 68 # Is our argument optional? The rules about consistency apply here, also, 69 # just like they do to taking arguments at all. --daniel 2011-03-30 70 @optional_argument = @optparse.any? { |o| o=~/[ =]\[/ } 71 if @optional_argument 72 raise ArgumentError, _("Options with optional arguments are not supported") 73 end 74 if @optional_argument and not @optparse.all? { |o| o=~/[ =]\[/ } then 75 raise ArgumentError, _("Option %{name} is inconsistent about the argument being optional") % { name: @name } 76 end 77 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 132 def default 133 @default and @default.call 134 end
default=(proc)
click to toggle source
# File lib/puppet/interface/option.rb 120 def default=(proc) 121 if required 122 raise ArgumentError, _("%{name} can't be optional and have a default value") % { name: self } 123 end 124 unless proc.is_a? Proc 125 #TRANSLATORS 'proc' is a Ruby block of code 126 raise ArgumentError, _("default value for %{name} is a %{class_name}, not a proc") % 127 { name: self, class_name: proc.class.name.inspect } 128 end 129 @default = proc 130 end
has_default?()
click to toggle source
# File lib/puppet/interface/option.rb 116 def has_default? 117 !!@default 118 end
optional_argument?()
click to toggle source
# File lib/puppet/interface/option.rb 109 def optional_argument? 110 !!@optional_argument 111 end
optparse_to_name(declaration)
click to toggle source
@api private
# File lib/puppet/interface/option.rb 97 def optparse_to_name(declaration) 98 name = optparse_to_optionname(declaration).tr('-', '_') 99 unless name.to_s =~ /^[a-z]\w*$/ 100 raise _("%{name} is an invalid option name") % { name: name.inspect } 101 end 102 name.to_sym 103 end
optparse_to_optionname(declaration)
click to toggle source
@api private
# File lib/puppet/interface/option.rb 88 def optparse_to_optionname(declaration) 89 found = declaration.match(/^-+(?:\[no-\])?([^ =]+)/) 90 unless found 91 raise ArgumentError, _("Can't find a name in the declaration %{declaration}") % { declaration: declaration.inspect } 92 end 93 found.captures.first 94 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 112 def required? 113 !!@required 114 end
takes_argument?()
click to toggle source
# File lib/puppet/interface/option.rb 106 def takes_argument? 107 !!@argument 108 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 83 def to_s 84 @name.to_s.tr('_', '-') 85 end