Class: Puppet::Interface::Option

Inherits:
Object
  • Object
show all
Includes:
TinyDocs
Defined in:
lib/vendor/puppet/interface/option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TinyDocs

#build_synopsis

Methods included from DocGen

#attr_doc, strip_whitespace

Constructor Details

#initialize(parent, *declaration, &block) ⇒ Option

Returns a new instance of Option.



6
7
8
9
10
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
# File 'lib/vendor/puppet/interface/option.rb', line 6

def initialize(parent, *declaration, &block)
  @parent   = parent
  @optparse = []
  @default  = nil

  # Collect and sort the arguments in the declaration.
  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

      # Duplicate checking...
      name = optparse_to_name(item)
      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

  # Now, infer the name from the options; we prefer the first long option as
  # the name, rather than just the first option.
  @name = optparse_to_name(@optparse.find do |a| a =~ /^--/ end || @optparse.first)
  @aliases = @optparse.map { |o| optparse_to_name(o) }

  # Do we take an argument?  If so, are we consistent about it, because
  # incoherence here makes our life super-difficult, and we can more easily
  # relax this rule later if we find a valid use case for it. --daniel 2011-03-30
  @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

  # Is our argument optional?  The rules about consistency apply here, also,
  # just like they do to taking arguments at all. --daniel 2011-03-30
  @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_actionObject

Returns the value of attribute after_action.



113
114
115
# File 'lib/vendor/puppet/interface/option.rb', line 113

def after_action
  @after_action
end

#aliasesObject (readonly)

Returns the value of attribute aliases.



99
100
101
# File 'lib/vendor/puppet/interface/option.rb', line 99

def aliases
  @aliases
end

#before_actionObject

Returns the value of attribute before_action.



106
107
108
# File 'lib/vendor/puppet/interface/option.rb', line 106

def before_action
  @before_action
end

#nameObject (readonly)

Returns the value of attribute name.



99
100
101
# File 'lib/vendor/puppet/interface/option.rb', line 99

def name
  @name
end

#optparseObject (readonly)

Returns the value of attribute optparse.



99
100
101
# File 'lib/vendor/puppet/interface/option.rb', line 99

def optparse
  @optparse
end

#parentObject (readonly)

Returns the value of attribute parent.



99
100
101
# File 'lib/vendor/puppet/interface/option.rb', line 99

def parent
  @parent
end

#requiredObject

Returns the value of attribute required.



100
101
102
# File 'lib/vendor/puppet/interface/option.rb', line 100

def required
  @required
end

Instance Method Details

#__decoration_name(type) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/vendor/puppet/interface/option.rb', line 120

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

#defaultObject



95
96
97
# File 'lib/vendor/puppet/interface/option.rb', line 95

def default
  @default and @default.call
end

#default=(proc) ⇒ Object



89
90
91
92
93
# File 'lib/vendor/puppet/interface/option.rb', line 89

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

Returns:

  • (Boolean)


85
86
87
# File 'lib/vendor/puppet/interface/option.rb', line 85

def has_default?
  !!@default
end

#optional_argument?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/vendor/puppet/interface/option.rb', line 78

def optional_argument?
  !!@optional_argument
end

#optparse_to_name(declaration) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/vendor/puppet/interface/option.rb', line 65

def optparse_to_name(declaration)
  unless found = declaration.match(/^-+(?:\[no-\])?([^ =]+)/) then
    raise ArgumentError, "Can't find a name in the declaration #{declaration.inspect}"
  end
  name = found.captures.first.tr('-', '_')
  raise "#{name.inspect} is an invalid option name" unless name.to_s =~ /^[a-z]\w*$/
  name.to_sym
end

#required?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/vendor/puppet/interface/option.rb', line 81

def required?
  !!@required
end

#takes_argument?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/vendor/puppet/interface/option.rb', line 75

def takes_argument?
  !!@argument
end

#to_sObject

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



61
62
63
# File 'lib/vendor/puppet/interface/option.rb', line 61

def to_s
  @name.to_s.tr('_', '-')
end