Skip to content
Permalink
Browse files

Updating shiftArgs to return shifted arg

If no args were available the method was returning false and now
returns null. If args were available the method was returning true it
now returns the shifted argument instead.
  • Loading branch information...
mariuswilms committed May 1, 2009
1 parent 4b89dd2 commit ebe1cd258a09adf06e54aec2b3b0ae3dd22836ff
Showing with 33 additions and 9 deletions.
  1. +4 −9 cake/console/cake.php
  2. +29 −0 cake/tests/cases/console/cake.test.php
@@ -279,7 +279,7 @@ function __bootstrap() {
* @access public
*/
function dispatch() {
if (!$arg = array_shift($this->args)) {
if (!$arg = $this->shiftArgs()) {
$this->help();
return false;
}
@@ -298,7 +298,7 @@ function dispatch() {
$this->shellName = Inflector::camelize($shell);
$this->shellClass = $this->shellName . 'Shell';
if ($arg = array_shift($this->args)) {
if ($arg = $this->shiftArgs()) {
$this->shellCommand = Inflector::variable($arg);
}
@@ -546,16 +546,11 @@ function __parseParams($params) {
/**
* Removes first argument and shifts other arguments up
*
* @return boolean False if there are no arguments
* @return mixed Null if there are no arguments otherwise the shifted argument
* @access public
*/
function shiftArgs() {
if (empty($this->args)) {
return false;
}
unset($this->args[0]);
$this->args = array_values($this->args);
return true;
return array_shift($this->args);
}
/**
* Shows console help
@@ -764,6 +764,35 @@ function testDispatchNotAShellWithoutMain() {
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}
/**
* Verify shifting of arguments
*
* @access public
* @return void
*/
function testShiftArgs() {
$Dispatcher =& new TestShellDispatcher();
$Dispatcher->args = array('a', 'b', 'c');
$this->assertEqual($Dispatcher->shiftArgs(), 'a');
$this->assertIdentical($Dispatcher->args, array('b', 'c'));
$Dispatcher->args = array('a' => 'b', 'c', 'd');
$this->assertEqual($Dispatcher->shiftArgs(), 'b');
$this->assertIdentical($Dispatcher->args, array('c', 'd'));
$Dispatcher->args = array('a', 'b' => 'c', 'd');
$this->assertEqual($Dispatcher->shiftArgs(), 'a');
$this->assertIdentical($Dispatcher->args, array('b' => 'c', 'd'));
$Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
$this->assertEqual($Dispatcher->shiftArgs(), 'a');
$this->assertIdentical($Dispatcher->args, array(0 => 'b', 1 => 'c'));
$Dispatcher->args = array();
$this->assertNull($Dispatcher->shiftArgs());
$this->assertIdentical($Dispatcher->args, array());
}
/**
* testHelpCommand method
*

0 comments on commit ebe1cd2

Please sign in to comment.
You can’t perform that action at this time.