HelloPablo
HelloPablo

Reputation: 625

Call Symfony console command within another command and suppress output

I have a simple console app using the Symfony console component.

I have two commands (say, cmdOne and cmdTwo) which can be called independently easily.

$ myApp.php cmdOne
$ myApp.php cmdTwo

Both commands have a considerable amount of output, which I can easily mute by issuing the -q option.

Now, I'd like cmdOne to call cmdTwo however I'd like cmdTwo to be quiet. I'm not trying to do anything crazy, but I'm struggling to get anywhere, despite reading through the docs.

Here's my sample code so far (this snippet would be contained within cmdOne->execute()):

$command = $this->getApplication()->find('cmdTwo');

$input = new ArrayInput(array(
    'command' => 'cmdTwo',
    '-q' => true
));

$returnCode = $command->run($input, $output);

This runs fine, as in the code command executes, but there's output on the console (generated by cmdTwo) which I'd like to not show.

Is specifying the -q option not possible because it's "reserved" (i.e not created by the dev), or am I missing something obvious?

Upvotes: 5

Views: 4561

Answers (1)

zerkms
zerkms

Reputation: 254886

Instead of passing the same $output instance (the one that outputs to your current console) create an instance of NullOutput

$returnCode = $command->run($input, new \Symfony\Component\Console\Output\NullOutput);

It basically is a blackhole - it accepts output and silently drops it.

Upvotes: 7

Related Questions