[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Daniel Pittman daniel at puppetlabs.com
Tue May 10 08:11:11 UTC 2011


The following commit has been merged in the experimental branch:
commit 7e7d246bf46349c904c76a31951d4a40c200790b
Author: Daniel Pittman <daniel at puppetlabs.com>
Date:   Tue Apr 5 11:37:51 2011 -0700

    (#6972) Recognize puppet global options in pre-parse.
    
    This extends the CLI pre-parse phase to identify both string *and* global
    options out of the Puppet settings/defaults system.  This makes the regular
    CLI support for setting Puppet configuration globals work as expected.
    
    This moves us along the line of supporting these options more fully.
    
    Reviewed-By: Dan Bode <dan at puppetlabs.com>

diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb
index 76b0a46..09d02c0 100644
--- a/lib/puppet/application/string_base.rb
+++ b/lib/puppet/application/string_base.rb
@@ -68,7 +68,9 @@ class Puppet::Application::StringBase < Puppet::Application
     until @action or (index += 1) >= command_line.args.length do
       item = command_line.args[index]
       if item =~ /^-/ then
-        option = @string.options.find { |a| item =~ /^-+#{a}\b/ }
+        option = @string.options.find do |name|
+          item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/
+        end
         if option then
           option = @string.get_option(option)
           # If we have an inline argument, just carry on.  We don't need to
@@ -79,6 +81,12 @@ class Puppet::Application::StringBase < Puppet::Application
             index += 1 unless
               (option.optional_argument? and command_line.args[index + 1] =~ /^-/)
           end
+        elsif option = find_global_settings_argument(item) then
+          unless Puppet.settings.boolean? option.name then
+            # As far as I can tell, we treat non-bool options as always having
+            # a mandatory argument. --daniel 2011-04-05
+            index += 1          # ...so skip the argument.
+          end
         else
           raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}"
         end
@@ -101,6 +109,18 @@ class Puppet::Application::StringBase < Puppet::Application
     end
   end
 
+  def find_global_settings_argument(item)
+    Puppet.settings.each do |name, object|
+      object.optparse_args.each do |arg|
+        next unless arg =~ /^-/
+        # sadly, we have to emulate some of optparse here...
+        pattern = /^#{arg.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/
+        pattern.match item and return object
+      end
+    end
+    return nil                  # nothing found.
+  end
+
   def setup
     Puppet::Util::Log.newdestination :console
 
diff --git a/spec/unit/application/string_base_spec.rb b/spec/unit/application/string_base_spec.rb
index cd24b6c..3f8ae73 100755
--- a/spec/unit/application/string_base_spec.rb
+++ b/spec/unit/application/string_base_spec.rb
@@ -42,6 +42,15 @@ describe Puppet::Application::StringBase do
     app
   end
 
+  describe "#find_global_settings_argument" do
+    it "should not match --ca to --ca-location" do
+      option = mock('ca option', :optparse_args => ["--ca"])
+      Puppet.settings.expects(:each).yields(:ca, option)
+
+      app.find_global_settings_argument("--ca-location").should be_nil
+    end
+  end
+
   describe "#preinit" do
     before :each do
       app.command_line.stubs(:args).returns %w{}
@@ -118,6 +127,26 @@ describe Puppet::Application::StringBase do
         expect { app.preinit }.
           should raise_error ArgumentError, /Unknown option "--bar"/
       end
+
+      { "boolean options before" => %w{--trace foo},
+        "boolean options after"  => %w{foo --trace}
+      }.each do |name, args|
+        it "should accept global boolean settings #{name} the action" do
+          app.command_line.stubs(:args).returns args
+          app.preinit && app.parse_options
+          Puppet[:trace].should be_true
+        end
+      end
+
+      { "before" => %w{--syslogfacility user1 foo},
+        " after" => %w{foo --syslogfacility user1}
+      }.each do |name, args|
+        it "should accept global settings with arguments #{name} the action" do
+          app.command_line.stubs(:args).returns args
+          app.preinit && app.parse_options
+          Puppet[:syslogfacility].should == "user1"
+        end
+      end
     end
   end
 

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list