Skip to content

[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

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Symfony/Component/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +26,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HelpCommand extends Command
class HelpCommand extends Command implements CompletionInterface
{
private $command;

Expand Down Expand Up @@ -80,4 +84,19 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('command_name')) {
$descriptor = new ApplicationDescription($this->getApplication());
$suggestions->suggestValues(array_keys($descriptor->getCommands()));

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$helper = new DescriptorHelper();
$suggestions->suggestValues($helper->getFormats());
}
}
}
21 changes: 20 additions & 1 deletion src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +26,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ListCommand extends Command
class ListCommand extends Command implements CompletionInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -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')) {

Choose a reason for hiding this comment

The 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 complete and calling it like this:

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 complete function for ListCommand.php and HelpCommand.php?

// 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');
}

?

Copy link
Member

Choose a reason for hiding this comment

The 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());
}
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Helper/DescriptorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public function getName()
{
return 'descriptor';
}

public function getFormats(): array
{
return array_keys($this->descriptors);
}
}
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
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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?)

Copy link
Member Author

Choose a reason for hiding this comment

The 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());
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;

class HelpCommandTest extends TestCase
Expand Down Expand Up @@ -68,4 +69,35 @@ public function testExecuteForApplicationCommandWithXmlOption()
$this->assertStringContainsString('list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
$this->assertStringContainsString('<command', $commandTester->getDisplay(), '->execute() returns an XML help text if --format=xml is passed');
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
$application = new Application();
$application->add(new \FooCommand());
$tester = new CommandCompletionTester($application->get('help'));
$suggestions = $tester->complete($input, 2);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
];

yield 'nothing' => [
[''],
[],
];

yield 'command_name' => [
['f'],
['completion', 'help', 'list', 'foo:bar'],
];
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;

class ListCommandTest extends TestCase
Expand Down Expand Up @@ -112,4 +113,35 @@ public function testExecuteListsCommandsOrderRaw()

$this->assertEquals($output, trim($commandTester->getDisplay(true)));
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
$application = new Application();
$application->add(new \FooCommand());
$tester = new CommandCompletionTester($application->get('list'));
$suggestions = $tester->complete($input, 2);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'option --format' => [
['--format', ''],
['txt', 'xml', 'json', 'md'],
];

yield 'namespace' => [
[''],
['_global', 'foo'],
];

yield 'namespace started' => [
['f'],
['_global', 'foo'],
];
}
}
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());
}
}