diff options
author | Jesse Wolfe <jes5199@gmail.com> | 2010-04-13 14:53:44 -0700 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 748aed9a4fe70cc2ecc0c782b694114356d9eb25 (patch) | |
tree | 749b66092e2d5dfea3a6bcf442c7a9e3cbd91d69 | |
parent | eafde5cacaac79da79d9d1415618801fcc37edcc (diff) | |
download | puppet-748aed9a4fe70cc2ecc0c782b694114356d9eb25.tar.gz puppet-748aed9a4fe70cc2ecc0c782b694114356d9eb25.tar.xz puppet-748aed9a4fe70cc2ecc0c782b694114356d9eb25.zip |
Fix #3552 single executable should display usage
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 <jes5199@gmail.com>
-rwxr-xr-x | bin/puppet | 9 | ||||
-rw-r--r-- | lib/puppet/util/command_line.rb | 15 | ||||
-rw-r--r-- | spec/unit/util/command_line.rb | 70 |
3 files changed, 88 insertions, 6 deletions
diff --git a/bin/puppet b/bin/puppet index cffa89135..2f497c5fa 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 000000000..59454364c --- /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 000000000..d6bbcd1be --- /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 |