8000 [Console] Document how to exclude messages from output using SymfonyStyle by chalasr · Pull Request #7360 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Console] Document how to exclude messages from output using SymfonyStyle #7360

< 8000 /div>
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reworded the description
  • Loading branch information
javiereguiluz authored Apr 15, 2017
commit ff0ccdeef96aac4fbf296522a0b4e10847a048d0
32 changes: 18 additions & 14 deletions console/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -377,27 +377,31 @@ of your commands to change their appearance::
Writing to the error output
---------------------------

If you rely on the output of a command (e.g. by redirecting it to a file for later reuse),
you may want to get only the relevant information outputted by the command, excluding progress
bars, notes, warnings and other error messages which have, most of the time, no value for reuse.
If you reuse the output of a command as the input of other commands or dump it
into a file for later reuse, you probably want to exclude progress bars, notes
and other output that provides no real value.

For excluding irrelevant messages from the output, you need to make your command write them to the error
output. Fortunately, the :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle` provides a convenient
method to easily switch between both outputs:
:method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::getErrorStyle`.
This method returns a new ``SymfonyStyle`` instance which makes use of the error output:
Commands can output information in two different streams: ``stdout`` (standard
output) is the stream where the real contents should be output and ``stderr``
(standard error) is the stream where the errors and the debugging messages
should be output.

The :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle` class provides a
convenient method called :method:`Symfony\\Component\\Console\\Style\\SymfonyStyle::getErrorStyle`
to switch between both streams. This method returns a new ``SymfonyStyle``
instance which makes use of the error output::

$io = new SymfonyStyle($input, $output);

// Write to the output
// Write to the standard output
$io->write('Reusable information');

// Write to the error output
$io->getErrorStyle()->warning('Irrelevant warning');
$io->getErrorStyle()->warning('Debugging information or errors');

.. note::

If you created the original ``SymfonyStyle`` instance with an ``OutputInterface`` object that is
not an instance of :class:`Symfony\\Component\\Console\\Output\\ConsoleOutputInterface`, using
``getErrorStyle()`` will return an instance which makes use of the normal output instead of
the error one, as if you did not called the method.
If you create a ``SymfonyStyle`` instance with an ``OutputInterface`` object
that is not an instance of :class:`Symfony\\Component\\Console\\Output\\ConsoleOutputInterface`,
the ``getErrorStyle()`` method will have no effect and the returned object
will still output to the standard output instead of the error output.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"will still write" to avoid the repetition?

0