首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动完成不适用于Symfony控制台脚本

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

Stack Overflow用户
提问于 2022-04-26 13:01:12
回答 1查看 342关注 0票数 1

我有一个Symfony控制台应用程序,我想使用它与自动完成,但autocoplite不工作。当我单击tab 时,什么都不会发生。

如果我输入如下命令行,我的应用程序就能成功地工作:

代码语言:javascript
复制
./myapp generate-constants nomenclature

然后印出来

你好,世界!,命名

这是我的脚本,名为myapp

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
<?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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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自动完成工作的示例命令,如预期的那样:

代码语言:javascript
复制
<?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;
    }
}

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72014429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档