Skip to content

Commit

Permalink
Improve the options completion by allowing to get options for subcomm…
Browse files Browse the repository at this point in the history
…ands
  • Loading branch information
HavokInspiration committed Dec 6, 2015
1 parent c2bd05c commit bd50541
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/Shell/CompletionShell.php
Expand Up @@ -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);
}
Expand Down Expand Up @@ -135,6 +138,10 @@ public function getOptionParser()
'command' => [
'help' => 'The command name',
'required' => false,
],
'subcommand' => [
'help' => 'The subcommand name',
'required' => false,
]
]
]
Expand Down
30 changes: 21 additions & 9 deletions src/Shell/Task/CommandTask.php
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 = [];
Expand Down
29 changes: 21 additions & 8 deletions tests/TestCase/Shell/CompletionShellTest.php
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/test_app/TestApp/Shell/SampleShell.php
Expand Up @@ -26,6 +26,8 @@
class SampleShell extends Shell
{

public $tasks = ['Sample'];

/**
* main method
*
Expand Down
33 changes: 33 additions & 0 deletions tests/test_app/TestApp/Shell/Task/SampleTask.php
@@ -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;
}
}
Empty file.

0 comments on commit bd50541

Please sign in to comment.