diff --git a/src/Symfony/Component/Console/Command/CompleteCommand.php b/src/Symfony/Component/Console/Command/CompleteCommand.php index 40006bd763d49..97357d6737ed3 100644 --- a/src/Symfony/Component/Console/Command/CompleteCommand.php +++ b/src/Symfony/Component/Console/Command/CompleteCommand.php @@ -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> $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') @@ -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); diff --git a/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php b/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php index bf8ab00c9f246..189928897cc7c 100644 --- a/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php @@ -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; @@ -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 */ diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.json b/src/Symfony/Component/Console/Tests/Fixtures/application_1.json index c346a44953a56..8c8ba2285f59a 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.json +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.json @@ -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": { diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml index 0976d90abf81a..5a17229343fcf 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_1.xml @@ -10,7 +10,7 @@