我有一个symfony2控制台组件命令task:execute
,它有一个必需的参数taskHandle
。
protected function configure()
{
parent::configure();
$this
->setName("task:execute")
->setDescription("...")
->addArgument(
'taskHandle',
InputArgument::REQUIRED,
'Which task would you like to run?'
);
...
}
为了做一些批量工作,我现在想从另一个命令执行这个命令。我不知道如何将参数传递给命令。
在我的BatchCommand中,我试过:
$command = $this->getApplication()->find('task:execute');
foreach ($handles as $handle) {
$input = new ArgvInput([
'taskHandle', $handle
]);
$command->run($input, $output);
}
或
$command = $this->getApplication()->find('task:execute');
foreach ($handles as $handle) {
$executeInput = new StringInput($handle);
$command->run($executeInput, $output);
}
屈服于:
Invalid taskhandle: does not exist.
我很困惑,这个论点变得更低了。然而我的执行:任务在自己调用时起作用。从另一个命令传递参数是问题所在。
答案 0 :(得分:1)
您可以通过发送带有所需参数的ArrayInput来实现:
$command = $this->getApplication()->find('doctrine:database:drop');
$arguments = array(
'command' => 'doctrine:database:drop',
'--force' => true,
);
$input = new ArrayInput($arguments);
$command->run($input, $output);