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


How to pass an argument when trying to run a command from another command using the Console Component?

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

我都很困惑,因为参数是小写的。然而,我的execute:tasks在单独调用时可以工作。从另一个命令传递参数是问题所在。

你可以通过发送一个带有所需参数的ArrayInput来实现:

$command = $this->getApplication()->find('doctrine:database:drop');
$arguments = array(
    'command' => 'doctrine:database:drop',
    '--force'  => true,
);
$input = new ArrayInput($arguments);
$command->run($input, $output);