Symfony 如何要求用户在自定义控制台命令中提供输入?

Symfony 如何要求用户在自定义控制台命令中提供输入?,symfony,Symfony,所以我用两个参数发出命令: class ServerCommand extends ContainerAwareCommand { protected function configure() { $this ->setName('chat:server') ->setDescription('Start the Chat server') ->addArgument('ho

所以我用两个参数发出命令:

class ServerCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('chat:server')
            ->setDescription('Start the Chat server')
            ->addArgument('host', InputArgument::REQUIRED, 'Provide a hostname')
            ->addArgument('port', InputArgument::OPTIONAL, 'Provide a port number')
        ;
    }
但是
server:chat
命令不要求我提供参数

如何要求用户在自定义控制台命令中提供输入?


除了用户6827096的答案之外:还有
interactive()
方法,该方法可用于使用问题帮助器从交互输入中预填充所需选项,并且将被调用,除非
--没有交互传递给命令:

/**
 * Interacts with the user.
 *
 * This method is executed before the InputDefinition is validated.
 * This means that this is the only place where the command can
 * interactively ask for values of missing required arguments.
 *
 * @param InputInterface  $input  An InputInterface instance
 * @param OutputInterface $output An OutputInterface instance
 */
protected function interact(InputInterface $input, OutputInterface $output)
{
}

在Symfony附带的Sensio的Generator捆绑包中可以找到它与问题助手组合使用的一个很好的例子:

Hum,是的,在提问之前阅读文档会很有帮助^^当然,但更有用的是谷歌
如何要求用户在自定义控制台命令中提供输入?
并移动到这里
/**
 * Interacts with the user.
 *
 * This method is executed before the InputDefinition is validated.
 * This means that this is the only place where the command can
 * interactively ask for values of missing required arguments.
 *
 * @param InputInterface  $input  An InputInterface instance
 * @param OutputInterface $output An OutputInterface instance
 */
protected function interact(InputInterface $input, OutputInterface $output)
{
}