Skip to content
Permalink
Browse files

Improve the options completion by allowing to get options for subcomm…

…ands
  • Loading branch information...
HavokInspiration committed Dec 6, 2015
1 parent c2bd05c commit bd505410c3cd202dc56ef0d27fe73818683c4641
@@ -66,11 +66,14 @@ public function commands()
*/
public function options()
{
$commandName = '';
$commandName = $subCommandName = '';
if (!empty($this->args[0])) {
$commandName = $this->args[0];
}
$options = $this->Command->options($commandName);
if (!empty($this->args[1])) {
$subCommandName = $this->args[1];
}
$options = $this->Command->options($commandName, $subCommandName);
return $this->_output($options);
}
@@ -135,6 +138,10 @@ public function getOptionParser()
'command' => [
'help' => 'The command name',
'required' => false,
],
'subcommand' => [
'help' => 'The subcommand name',
'required' => false,
]
]
]
@@ -174,8 +174,8 @@ public function subCommands($commandName)
/**
* Get Shell instance for the given command
*
* @param mixed $commandName The command you want.
* @return mixed
* @param string $commandName The command you want.
* @return \Cake\Console\Shell|bool Shell instance if the command can be found, false otherwise.
*/
public function getShell($commandName)
{
@@ -219,18 +219,30 @@ public function getShell($commandName)
}
/**
* Get Shell instance for the given command
* Get options list for the given command or subcommand
*
* @param mixed $commandName The command to get options for.
* @return array
* @param string $commandName The command to get options for.
* @param string $subCommandName The subcommand to get options for. Can be empty to get options for the command.
* If this parameter is used, the subcommand must be a valid subcommand of the command passed
* @return array Options list for the given command or subcommand
*/
public function options($commandName)
public function options($commandName, $subCommandName = '')
{
$Shell = $this->getShell($commandName);
if (!$Shell) {
$parser = new ConsoleOptionParser();
} else {
$parser = $Shell->getOptionParser();
return [];
}
$parser = $Shell->getOptionParser();
if (!empty($subCommandName)) {
$subCommandName = Inflector::camelize($subCommandName);
if ($Shell->hasTask($subCommandName)) {
$parser = $Shell->{$subCommandName}->getOptionParser();
} else {
return [];
}
}
$options = [];
@@ -128,7 +128,7 @@ public function testCommands()
}
/**
* test that options without argument returns the default options
* test that options without argument returns nothing
*
* @return void
*/
@@ -137,26 +137,25 @@ public function testOptionsNoArguments()
$this->Shell->runCommand(['options']);
$output = $this->out->output;
$expected = "--help -h --verbose -v --quiet -q\n";
$expected = "";
$this->assertTextEquals($expected, $output);
}
/**
* test that options with a nonexisting command returns the default options
* test that options with a nonexisting command returns nothing
*
* @return void
*/
public function testOptionsNonExistingCommand()
{
$this->Shell->runCommand(['options', 'foo']);
$output = $this->out->output;
$expected = "--help -h --verbose -v --quiet -q\n";
$expected = "";
$this->assertTextEquals($expected, $output);
}
/**
* test that options with a existing command returns the proper options
* test that options with an existing command returns the proper options
*
* @return void
*/
@@ -169,6 +168,20 @@ public function testOptions()
$this->assertTextEquals($expected, $output);
}
/**
* test that options with an existing command / subcommand pair returns the proper options
*
* @return void
*/
public function testOptionsTask()
{
$this->Shell->runCommand(['options', 'sample', 'sample']);
$output = $this->out->output;
$expected = "--help -h --verbose -v --quiet -q --sample -s\n";
$this->assertTextEquals($expected, $output);
}
/**
* test that subCommands with a existing CORE command returns the proper sub commands
*
@@ -193,7 +206,7 @@ public function testSubCommandsAppPlugin()
$this->Shell->runCommand(['subcommands', 'app.sample']);
$output = $this->out->output;
$expected = "derp\n";
$expected = "derp sample\n";
$this->assertTextEquals($expected, $output);
}
@@ -251,7 +264,7 @@ public function testSubCommandsAppDuplicatePluginNoDot()
$this->Shell->runCommand(['subcommands', 'sample']);
$output = $this->out->output;
$expected = "derp\n";
$expected = "derp sample\n";
$this->assertTextEquals($expected, $output);
}
@@ -26,6 +26,8 @@
class SampleShell extends Shell
{
public $tasks = ['Sample'];
/**
* main method
*
@@ -0,0 +1,33 @@
<?php
/**
* SampleTask file
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp\Shell\Task;
use Cake\Console\Shell;
class SampleTask extends Shell
{
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addOption('sample', [
'short' => 's',
'help' => 'This is a sample option for the sample task.',
]);
return $parser;
}
}
No changes.

0 comments on commit bd50541

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