Browse Source

Refactor code.

mark_story 12 years ago
parent
commit
96bd2c658d

+ 4 - 2
src/Console/Command/CompletionShell.php

@@ -75,7 +75,7 @@ class CompletionShell extends Shell {
  *
  * @return void
  */
-	public function subCommands() {
+	public function subcommands() {
 		if (!$this->args) {
 			return $this->_output();
 		}
@@ -86,7 +86,7 @@ class CompletionShell extends Shell {
 
 /**
  * Guess autocomplete from the whole argument string
- * 
+ *
  * @return void
  */
 	public function fuzzy() {
@@ -132,6 +132,8 @@ class CompletionShell extends Shell {
 					]
 				]
 			]
+		])->addSubcommand('fuzzy', [
+			'help' => __d('cake_console', 'Guess autocomplete')
 		])->epilog([
 			__d('cake_console', 'This command is not intended to be called manually'),
 		]);

+ 1 - 0
src/Console/ConsoleOptionParser.php

@@ -465,6 +465,7 @@ class ConsoleOptionParser {
  */
 	public function parse($argv, $command = null) {
 		if (isset($this->_subcommands[$command]) && $this->_subcommands[$command]->parser()) {
+			array_shift($argv);
 			return $this->_subcommands[$command]->parser()->parse($argv);
 		}
 		$params = $args = [];

+ 6 - 10
src/Console/Shell.php

@@ -344,6 +344,7 @@ class Shell {
 		try {
 			list($this->params, $this->args) = $this->OptionParser->parse($argv, $command);
 		} catch (Error\ConsoleException $e) {
+			$this->err('<error>Error: ' . $e->getMessage() . '</error>');
 			$this->out($this->OptionParser->help($command));
 			return false;
 		}
@@ -364,27 +365,22 @@ class Shell {
 		}
 
 		$subcommands = $this->OptionParser->subcommands();
-
-		// Method when there are no subcommands.
 		$isMethod = $this->hasMethod($command);
-		if ($isMethod && count($subcommands) === 0) {
-			array_shift($this->args);
-			$this->startup();
-			return call_user_func_array([$this, $command], $this->args);
-		}
 
-		// Method when there is a subcommand
-		if ($isMethod && isset($subcommands[$command])) {
+		if (
+			$isMethod &&
+			(count($subcommands) === 0 || isset($subcommands[$command]))
+		) {
 			array_shift($this->args);
 			$this->startup();
 			return call_user_func_array([$this, $command], $this->args);
 		}
 
-		// Exposed task.
 		if ($this->hasTask($command) && isset($subcommands[$command])) {
 			array_shift($argv);
 			$this->startup();
 			$command = Inflector::camelize($command);
+			// TODO this is suspicious.
 			return $this->{$command}->runCommand('execute', $argv);
 		}
 

+ 6 - 6
tests/TestCase/Console/Command/CompletionShellTest.php

@@ -161,7 +161,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommandsCorePlugin() {
-		$this->Shell->runCommand('subCommands', array('subCommands', 'CORE.bake'));
+		$this->Shell->runCommand('subcommands', array('subcommands', 'CORE.bake'));
 		$output = $this->out->output;
 
 		$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";
@@ -174,7 +174,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommandsAppPlugin() {
-		$this->Shell->runCommand('subCommands', array('subCommands', 'app.sample'));
+		$this->Shell->runCommand('subcommands', array('subcommands', 'app.sample'));
 		$output = $this->out->output;
 
 		$expected = '';
@@ -187,7 +187,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommandsPlugin() {
-		$this->Shell->runCommand('subCommands', array('subCommands', 'TestPluginTwo.welcome'));
+		$this->Shell->runCommand('subcommands', array('subcommands', 'TestPluginTwo.welcome'));
 		$output = $this->out->output;
 
 		$expected = "say_hello\n";
@@ -200,7 +200,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommandsNoArguments() {
-		$this->Shell->runCommand('subCommands', array());
+		$this->Shell->runCommand('subcommands', array());
 		$output = $this->out->output;
 
 		$expected = '';
@@ -213,7 +213,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommandsNonExistingCommand() {
-		$this->Shell->runCommand('subCommands', array('subCommands', 'foo'));
+		$this->Shell->runCommand('subcommands', array('subcommands', 'foo'));
 		$output = $this->out->output;
 
 		$expected = '';
@@ -226,7 +226,7 @@ class CompletionShellTest extends TestCase {
  * @return void
  */
 	public function testSubCommands() {
-		$this->Shell->runCommand('subCommands', array('subCommands', 'bake'));
+		$this->Shell->runCommand('subcommands', array('subcommands', 'bake'));
 		$output = $this->out->output;
 
 		$expected = "behavior component controller fixture helper model plugin project shell test view widget zerg\n";