【问题标题】:How to pass an argument when trying to run a command from another command using the Console Component?尝试使用控制台组件从另一个命令运行命令时如何传递参数?
【发布时间】:2015-08-14 19:20:26
【问题描述】:

我有一个 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 在单独调用时工作。从另一个命令传递参数是个问题。

【问题讨论】:

    标签: php symfony command-line-arguments


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-29
      • 2016-12-17
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 2011-09-28
      相关资源
      最近更新 更多