Symfony:如何在QuestionHelper中启用箭头键(向左/向右)?

Hoo*_*ooK 5 command arrow-keys symfony

我在Symfony 3项目中有一个自定义命令。

该命令会询问一些问题,例如捆绑包名称。

    $io = new SymfonyStyle($input, $output);
    $question = new Question('Please enter the name of your bundle: ', 'AppBundle');
    $bundle = $io->askQuestion($question);
Run Code Online (Sandbox Code Playgroud)

该代码可以很好地工作,但是如果用户输入错误,则左箭头不起作用。
左箭头为^ [[D,右^ [[C

用户按下箭头键时是否可以移动光标?

小智 0

您可以使用 Symfony Styles ( https://symfony.com/doc/current/console/style.html ) 来做到这一点,然后您可以启用箭头键支持并在自定义命令中移动光标。

    $io = new SymfonyStyle($input, $output);
    
    //This is where you enable arrow key support and move the cursor
    $io->getInput()->setInteractive(true);
    $io->setInputStty(['echo', 'icanon', 'opost', 'onlcr']);
    
    $question = new Question('Please enter the name of your bundle: ', 'AppBundle');
    $bundle = $io->askQuestion($question);
    
    // Reset the input config
    $io->getInput()->setInteractive(false);
    $io->setInputStty([]);
    
Run Code Online (Sandbox Code Playgroud)