k0pernikus
k0pernikus

Reputation: 66450

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

I have a symfony2 console component command task:execute which has a required argument taskHandle.

protected function configure()
{
    parent::configure();
    $this
        ->setName("task:execute")
        ->setDescription("...")
        ->addArgument(
            'taskHandle',
            InputArgument::REQUIRED,
            'Which task would you like to run?'
        );
    ...
}

In order to do some batch work, I now want to execute this command from another command. It eludes me as to how I am supposed to pass the argument to the command.

In my BatchCommand I tried:

$command = $this->getApplication()->find('task:execute');
foreach ($handles as $handle) {
    $input = new ArgvInput([
        'taskHandle', $handle
    ]);

    $command->run($input, $output);
}

or

$command = $this->getApplication()->find('task:execute');
foreach ($handles as $handle) {
    $executeInput = new StringInput($handle);
    $command->run($executeInput, $output);
}

Yielding in:

Invalid taskhandle:  does not exist.

I am both confused that the argument gets lowercased. Yet my execute:tasks works when called on its own. Passing the argument from another command is the problem.

Upvotes: 0

Views: 81

Answers (1)

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

You can do that by sending an ArrayInput, with the needed parameters:

$command = $this->getApplication()->find('doctrine:database:drop');

$arguments = array(
    'command' => 'doctrine:database:drop',
    '--force'  => true,
);

$input = new ArrayInput($arguments);
$command->run($input, $output);

Upvotes: 1

Related Questions