8000 [Console] Open CompleteCommand for custom outputs · symfony/symfony@1f58312 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f58312

Browse files
committed
[Console] Open CompleteCommand for custom outputs
1 parent 781508d commit 1f58312

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

src/Symfony/Component/Console/Command/CompleteCommand.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ final class CompleteCommand extends Command
3131
protected static $defaultName = '|_complete';
3232
protected static $defaultDescription = 'Internal command to provide shell completion suggestions';
3333

34-
private static $completionOutputs = [
35-
'bash' => BashCompletionOutput::class,
36-
];
34+
private $completionOutputs;
3735

3836
private $isDebug = false;
3937

38+
/**
39+
* @param array<string, class-string<CompletionOutputInterface>> $completionOutputs A list of additional completion outputs, with shell name as key and FQCN as value
40+
*/
41+
public function __construct(array $completionOutputs = [])
42+
{
43+
// must be set before the parent constructor, as the property value is used in configure()
44+
$this->completionOutputs = $completionOutputs + ['bash' => BashCompletionOutput::class];
45+
46+
parent::__construct();
47+
}
48+
4049
protected function configure(): void
4150
{
4251
$this
43-
->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type (e.g. "bash")')
52+
->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type ("'.implode('", "', array_keys($this->completionOutputs)).'")')
4453
->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)')
4554
->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)')
4655
->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'The version of the completion script')
@@ -71,8 +80,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7180
throw new \RuntimeException('The "--shell" option must be set.');
7281
}
7382

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

7887
$completionInput = $this->createCompletionInput($input);

src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Command\CompleteCommand;
1818
use Symfony\Component\Console\Completion\CompletionInput;
1919
use Symfony\Component\Console\Completion\CompletionSuggestions;
20+
use Symfony\Component\Console\Completion\Output\BashCompletionOutput;
2021
use Symfony\Component\Console\Input\InputArgument;
2122
use Symfony\Component\Console\Output\OutputInterface;
2223
use Symfony\Component\Console\Tester\CommandTester;
@@ -50,6 +51,20 @@ public function testUnsupportedShellOption()
5051
$this->execute(['--shell' => 'unsupported']);
5152
}
5253

54+
public function testAdditionalShellSupport()
55+
{
56+
$this->command = new CompleteCommand(['supported' => BashCompletionOutput::class]);
57+
$this->command->setApplication($this->application);
58+
$this->tester = new CommandTester($this->command);
59+
60+
$this->execute(['--shell' => 'supported', '--current' => '1', '--input' => ['bin/console']]);
61+
62+
// verify that the default set of shells is still supported
63+
$this->execute(['--shell' => 'bash', '--current' => '1', '--input' => ['bin/console']]);
64+
65+
$this->assertTrue(true);
66+
}
67+
5368
/**
5469
* @dataProvider provideInputAndCurrentOptionValues
5570
*/

src/Symfony/Component/Console/Tests/Fixtures/application_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"accept_value": true,
9090
"is_value_required": true,
9191
"is_multiple": false,
92-
"description": "The shell type (e.g. \"bash\")",
92+
"description": "The shell type (\"bash\")",
9393
"default": null
9494
},
9595
"current": {

src/Symfony/Component/Console/Tests/Fixtures/application_1.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<arguments/>
1111
<options>
1212
<option name="--shell" shortcut="-s" accept_value="1" is_value_required="1" is_multiple="0">
13-
<description>The shell type (e.g. "bash")</description>
13+
<description>The shell type ("bash")</description>
1414
<defaults/>
1515
</option>
1616
<option name="--input" shortcut="-i" accept_value="1" is_value_required="1" is_multiple="1">

src/Symfony/Component/Console/Tests/Fixtures/application_2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"accept_value": true,
9494
"is_value_required": true,
9595
"is_multiple": false,
96-
"description": "The shell type (e.g. \"bash\")",
96+
"description": "The shell type (\"bash\")",
9797
"default": null
9898
},
9999
"current": {

src/Symfony/Component/Console/Tests/Fixtures/application_2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<arguments/>
1111
<options>
1212
<option name="--shell" shortcut="-s" accept_value="1" is_value_required="1" is_multiple="0">
13-
<description>The shell type (e.g. "bash")</description>
13+
<description>The shell type ("bash")</description>
1414
<defaults/>
1515
</option>
1616
<option name="--input" shortcut="-i" accept_value="1" is_value_required="1" is_multiple="1">

0 commit comments

Comments
 (0)
0