Skip to content

Commit d0c8221

Browse files
committed
[component][console] Initial documentation to support symfony/symfony#3501
1 parent 35c0255 commit d0c8221

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

components/console/introduction.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,77 @@ if you needed to know the name of something, you might do the following::
251251
$dialog = $this->getHelperSet()->get('dialog');
252252
$name = $dialog->ask($output, 'Please enter the name of the widget', 'foo');
253253

254+
Displaying a Progress Bar
255+
-------------------------
256+
257+
.. versionadded:: 2.2
258+
The ``progress`` helper was added in Symfony 2.2.
259+
260+
When executing longer-running commands, it may be helpful to to show progress
261+
information, which updates as your command runs:
262+
263+
.. image:: /images/components/console/progress.png
264+
265+
To display progress details, use the :class:`Symfony\\Component\\Console\\Helper\\ProgressHelper`,
266+
pass it a total number of units, and advance the progress as your command executes::
267+
268+
$progress = $app->getHelperSet()->get('progress');
269+
270+
$progress->start($output, 50);
271+
$i = 0;
272+
while ($i++ < 50) {
273+
// do some work
274+
275+
// advance the progress bar 1 unit
276+
$progress->advance();
277+
}
278+
279+
$progress->finish();
280+
281+
The appearance of the progress output can be customized as well, with a number
282+
of different levels of verbosity. Each of these displays different possible
283+
items - like percentage completion, a moving progress bar, or current/total
284+
information (e.g. 10/50)::
285+
286+
$progress->setFormat(ProgressHelper::QUIET);
287+
$progress->setFormat(ProgressHelper::NORMAL);
288+
$progress->setFormat(ProgressHelper::VERBOSE);
289+
$progress->setFormat(ProgressHelper::FORMAT_QUIET_NOMAX);
290+
// the default value
291+
$progress->setFormat(ProgressHelper::FORMAT_NORMAL_NOMAX);
292+
$progress->setFormat(ProgressHelper::FORMAT_VERBOSE_NOMAX);
293+
294+
You can also control the different characters and the width used for the
295+
progress bar::
296+
297+
// the finished part of the bar
298+
$progress->setBarCharacter('<comment>=</comment>');
299+
// the unfinished part of the bar
300+
$progress->setEmptyBarCharacter(' ');
301+
$progress->setProgressChar('|');
302+
$progress->setBarWidth(50);
303+
304+
To see other available options, check the API documentation for
305+
:class:`Symfony\\Component\\Console\\Helper\\ProgressHelper`.
306+
307+
.. caution::
308+
309+
For performance reasons, be careful to not set the total number of steps
310+
to a high number. For example, if you're iterating over a large number
311+
of items, consider a smaller "step" number that updates on only some
312+
iterations::
313+
314+
$progress->start($output, 500);
315+
$i = 0;
316+
while ($i++ < 50000) {
317+
// do some work
318+
319+
// advance every 100 iterations
320+
if ($i % 100 == 0) {
321+
$progress->advance();
322+
}
323+
}
324+
254325
Testing Commands
255326
----------------
256327

3.29 KB
Loading

0 commit comments

Comments
 (0)