我有一个Symfony控制台应用程序,我想使用它与自动完成,但autocoplite不工作。当我单击tab 时,什么都不会发生。
如果我输入如下命令行,我的应用程序就能成功地工作:
./myapp generate-constants 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-控制台完成,但是当我运行eval $(./myapp _completion --generate-hook)时,光标会转到新行,并永远停留在那里。
我使用Bash5.0.17(1)-release、Ubuntu20.04.4LTS、Symfony控制台5.4.7和PHP7.3.26。
发布于 2022-04-26 14:42:59
根据文献资料
确保安装bash-completion包:sudo apt-get install -y bash-completion。
然后运行以下命令一旦(安装Symfony的bash完成脚本):php bin/console completion bash | sudo tee /etc/bash_completion.d/console-events-terminate。
重新启动终端,它应该可以工作。
这是我用于测试和shell自动完成工作的示例命令,如预期的那样:
<?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;
}
}

https://stackoverflow.com/questions/72014429
复制相似问题