8000 [Console] feat: add completion for CompletionCommand "shell" argument by dkarlovi · Pull Request #43615 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Console] feat: add completion for CompletionCommand "shell" argument #43615

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
26 changes: 22 additions & 4 deletions src/Symfony/Component/Console/Command/DumpCompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

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\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -23,11 +26,18 @@
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
final class DumpCompletionCommand extends Command
final class DumpCompletionCommand extends Command implements CompletionInterface
{
protected static $defaultName = 'completion';
protected static $defaultDescription = 'Dump the shell completion script';

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('shell')) {
$suggestions->suggestValues($this->getSupportedShells());
}
}

protected function configure()
{
$fullCommand = $_SERVER['PHP_SELF'];
Expand Down Expand Up @@ -82,9 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$shell = $input->getArgument('shell') ?? self::guessShell();
$completionFile = __DIR__.'/../Resources/completion.'.$shell;
if (!file_exists($completionFile)) {
$supportedShells = array_map(function ($f) {
return pathinfo($f, \PATHINFO_EXTENSION);
}, glob(__DIR__.'/../Resources/completion.*'));
$supportedShells = $this->getSupportedShells();

($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)
->writeln(sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells)));
Expand Down Expand Up @@ -113,4 +121,14 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
$output->write($line);
});
}

/**
* @return string[]
*/
private function getSupportedShells(): array
{
return array_map(function ($f) {
return pathinfo($f, \PATHINFO_EXTENSION);
}, glob(__DIR__.'/../Resources/completion.*'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Component\Console\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\DumpCompletionCommand;
use Symfony\Component\Console\Tester\CommandCompletionTester;

class DumpCompletionCommandTest extends TestCase
{
/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$tester = new CommandCompletionTester(new DumpCompletionCommand());
$suggestions = $tester->complete($input);

$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'shell' => [
[''],
['bash'],
];
}
}
0