8000 feature #43615 feat: add completion for CompletionCommand "shell" arg… · symfony/symfony@ec34dd5 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ec34dd5

Browse files
committed
feature #43615 feat: add completion for CompletionCommand "shell" argument (dkarlovi)
This PR was merged into the 5.4 branch. Discussion ---------- feat: add completion for CompletionCommand "shell" argument | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #43594 | License | MIT | Doc PR | N/A Commits ------- cc984e1 feat: add completion for CompletionCommand "shell" argument
2 parents 1fdd7a8 + cc984e1 commit ec34dd5

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Console\Command;
1313

14+
use Symfony\Component\Console\Completion\CompletionInput;
15+
use Symfony\Component\Console\Completion\CompletionInterface;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1417
use Symfony\Component\Console\Input\InputArgument;
1518
use Symfony\Component\Console\Input\InputInterface;
1619
use Symfony\Component\Console\Input\InputOption;
@@ -23,11 +26,18 @@
2326
*
2427
* @author Wouter de Jong <wouter@wouterj.nl>
2528
*/
26-
final class DumpCompletionCommand extends Command
29+
final class DumpCompletionCommand extends Command implements CompletionInterface
2730
{
2831
protected static $defaultName = 'completion';
2932
protected static $defaultDescription = 'Dump the shell completion script';
3033

34+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
35+
{
36+
if ($input->mustSuggestArgumentValuesFor('shell')) {
37+
$suggestions->suggestValues($this->getSupportedShells());
38+
}
39+
}
40+
3141
protected function configure()
3242
{
3343
$fullCommand = $_SERVER['PHP_SELF'];
@@ -82,9 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8292
$shell = $input->getArgument('shell') ?? self::guessShell();
8393
$completionFile = __DIR__.'/../Resources/completion.'.$shell;
8494
if (!file_exists($completionFile)) {
85-
$supportedShells = array_map(function ($f) {
86-
return pathinfo($f, \PATHINFO_EXTENSION);
87-
}, glob(__DIR__.'/../Resources/completion.*'));
95+
$supportedShells = $this->getSupportedShells();
8896

8997
($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)
9098
->writeln(sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells)));
@@ -113,4 +121,14 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
113121
$output->write($line);
114122
});
115123
}
124+
125+
/**
126+
* @return string[]
127+
*/
128+
private function getSupportedShells(): array
129+
{
130+
return array_map(function ($f) {
131+
return pathinfo($f, \PATHINFO_EXTENSION);
132+
}, glob(__DIR__.'/../Resources/completion.*'));
133+
}
116134
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Symfony\Component\Console\Tests\Command;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Console\Command\DumpCompletionCommand;
7+
use Symfony\Component\Console\Tester\CommandCompletionTester;
8+
9+
class DumpCompletionCommandTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider provideCompletionSuggestions
13+
*/
14+
public function testComplete(array $input, array $expectedSuggestions)
15+
{
16+
$tester = new CommandCompletionTester(new DumpCompletionCommand());
17+
$suggestions = $tester->complete($input);
18+
19+
$this->assertSame($expectedSuggestions, $suggestions);
20+
}
21+
22+
public function provideCompletionSuggestions()
23+
{
24+
yield 'shell' => [
25+
[''],
26+
['bash'],
27+
];
28+
}
29+
}

0 commit comments

Comments
 (0)
0