Browse Source

Add Shell::$plugin and update getOptionParser()

Adding a plugin property allows the correct
usage/help information to be generated.

Fixes #2359
mark_story 14 years ago
parent
commit
205bfb506f

+ 10 - 1
lib/Cake/Console/Shell.php

@@ -80,6 +80,14 @@ class Shell extends Object {
 	public $name = null;
 
 /**
+ * The name of the plugin the shell belongs to.
+ * Is automatically set by ShellDispatcher when a shell is constructed.
+ *
+ * @var string
+ */
+	public $plugin = null;
+
+/**
  * Contains tasks to load and instantiate
  *
  * @var array
@@ -409,7 +417,8 @@ class Shell extends Object {
  * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::getOptionParser
  */
 	public function getOptionParser() {
-		$parser = new ConsoleOptionParser($this->name);
+		$name = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
+		$parser = new ConsoleOptionParser($name);
 		return $parser;
 	}
 

+ 1 - 0
lib/Cake/Console/ShellDispatcher.php

@@ -219,6 +219,7 @@ class ShellDispatcher {
 			));
 		}
 		$Shell = new $class();
+		$Shell->plugin = trim($plugin, '.');
 		return $Shell;
 	}
 

+ 2 - 0
lib/Cake/Test/Case/Console/ShellDispatcherTest.php

@@ -422,6 +422,8 @@ class ShellDispatcherTest extends CakeTestCase {
 		$Dispatcher = new TestShellDispatcher();
 		$result = $Dispatcher->getShell('test_plugin.example');
 		$this->assertInstanceOf('ExampleShell', $result);
+		$this->assertEquals('TestPlugin', $result->plugin);
+		$this->assertEquals('Example', $result->name);
 
 		$Dispatcher = new TestShellDispatcher();
 		$result = $Dispatcher->getShell('TestPlugin.example');

+ 13 - 0
lib/Cake/Test/Case/Console/ShellTest.php

@@ -841,4 +841,17 @@ TEXT;
 		$expected = 'TestApple';
 		$this->assertEquals($expected, $this->Shell->TestApple->name);
 	}
+
+/**
+ * Test that option parsers are created with the correct name/command.
+ *
+ * @return void
+ */
+	public function testGetOptionParser() {
+		$this->Shell->name = 'test';
+		$this->Shell->plugin = 'plugin';
+		$parser = $this->Shell->getOptionParser();
+	
+		$this->assertEquals('plugin.test', $parser->command());
+	}
 }