Skip to content

Commit 5fa647a

Browse files
committed
Addking details on user the dialog helper in commands
1 parent 02c5ea8 commit 5fa647a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cookbook/console.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ will work:
155155
app/console demo:greet Fabien --iterations=5 --yell
156156
app/console demo:greet Fabien --yell --iterations=5
157157
158+
Asking the User for Information
159+
-------------------------------
160+
161+
When creating commands, you have the ability to collect more information
162+
from the user by asking him/her questions. For example, suppose you want
163+
to confirm an action before actually executing it. Add the following to your
164+
command::
165+
166+
$dialog = $this->getHelperSet()->get('dialog');
167+
if (!$dialog->askConfirmation($output, 'Continue with this action?', false)) {
168+
return;
169+
}
170+
171+
In this case, the user will be asked "Continue with this action", and unless
172+
they answer with ``y``, the task will stop running. The third argument to
173+
``askConfirmation`` is the default value to return if the user doesn't enter
174+
any input.
175+
176+
You can also ask questions with more than a simple yes/no answer. For example,
177+
if you needed to know the name of something, you might do the following::
178+
179+
$dialog = $this->getHelperSet()->get('dialog');
180+
$name = $dialog->ask($output, 'Please enter the name of the widget', 'foo');
181+
158182
Testing Commands
159183
----------------
160184

0 commit comments

Comments
 (0)