Skip to content

[Console] Open CompleteCommand for custom outputs #43923

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
Nov 4, 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: 15 additions & 6 deletions src/Symfony/Component/Console/Command/CompleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ final class CompleteCommand extends Command
protected static $defaultName = '|_complete';
protected static $defaultDescription = 'Internal command to provide shell completion suggestions';

private static $completionOutputs = [
'bash' => BashCompletionOutput::class,
];
private $completionOutputs;

private $isDebug = false;

/**
* @param array<string, class-string<CompletionOutputInterface>> $completionOutputs A list of additional completion outputs, with shell name as key and FQCN as value
*/
public function __construct(array $completionOutputs = [])
{
// must be set before the parent constructor, as the property value is used in configure()
$this->completionOutputs = $completionOutputs + ['bash' => BashCompletionOutput::class];

parent::__construct();
}

protected function configure(): void
{
$this
->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type (e.g. "bash")')
->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type ("'.implode('", "', array_keys($this->completionOutputs)).'")')
->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)')
->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)')
->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'The version of the completion script')
Expand Down Expand Up @@ -71,8 +80,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new \RuntimeException('The "--shell" option must be set.');
}

if (!$completionOutput = self::$completionOutputs[$shell] ?? false) {
throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shell, implode('", "', array_keys(self::$completionOutputs))));
if (!$completionOutput = $this->completionOutputs[$shell] ?? false) {
throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shell, implode('", "', array_keys($this->completionOutputs))));
}

$completionInput = $this->createCompletionInput($input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Command\CompleteCommand;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -50,6 +51,20 @@ public function testUnsupportedShellOption()
$this->execute(['--shell' => 'unsupported']);
}

public function testAdditionalShellSupport()
{
$this->command = new CompleteCommand(['supported' => BashCompletionOutput::class]);
$this->command->setApplication($this->application);
$this->tester = new CommandTester($this->command);

$this->execute(['--shell' => 'supported', '--current' => '1', '--input' => ['bin/console']]);

// verify that the default set of shells is still supported
$this->execute(['--shell' => 'bash', '--current' => '1', '--input' => ['bin/console']]);

$this->assertTrue(true);
}

/**
* @dataProvider provideInputAndCurrentOptionValues
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"accept_value": true,
"is_value_required": true,
"is_multiple": false,
"description": "The shell type (e.g. \"bash\")",
"description": "The shell type (\"bash\")",
"default": null
},
"current": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<arguments/>
<options>
<option name="--shell" shortcut="-s" accept_value="1" is_value_required="1" is_multiple="0">
<description>The shell type (e.g. "bash")</description>
<description>The shell type ("bash")</description>
<defaults/>
</option>
<option name="--input" shortcut="-i" accept_value="1" is_value_required="1" is_multiple="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"accept_value": true,
"is_value_required": true,
"is_multiple": false,
"description": "The shell type (e.g. \"bash\")",
"description": "The shell type (\"bash\")",
"default": null
},
"current": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<arguments/>
<options>
<option name="--shell" shortcut="-s" accept_value="1" is_value_required="1" is_multiple="0">
<description>The shell type (e.g. "bash")</description>
<description>The shell type ("bash")</description>
<defaults/>
</option>
<option name="--input" shortcut="-i" accept_value="1" is_value_required="1" is_multiple="1">
Expand Down