Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 793
HelloPablo Avatar asked Jan 09 '15 01:01

HelloPablo


People also ask

Can a command call another command?

If a command depends on another one being run before it you can call in the console command itself.

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

How do I run console commands?

Easily open Command Prompt by running Windows Run by holding the Windows button and hitting the R button on your keyboard. You can then type "cmd" and press enter, opening Command Prompt. If you're unsure of what commands to use, you can type "Help" into Command Prompt.


1 Answers

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.

like image 59
zerkms Avatar answered Sep 30 '22 03:09

zerkms