-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Components/Console improvements #2074
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
Changes from 2 commits
a708da8
66c94e4
61abc95
f002cea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,3 +148,30 @@ You can also ask and validate a hidden response:: | |
|
||
If you want to allow the response to be visible if it cannot be hidden for | ||
some reason, pass true as the fifth argument. | ||
|
||
Let the user choose from a list of answers | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.2 | ||
The ``select`` method was added in Symfony 2.2. | ||
|
||
If you have a predefined set of answers the user can choose from, you | ||
could use the ``ask`` method described above or, to make sure the user | ||
provided a correct answer, the ``askAndValidate`` method. Both have | ||
the disadvantage that you need to handle incorrect values yourself. | ||
|
||
Instead, you can use the ``select`` method, which makes sure that the user | ||
can only enter a valid string from a predefined list:: | ||
|
||
$dialog = $app->getHelperSet()->get('dialog'); | ||
$colors = array('red', 'blue', 'yellow'); | ||
|
||
$color = $dialog->select($output, 'Please select your favorite color (default to red)', $colors, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't correct. The |
||
|
||
// Work with the color | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be
|
||
|
||
If the user enters an invalid string, an error message is shown and the user | ||
is asked to provide the answer another time, till he enters a valid string. | ||
|
||
The last parameter is the index of the default value in the array or ``null`` if | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please not that |
||
no default should be provided. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* :doc:`/components/console/helpers/dialoghelper` | ||
* :doc:`/components/console/helpers/formatterhelper` | ||
* :doc:`/components/console/helpers/progresshelper` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
.. index:: | ||
single: Console Helpers; Progress Helper | ||
|
||
Progress Helper | ||
=============== | ||
|
||
.. versionadded:: 2.2 | ||
The ``progress`` helper was added in Symfony 2.2. | ||
|
||
When executing longer-running commands, it may be helpful to show progress | ||
information, which updates as your command runs: | ||
|
||
.. image:: /images/components/console/progress.png | ||
|
||
To display progress details, use the :class:`Symfony\\Component\\Console\\Helper\\ProgressHelper`, | ||
pass it a total number of units, and advance the progress as your command executes:: | ||
|
||
$progress = $this->getHelperSet()->get('progress'); | ||
|
||
$progress->start($output, 50); | ||
$i = 0; | ||
while ($i++ < 50) { | ||
// do some work | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be
|
||
|
||
// advance the progress bar 1 unit | ||
$progress->advance(); | ||
} | ||
|
||
$progress->finish(); | ||
|
||
The appearance of the progress output can be customized as well, with a number | ||
of different levels of verbosity. Each of these displays different possible | ||
items - like percentage completion, a moving progress bar, or current/total | ||
information (e.g. 10/50):: | ||
|
||
$progress->setFormat(ProgressHelper::FORMAT_QUIET); | ||
$progress->setFormat(ProgressHelper::FORMAT_NORMAL); | ||
$progress->setFormat(ProgressHelper::FORMAT_VERBOSE); | ||
$progress->setFormat(ProgressHelper::FORMAT_QUIET_NOMAX); | ||
// the default value | ||
$progress->setFormat(ProgressHelper::FORMAT_NORMAL_NOMAX); | ||
$progress->setFormat(ProgressHelper::FORMAT_VERBOSE_NOMAX); | ||
|
||
You can also control the different characters and the width used for the | ||
progress bar:: | ||
|
||
// the finished part of the bar | ||
$progress->setBarCharacter('<comment>=</comment>'); | ||
// the unfinished part of the bar | ||
$progress->setEmptyBarCharacter(' '); | ||
$progress->setProgressCharacter('|'); | ||
$progress->setBarWidth(50); | ||
|
||
To see other available options, check the API documentation for | ||
:class:`Symfony\\Component\\Console\\Helper\\ProgressHelper`. | ||
|
||
.. caution:: | ||
|
||
For performance reasons, be careful to not set the total number of steps | ||
to a high number. For example, if you're iterating over a large number | ||
of items, consider a smaller "step" number that updates on only some | ||
iterations:: | ||
|
||
$progress->start($output, 500); | ||
$i = 0; | ||
while ($i++ < 50000) { | ||
// ... do some work | ||
|
||
// advance every 100 iterations | ||
if ($i % 100 == 0) { | ||
$progress->advance(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use an API link here for the
select
method.