summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/option_builder.rb
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-07-20 11:58:55 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-07-20 17:06:34 -0700
commitb75b1c19ecf6c278b065d203ac8486fa598caa8b (patch)
tree14378b3efc4a99db382ca4bf035967001c0ed646 /lib/puppet/interface/option_builder.rb
parentda504b3a0004666e9014ffbd0bf108acf07f9dce (diff)
downloadpuppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.tar.gz
puppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.tar.xz
puppet-b75b1c19ecf6c278b065d203ac8486fa598caa8b.zip
(#6787) Add `default_to` for options.
This implement support for options with default values, allowing faces to set those values when not invoked. This can eliminate substantial duplicate code from actions, especially when there are face-level options in use. Reviewed-By: Pieter van de Bruggen <pieter@puppetlabs.com>
Diffstat (limited to 'lib/puppet/interface/option_builder.rb')
-rw-r--r--lib/puppet/interface/option_builder.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/puppet/interface/option_builder.rb b/lib/puppet/interface/option_builder.rb
index 5676ec977..c87adc2c0 100644
--- a/lib/puppet/interface/option_builder.rb
+++ b/lib/puppet/interface/option_builder.rb
@@ -51,4 +51,17 @@ class Puppet::Interface::OptionBuilder
def required(value = true)
@option.required = value
end
+
+ def default_to(&block)
+ block or raise ArgumentError, "#{@option} default_to requires a block"
+ if @option.has_default?
+ raise ArgumentError, "#{@option} already has a default value"
+ end
+ # Ruby 1.8 treats a block without arguments as accepting any number; 1.9
+ # gets this right, so we work around it for now... --daniel 2011-07-20
+ unless block.arity == 0 or (RUBY_VERSION =~ /^1\.8/ and block.arity == -1)
+ raise ArgumentError, "#{@option} default_to block should not take any arguments"
+ end
+ @option.default = block
+ end
end