自动完成不适用于 Symfony 控制台脚本

Autocomplete doesn't work for a Symfony Console script

我有一个 Symfony 控制台应用程序,我想将它与自动完成一起使用,但 autocoplite 不起作用。 当我点击 tab 时没有任何反应。

如果我这样输入,我的应用程序可以从命令行成功运行:

./myapp generate-constants nomenclature

然后打印

Hello World!, nomenclature

这是我的名为 myapp 的脚本

#!/usr/bin/env php
<?php
require 'bootstrap.php';

use MyApp\Core\Console\GenerateConstantsCommand;
use Symfony\Component\Console\Application;

$app = new Application();
$app->add(new GenerateConstantsCommand());
$app->run();

这是GenerateConstantsCommand.php

<?php
namespace MyApp\Core\Console;
use LogicException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;

class GenerateConstantsCommand extends Command
{
    protected function configure()
    {
        $this->setName('generate-constants')
            ->setDescription('Generate constsants')
            ->setHelp('Demonstration of custom commands created by Symfony Console component.')
            ->addArgument('nomenclature', InputArgument::REQUIRED, 'Pass the nomenclature');
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln(sprintf('Hello World!, %s', $input->getArgument('nomenclature')));
        return 0;
    }

    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
    {
        if ($input->mustSuggestOptionValuesFor('nomenclature')) {
            $suggestions->suggestValues(['json', 'xml']);
        }
    }
}

我做错了什么?为什么自动完成不起作用?

我也尝试过 stecman/symfony-console-completion 但是当我 运行 eval $(./myapp _completion --generate-hook) 时,光标转到新行并永远停留在那里。

我使用 Bash 5.0.17(1)-release、Ubuntu 20.04.4 LTS、Symfony Console 5.4.7 和 PHP 7.3.26.

根据the documentation

确保安装 bash-completion 软件包:sudo apt-get install -y bash-completion

然后运行下面的命令ONCE(安装Symfony的bash完成脚本):php bin/console completion bash | sudo tee /etc/bash_completion.d/console-events-terminate.

重新启动您的终端,它应该可以工作了。

这是我用于测试的示例命令,shell auto-completion 按预期工作:

<?php declare(strict_types = 1);

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function is_string;
use function sprintf;

class TestCommand extends Command
{
    protected function configure(): void
    {
        $this->setName('app:test')
            ->setDescription('Test command')
            ->setHidden(false)
            ->addArgument('format', InputArgument::REQUIRED, 'The format');
    }

    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
    {
        if (true === $input->mustSuggestArgumentValuesFor('format')) {
            $suggestions->suggestValues(['json', 'xml']);
        }
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $format = $input->getArgument('format');
        if (false === is_string($format)) {
            $output->writeln('Given format is not a string');
            return Command::FAILURE;
        }
        $output->writeln(sprintf('Format: %s', $format));
        return Command::SUCCESS;
    }
}