Module: Puppet::Interface::OptionManager

Included in:
Puppet::Interface, Puppet::Interface
Defined in:
lib/vendor/puppet/interface/option_manager.rb

Instance Method Summary collapse

Instance Method Details

#add_option(option) ⇒ Object



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

def add_option(option)
  # @options collects the added options in the order they're declared.
  # @options_hash collects the options keyed by alias for quick lookups.
  @options      ||= []
  @options_hash ||= {}

  option.aliases.each do |name|
    if conflict = get_option(name) then
      raise ArgumentError, "Option #{option} conflicts with existing option #{conflict}"
    end

    actions.each do |action|
      action = get_action(action)
      if conflict = action.get_option(name) then
        raise ArgumentError, "Option #{option} conflicts with existing option #{conflict} on #{action}"
      end
    end
  end

  @options << option.name

  option.aliases.each do |name|
    @options_hash[name] = option
  end

  return option
end

#get_option(name, with_inherited_options = true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vendor/puppet/interface/option_manager.rb', line 50

def get_option(name, with_inherited_options = true)
  @options_hash ||= {}

  result = @options_hash[name.to_sym]
  if result.nil? and with_inherited_options then
    if self.is_a?(Class) and superclass.respond_to?(:get_option)
      result = superclass.get_option(name)
    elsif self.class.respond_to?(:get_option)
      result = self.class.get_option(name)
    end
  end

  return result
end

#option(*declaration, &block) ⇒ Object

Declare that this app can take a specific option, and provide the code to do so.



6
7
8
# File 'lib/vendor/puppet/interface/option_manager.rb', line 6

def option(*declaration, &block)
  add_option Puppet::Interface::OptionBuilder.build(self, *declaration, &block)
end

#option?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/vendor/puppet/interface/option_manager.rb', line 65

def option?(name)
  options.include? name.to_sym
end

#optionsObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vendor/puppet/interface/option_manager.rb', line 38

def options
  result = (@options ||= [])

  if self.is_a?(Class) and superclass.respond_to?(:options)
    result = superclass.options + result
  elsif self.class.respond_to?(:options)
    result = self.class.options + result
  end

  return result
end