所以我用两个参数发出命令:
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')
;
}
Run Code Online (Sandbox Code Playgroud)
但server:chat命令不要求我提供论据。
如何要求用户在自定义控制台命令中提供输入?
http://symfony.com/doc/current/components/console/helpers/questionhelper.html
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')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question1 = new Question('Provide a hostname: ', 'localhost');
$question2 = new Question('Provide a port number: ', '8080');
$localhost = $helper->ask($input, $output, $question1);
$port = $helper->ask($input, $output, $question2);
Run Code Online (Sandbox Code Playgroud)
除了 user6827096 的答案之外:还有一种interact()方法可用于使用问题助手从交互式输入中预先填充所需的选项,并且除非--no-interaction传递给命令,否则将被调用:
/**
* 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)
{
}
Run Code Online (Sandbox Code Playgroud)
与问题助手结合使用的一个很好的例子可以在 Symfony 附带的 Sensio 的 Generator Bundle 中找到:https://github.com/sensiolabs/SensioGeneratorBundle/blob/master/Command/GenerateBundleCommand.php#L112
| 归档时间: |
|
| 查看次数: |
6844 次 |
| 最近记录: |