-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Console] Add completion to help & list commands #43596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ | |
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
use Symfony\Component\Console\Completion\CompletionInput; | ||
use Symfony\Component\Console\Completion\CompletionInterface; | ||
use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
use Symfony\Component\Console\Descriptor\ApplicationDescription; | ||
use Symfony\Component\Console\Helper\DescriptorHelper; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
@@ -22,7 +26,7 @@ | |
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class ListCommand extends Command | ||
class ListCommand extends Command implements CompletionInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
|
@@ -74,4 +78,19 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
|
||
return 0; | ||
} | ||
|
||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void | ||
{ | ||
if ($input->mustSuggestArgumentValuesFor('namespace')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be an improvement to create a "Helper", in order to have common functions for public function shouldSuggestValues(string $type): void {
if ($input->mustSuggestArgumentValuesFor($type)) {
$descriptor = new ApplicationDescription($this->getApplication());
$suggestions->suggestValues(array_keys($descriptor->getNamespaces()));
return;
}
if ($input->mustSuggestOptionValuesFor('format')) {
$helper = new DescriptorHelper();
$suggestions->suggestValues($helper->getFormats());
}
} And calling it in the // ListCommand.php
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
$this->commandHelper->shouldSuggestValues('namespace');
} // HelpCommand.php
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
$this->commandHelper->shouldSuggestValues('command_name');
} ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a helper knowing about argument names of all Symfony commands does not make sense (and would require to make argument names unique across all commands) |
||
$descriptor = new ApplicationDescription($this->getApplication()); | ||
$suggestions->suggestValues(array_keys($descriptor->getNamespaces())); | ||
|
||
return; | ||
} | ||
|
||
if ($input->mustSuggestOptionValuesFor('format')) { | ||
$helper = new DescriptorHelper(); | ||
$suggestions->suggestValues($helper->getFormats()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tester; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Completion\CompletionInput; | ||
use Symfony\Component\Console\Completion\CompletionInterface; | ||
use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
|
||
/** | ||
* Eases the testing of command completion. | ||
* | ||
* @author Jérôme Tamarelle <jerome@tamarelle.net> | ||
*/ | ||
class CommandCompletionTester | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this one final |
||
{ | ||
private $command; | ||
|
||
public function __construct(Command $command) | ||
{ | ||
$this->command = $command; | ||
} | ||
|
||
/** | ||
* Create completion suggestions from input tokens. | ||
*/ | ||
public function complete(array $input): array | ||
{ | ||
if (!$this->command instanceof CompletionInterface) { | ||
throw new \LogicException(sprintf('Command "%s" must implement "%s" to support completion.', \get_class($this->command), CompletionInput::class)); | ||
} | ||
|
||
$currentIndex = \count($input); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we make this a parameter (e.g. for cases where the suggestions for argument 1 is different based on argument 2?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please open a PR. The index thing is tricky and I don't know to deal with it. |
||
if ('' === end($input)) { | ||
array_pop($input); | ||
} | ||
array_unshift($input, $this->command->getName()); | ||
|
||
$completionInput = CompletionInput::fromTokens($input, $currentIndex); | ||
$completionInput->bind($this->command->getDefinition()); | ||
$suggestions = new CompletionSuggestions(); | ||
|
||
$this->command->complete($completionInput, $suggestions); | ||
|
||
$options = []; | ||
foreach ($suggestions->getOptionSuggestions() as $option) { | ||
$options[] = '--'.$option->getName(); | ||
} | ||
|
||
return array_merge($options, $suggestions->getValueSuggestions()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\Helper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Helper\DescriptorHelper; | ||
|
||
class DescriptorHelperTest extends TestCase | ||
{ | ||
public function testGetFormats() | ||
{ | ||
$helper = new DescriptorHelper(); | ||
$expectedFormats = [ | ||
'txt', | ||
'xml', | ||
'json', | ||
'md', | ||
]; | ||
$this->assertSame($expectedFormats, $helper->getFormats()); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.