diff options
-rw-r--r-- | lib/puppet/util/settings.rb | 68 | ||||
-rwxr-xr-x | spec/unit/util/settings.rb | 16 |
2 files changed, 47 insertions, 37 deletions
diff --git a/lib/puppet/util/settings.rb b/lib/puppet/util/settings.rb index c60f1713b..f9b9345f3 100644 --- a/lib/puppet/util/settings.rb +++ b/lib/puppet/util/settings.rb @@ -22,29 +22,7 @@ class Puppet::Util::Settings # Set a config value. This doesn't set the defaults, it sets the value itself. def []=(param, value) - param = param.to_sym - unless element = @config[param] - raise ArgumentError, - "Attempt to assign a value to unknown configuration parameter %s" % param.inspect - end - if element.respond_to?(:munge) - value = element.munge(value) - end - if element.respond_to?(:handle) - element.handle(value) - end - # Reset the name, so it's looked up again. - if param == :name - @name = nil - end - @sync.synchronize do # yay, thread-safe - @values[:memory][param] = value - @cache.clear - - clearused - end - - return value + set_value(param, value, :memory) end # Generate the list of valid arguments, in a format that GetoptLong can @@ -156,7 +134,7 @@ class Puppet::Util::Settings end # Handle a command-line argument. - def handlearg(opt, value = nil) + def handlearg(opt, value) @cache.clear value = munge_value(value) if value str = opt.sub(/^--/,'') @@ -167,17 +145,12 @@ class Puppet::Util::Settings bool = false end str = str.intern - if self.valid?(str) - @sync.synchronize do - if self.boolean?(str) - @values[:cli][str] = bool - else - @values[:cli][str] = value - end - end - else - raise ArgumentError, "Invalid argument %s" % opt + + if value == "" + value = bool end + + set_value(str, value, :cli) end def include?(name) @@ -488,6 +461,33 @@ class Puppet::Util::Settings return sectionlist, sections end + def set_value(param, value, type) + param = param.to_sym + unless element = @config[param] + raise ArgumentError, + "Attempt to assign a value to unknown configuration parameter %s" % param.inspect + end + if element.respond_to?(:munge) + value = element.munge(value) + end + if element.respond_to?(:handle) + element.handle(value) + end + # Reset the name, so it's looked up again. + if param == :name + @name = nil + end + @sync.synchronize do # yay, thread-safe + @values[type][param] = value + @cache.clear + clearused + end + + return value + end + + private :set_value + # Set a bunch of defaults in a given section. The sections are actually pretty # pointless, but they help break things up a bit, anyway. def setdefaults(section, defs) diff --git a/spec/unit/util/settings.rb b/spec/unit/util/settings.rb index c1d74a88b..6f92fae46 100755 --- a/spec/unit/util/settings.rb +++ b/spec/unit/util/settings.rb @@ -79,14 +79,14 @@ describe Puppet::Util::Settings do end it "should support a getopt-specific mechanism for turning booleans off" do - @settings.handlearg("--no-bool") + @settings.handlearg("--no-bool", "") @settings[:bool].should == false end it "should support a getopt-specific mechanism for turning booleans on" do # Turn it off first @settings[:bool] = false - @settings.handlearg("--bool") + @settings.handlearg("--bool", "") @settings[:bool].should == true end @@ -99,7 +99,7 @@ describe Puppet::Util::Settings do it "should not clear other values when setting getopt-specific values" do @settings[:myval] = "yay" - @settings.handlearg("--no-bool") + @settings.handlearg("--no-bool", "") @settings[:myval].should == "yay" end @@ -117,6 +117,16 @@ describe Puppet::Util::Settings do values.should == %w{something} end + it "should call passed blocks when values are set via the command line" do + values = [] + @settings.setdefaults(:section, :hooker => {:default => "yay", :desc => "boo", :hook => lambda { |v| values << v }}) + values.should == [] + + @settings.handlearg("--hooker", "yay") + + values.should == %w{yay} + end + it "should provide an option to call passed blocks during definition" do values = [] @settings.setdefaults(:section, :hooker => {:default => "yay", :desc => "boo", :call_on_define => true, :hook => lambda { |v| values << v }}) |