Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da29e85

Browse filesBrowse files
committed
Document how to exclude irrelevant messages from command output using SymfonyStyle
1 parent c45a7f2 commit da29e85
Copy full SHA for da29e85

File tree

1 file changed

+28
-0
lines changed
Filter options

1 file changed

+28
-0
lines changed

‎console/style.rst

Copy file name to clipboardExpand all lines: console/style.rst
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,31 @@ of your commands to change their appearance::
373373
// ...
374374
}
375375
}
376+
377+
Writing to the error output
378+
---------------------------
379+
380+
If you rely on the output of a command (e.g. by redirecting it to a file for later reuse),
381+
you may want to get only the relevant information outputted by the command, excluding progress
382+
bars, notes, warnings and other error messages which have, most of the time, no value for reuse.
383+
384+
For excluding irrelevant messages from the output, you need to make your command write them to the error
385+
output. Fortunately, the :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle` provides a convenient
386+
method to easily switch between both outputs:
387+
:method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::getErrorStyle`.
388+
This method returns a new ``SymfonyStyle`` instance which makes use of the error output:
389+
390+
$io = new SymfonyStyle($input, $output);
391+
392+
// Write to the output
393+
$io->write('Reusable information');
394+
395+
// Write to the error output
396+
$io->getErrorStyle()->warning('Irrelevant warning');
397+
398+
.. note::
399+
400+
If you created the original ``SymfonyStyle`` instance with an ``OutputInterface`` object that is
401+
not an instance of :class:`Symfony\\Component\\Console\\Output\ConsoleOutputInterface`, using
402+
``getErrorStyle()`` will return an instance which makes use of the normal output instead of
403+
the error one, as if you did not called the method.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.