summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/option_manager.rb
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-07 15:53:17 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-07 15:53:17 -0700
commita35fa519c85a761ac8bed3be8fde2d6523cae474 (patch)
tree36f3583ee364ba1d68467a2b614a8dfcf9ed43ae /lib/puppet/interface/option_manager.rb
parent3d5ec4f61fee08552767e950ac021752c202a599 (diff)
parent87ed3188e65d3f5f9c2c32a409b271d1b39684b9 (diff)
downloadpuppet-a35fa519c85a761ac8bed3be8fde2d6523cae474.tar.gz
puppet-a35fa519c85a761ac8bed3be8fde2d6523cae474.tar.xz
puppet-a35fa519c85a761ac8bed3be8fde2d6523cae474.zip
Merge branch 'refactor/master/7012-rename-strings-to-interfaces-and-faces'
Diffstat (limited to 'lib/puppet/interface/option_manager.rb')
-rw-r--r--lib/puppet/interface/option_manager.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/puppet/interface/option_manager.rb b/lib/puppet/interface/option_manager.rb
new file mode 100644
index 000000000..56df9760f
--- /dev/null
+++ b/lib/puppet/interface/option_manager.rb
@@ -0,0 +1,56 @@
+require 'puppet/interface/option_builder'
+
+module Puppet::Interface::OptionManager
+ # Declare that this app can take a specific option, and provide
+ # the code to do so.
+ def option(*declaration, &block)
+ add_option Puppet::Interface::OptionBuilder.build(self, *declaration, &block)
+ end
+
+ def add_option(option)
+ 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
+
+ option.aliases.each { |name| @options[name] = option }
+ option
+ end
+
+ def options
+ @options ||= {}
+ result = @options.keys
+
+ if self.is_a?(Class) and superclass.respond_to?(:options)
+ result += superclass.options
+ elsif self.class.respond_to?(:options)
+ result += self.class.options
+ end
+ result.sort
+ end
+
+ def get_option(name)
+ @options ||= {}
+ result = @options[name.to_sym]
+ unless result 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
+
+ def option?(name)
+ options.include? name.to_sym
+ end
+end