尝试使用控制台组件从另一个命令运行命令时如何传递参数?

时间:2015-08-14 10:19:59

标签: php symfony command-line-arguments

我有一个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.

我很困惑,这个论点变得更低了。然而我的执行:任务在自己调用时起作用。从另一个命令传递参数是问题所在。

1 个答案:

答案 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);