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

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 all commits
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
32 changes: 32 additions & 0 deletions console/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,35 @@ of your commands to change their appearance::
// ...
}
}

Writing to the error output
---------------------------

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.

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 standard output
$io->write('Reusable information');

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

.. note::

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 write to the standard output instead of the error output.
0