Class: Puppet::Interface::Option
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from TinyDocs
#build_synopsis, #description, #summary
Methods included from DocGen
#attr_doc, strip_whitespace
Constructor Details
#initialize(parent, *declaration, &block) ⇒ Option
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Option.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/puppet/interface/option.rb', line 11
def initialize(parent, *declaration, &block)
@parent = parent
@optparse = []
@default = nil
dups = {}
declaration.each do |item|
if item.is_a? String and item.to_s =~ /^-/ then
unless item =~ /^-[a-z]\b/ or item =~ /^--[^-]/ then
raise ArgumentError, "#{item.inspect}: long options need two dashes (--)"
end
@optparse << item
name = optparse_to_optionname(item)
if Puppet.settings.include? name then
raise ArgumentError, "#{item.inspect}: already defined in puppet"
end
if dup = dups[name] then
raise ArgumentError, "#{item.inspect}: duplicates existing alias #{dup.inspect} in #{@parent}"
else
dups[name] = item
end
else
raise ArgumentError, "#{item.inspect} is not valid for an option argument"
end
end
if @optparse.empty? then
raise ArgumentError, "No option declarations found while building"
end
@name = optparse_to_name(@optparse.find do |a| a =~ /^--/ end || @optparse.first)
@aliases = @optparse.map { |o| optparse_to_name(o) }
@argument = @optparse.any? { |o| o =~ /[ =]/ }
if @argument and not @optparse.all? { |o| o =~ /[ =]/ } then
raise ArgumentError, "Option #{@name} is inconsistent about taking an argument"
end
@optional_argument = @optparse.any? { |o| o=~/[ =]\[/ }
@optional_argument and raise ArgumentError, "Options with optional arguments are not supported"
if @optional_argument and not @optparse.all? { |o| o=~/[ =]\[/ } then
raise ArgumentError, "Option #{@name} is inconsistent about the argument being optional"
end
end
|
Instance Attribute Details
#after_action ⇒ Object
136
137
138
|
# File 'lib/puppet/interface/option.rb', line 136
def after_action
@after_action
end
|
122
123
124
|
# File 'lib/puppet/interface/option.rb', line 122
def aliases
@aliases
end
|
#before_action ⇒ Object
129
130
131
|
# File 'lib/puppet/interface/option.rb', line 129
def before_action
@before_action
end
|
122
123
124
|
# File 'lib/puppet/interface/option.rb', line 122
def name
@name
end
|
122
123
124
|
# File 'lib/puppet/interface/option.rb', line 122
def optparse
@optparse
end
|
122
123
124
|
# File 'lib/puppet/interface/option.rb', line 122
def parent
@parent
end
|
123
124
125
|
# File 'lib/puppet/interface/option.rb', line 123
def required
@required
end
|
Instance Method Details
#__decoration_name(type) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/puppet/interface/option.rb', line 143
def __decoration_name(type)
if @parent.is_a? Puppet::Interface::Action then
:"option #{name} from #{parent.name} #{type} decoration"
else
:"option #{name} #{type} decoration"
end
end
|
118
119
120
|
# File 'lib/puppet/interface/option.rb', line 118
def default
@default and @default.call
end
|
#default=(proc) ⇒ Object
112
113
114
115
116
|
# File 'lib/puppet/interface/option.rb', line 112
def default=(proc)
required and raise ArgumentError, "#{self} can't be optional and have a default value"
proc.is_a? Proc or raise ArgumentError, "default value for #{self} is a #{proc.class.name.inspect}, not a proc"
@default = proc
end
|
#has_default? ⇒ Boolean
108
109
110
|
# File 'lib/puppet/interface/option.rb', line 108
def has_default?
!!@default
end
|
#optional_argument? ⇒ Boolean
101
102
103
|
# File 'lib/puppet/interface/option.rb', line 101
def optional_argument?
!!@optional_argument
end
|
#optparse_to_name(declaration) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
91
92
93
94
95
|
# File 'lib/puppet/interface/option.rb', line 91
def optparse_to_name(declaration)
name = optparse_to_optionname(declaration).tr('-', '_')
raise "#{name.inspect} is an invalid option name" unless name.to_s =~ /^[a-z]\w*$/
name.to_sym
end
|
#optparse_to_optionname(declaration) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
83
84
85
86
87
88
|
# File 'lib/puppet/interface/option.rb', line 83
def optparse_to_optionname(declaration)
unless found = declaration.match(/^-+(?:\[no-\])?([^ =]+)/) then
raise ArgumentError, "Can't find a name in the declaration #{declaration.inspect}"
end
found.captures.first
end
|
#required? ⇒ Boolean
104
105
106
|
# File 'lib/puppet/interface/option.rb', line 104
def required?
!!@required
end
|
#takes_argument? ⇒ Boolean
98
99
100
|
# File 'lib/puppet/interface/option.rb', line 98
def takes_argument?
!!@argument
end
|
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
78
79
80
|
# File 'lib/puppet/interface/option.rb', line 78
def to_s
@name.to_s.tr('_', '-')
end
|