Added some tests to make the single executable command behavior
explicit.
Added logic to display the usage message if we're on a tty and no
arguments are passed.

Signed-off-by: Jesse Wolfe <[email protected]>
---
 bin/puppet                      |    9 ++---
 lib/puppet/util/command_line.rb |   15 ++++++++
 spec/unit/util/command_line.rb  |   70 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 6 deletions(-)
 create mode 100644 lib/puppet/util/command_line.rb
 create mode 100644 spec/unit/util/command_line.rb

diff --git a/bin/puppet b/bin/puppet
index cffa891..2f497c5 100755
--- a/bin/puppet
+++ b/bin/puppet
@@ -7,13 +7,10 @@ builtins = Dir[File.join(absolute_appdir, '*.rb')].map{|fn| 
File.basename(fn, '.
 usage = "Usage: puppet command <space separated arguments>"
 available = "Available commands are: #{builtins.sort.join(', ')}"
 
-command_name = ARGV.empty? || ARGV.first[/^-/] || ARGV.first =~ /\.pp/ || 
ARGV.first =~ /\.rb/ ? nil : ARGV.shift # subcommand?
+require 'puppet/util/command_line'
+command_line = Puppet::Util::CommandLine.shift_subcommand_from_argv
 
-if command_name.nil? # It's old-style puppet, executing something
-    command_name = "main"
-end
-
-if command_name.nil? # main
+if command_name.nil?
     puts usage, available
 elsif builtins.include?(command_name) #subcommand
     require File.join(appdir, command_name)
diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb
new file mode 100644
index 0000000..5945436
--- /dev/null
+++ b/lib/puppet/util/command_line.rb
@@ -0,0 +1,15 @@
+module Puppet
+    module Util
+        module CommandLine
+            def self.shift_subcommand_from_argv( argv = ARGV, stdin = STDIN )
+                if ! argv.first
+                    "main" unless stdin.tty? # ttys get usage info
+                elsif argv.first =~ /^-|\.pp$|\.rb$/
+                    "main"
+                else
+                    argv.shift
+                end
+            end
+        end
+    end
+end
diff --git a/spec/unit/util/command_line.rb b/spec/unit/util/command_line.rb
new file mode 100644
index 0000000..d6bbcd1
--- /dev/null
+++ b/spec/unit/util/command_line.rb
@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? 
require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+
+require 'puppet/util/command_line'
+
+describe Puppet::Util::CommandLine do
+    before do
+        @tty  = stub("tty",  :tty? => true )
+        @pipe = stub("pipe", :tty? => false)
+    end
+
+    it "should pull off the first argument if it looks like a subcommand" do
+        args    = %w( client --help whatever.pp )
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == "client"
+        args.should    == %w( --help whatever.pp )
+    end
+
+    it "should use main if the first argument looks like a .pp file" do
+        args    = %w( whatever.pp )
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == "main"
+        args.should    == %w( whatever.pp )
+    end
+
+    it "should use main if the first argument looks like a .rb file" do
+        args    = %w( whatever.rb )
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == "main"
+        args.should    == %w( whatever.rb )
+    end
+
+    it "should use main if the first argument looks like a flag" do
+        args    = %w( --debug )
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == "main"
+        args.should    == %w( --debug )
+    end
+
+    it "should use main if the first argument is -" do
+        args    = %w( - )
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == "main"
+        args.should    == %w( - )
+    end
+
+    it "should return nil if there are no arguments on a tty" do
+        args    = []
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@tty )
+
+        command.should == nil
+        args.should    == []
+    end
+
+    it "should use main if there are no arguments on a pipe" do
+        args    = []
+        command = Puppet::Util::CommandLine.shift_subcommand_from_argv( args, 
@pipe )
+
+        command.should == "main"
+        args.should    == []
+    end
+
+end
-- 
1.6.3.3

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to