@@ -13,7 +13,7 @@ helper set, which you can get by calling
1313
1414The Question Helper has a single method
1515:method: `Symfony\\ Component\\ Console\\ Command\\ Command::ask ` that needs an
16- :class: `Symfony\\ Component\\ Console\\ Output \\ InputInterface ` instance as the
16+ :class: `Symfony\\ Component\\ Console\\ Input \\ InputInterface ` instance as the
1717first argument, an :class: `Symfony\\ Component\\ Console\\ Output\\ OutputInterface `
1818instance as the second argument and a
1919:class: `Symfony\\ Component\\ Console\\ Question\\ Question ` as last argument.
@@ -24,14 +24,22 @@ Asking the User for Confirmation
2424Suppose you want to confirm an action before actually executing it. Add
2525the following to your command::
2626
27- use Symfony\Component\Console\Question\ConfirmationQuestion;
2827 // ...
28+ use Symfony\Component\Console\Question\ConfirmationQuestion;
2929
30- $helper = $this->getHelper('question');
31- $question = new ConfirmationQuestion('Continue with this action?', false);
30+ class YourCommand extends Command
31+ {
32+ // ...
33+
34+ public function execute(InputInterface $input, OutputInterface $output)
35+ {
36+ $helper = $this->getHelper('question');
37+ $question = new ConfirmationQuestion('Continue with this action?', false);
3238
33- if (!$helper->ask($input, $output, $question)) {
34- return;
39+ if (!$helper->ask($input, $output, $question)) {
40+ return;
41+ }
42+ }
3543 }
3644
3745In this case, the user will be asked "Continue with this action?". If the user
@@ -66,11 +74,15 @@ You can also ask a question with more than a simple yes/no answer. For instance,
6674if you want to know a bundle name, you can add this to your command::
6775
6876 use Symfony\Component\Console\Question\Question;
69- // ...
7077
71- $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
78+ // ...
79+ public function execute(InputInterface $input, OutputInterface $output)
80+ {
81+ // ...
82+ $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
7283
73- $bundle = $helper->ask($input, $output, $question);
84+ $bundle = $helper->ask($input, $output, $question);
85+ }
7486
7587The user will be asked "Please enter the name of the bundle". They can type
7688some name which will be returned by the
@@ -86,20 +98,24 @@ which makes sure that the user can only enter a valid string
8698from a predefined list::
8799
88100 use Symfony\Component\Console\Question\ChoiceQuestion;
89- // ...
90101
91- $helper = $this->getHelper('question');
92- $question = new ChoiceQuestion(
93- 'Please select your favorite color (defaults to red)',
94- array('red', 'blue', 'yellow'),
95- 0
96- );
97- $question->setErrorMessage('Color %s is invalid.');
102+ // ...
103+ public function execute(InputInterface $input, OutputInterface $output)
104+ {
105+ // ...
106+ $helper = $this->getHelper('question');
107+ $question = new ChoiceQuestion(
108+ 'Please select your favorite color (defaults to red)',
109+ array('red', 'blue', 'yellow'),
110+ 0
111+ );
112+ $question->setErrorMessage('Color %s is invalid.');
98113
99- $color = $helper->ask($input, $output, $question);
100- $output->writeln('You have just selected: '.$color);
114+ $color = $helper->ask($input, $output, $question);
115+ $output->writeln('You have just selected: '.$color);
101116
102- // ... do something with the color
117+ // ... do something with the color
118+ }
103119
104120The option which should be selected by default is provided with the third
105121argument of the constructor. The default is ``null ``, which means that no
@@ -120,18 +136,22 @@ feature using comma separated values. This is disabled by default, to enable
120136this use :method: `Symfony\\ Component\\ Console\\ Question\\ ChoiceQuestion::setMultiselect `::
121137
122138 use Symfony\Component\Console\Question\ChoiceQuestion;
123- // ...
124139
125- $helper = $this->getHelper('question');
126- $question = new ChoiceQuestion(
127- 'Please select your favorite colors (defaults to red and blue)',
128- array('red', 'blue', 'yellow'),
129- '0,1'
130- );
131- $question->setMultiselect(true);
140+ // ...
141+ public function execute(InputInterface $input, OutputInterface $output)
142+ {
143+ // ...
144+ $helper = $this->getHelper('question');
145+ $question = new ChoiceQuestion(
146+ 'Please select your favorite colors (defaults to red and blue)',
147+ array('red', 'blue', 'yellow'),
148+ '0,1'
149+ );
150+ $question->setMultiselect(true);
132151
133- $colors = $helper->ask($input, $output, $question);
134- $output->writeln('You have just selected: ' . implode(', ', $colors));
152+ $colors = $helper->ask($input, $output, $question);
153+ $output->writeln('You have just selected: ' . implode(', ', $colors));
154+ }
135155
136156Now, when the user enters ``1,2 ``, the result will be:
137157``You have just selected: blue, yellow ``.
@@ -146,13 +166,17 @@ You can also specify an array of potential answers for a given question. These
146166will be autocompleted as the user types::
147167
148168 use Symfony\Component\Console\Question\Question;
149- // ...
150169
151- $bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
152- $question = new Question('Please enter the name of a bundle', 'FooBundle');
153- $question->setAutocompleterValues($bundles);
170+ // ...
171+ public function execute(InputInterface $input, OutputInterface $output)
172+ {
173+ // ...
174+ $bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
175+ $question = new Question('Please enter the name of a bundle', 'FooBundle');
176+ $question->setAutocompleterValues($bundles);
154177
155- $name = $helper->ask($input, $output, $question);
178+ $name = $helper->ask($input, $output, $question);
179+ }
156180
157181Hiding the User's Response
158182~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,13 +185,17 @@ You can also ask a question and hide the response. This is particularly
161185convenient for passwords::
162186
163187 use Symfony\Component\Console\Question\Question;
164- // ...
165188
166- $question = new Question('What is the database password?');
167- $question->setHidden(true);
168- $question->setHiddenFallback(false);
189+ // ...
190+ public function execute(InputInterface $input, OutputInterface $output)
191+ {
192+ // ...
193+ $question = new Question('What is the database password?');
194+ $question->setHidden(true);
195+ $question->setHiddenFallback(false);
169196
170- $password = $helper->ask($input, $output, $question);
197+ $password = $helper->ask($input, $output, $question);
198+ }
171199
172200.. caution ::
173201
@@ -189,20 +217,24 @@ be suffixed with ``Bundle``. You can validate that by using the
189217method::
190218
191219 use Symfony\Component\Console\Question\Question;
192- // ...
193-
194- $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
195- $question->setValidator(function ($answer) {
196- if ('Bundle' !== substr($answer, -6)) {
197- throw new \RuntimeException(
198- 'The name of the bundle should be suffixed with \'Bundle\''
199- );
200- }
201- return $answer;
202- });
203- $question->setMaxAttempts(2);
204220
205- $name = $helper->ask($input, $output, $question);
221+ // ...
222+ public function execute(InputInterface $input, OutputInterface $output)
223+ {
224+ // ...
225+ $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
226+ $question->setValidator(function ($answer) {
227+ if ('Bundle' !== substr($answer, -6)) {
228+ throw new \RuntimeException(
229+ 'The name of the bundle should be suffixed with \'Bundle\''
230+ );
231+ }
232+ return $answer;
233+ });
234+ $question->setMaxAttempts(2);
235+
236+ $name = $helper->ask($input, $output, $question);
237+ }
206238
207239The ``$validator `` is a callback which handles the validation. It should
208240throw an exception if there is something wrong. The exception message is displayed
@@ -222,23 +254,26 @@ Validating a Hidden Response
222254You can also use a validator with a hidden question::
223255
224256 use Symfony\Component\Console\Question\Question;
225- // ...
226-
227- $helper = $this->getHelper('question');
228257
229- $question = new Question('Please enter your password');
230- $question->setValidator( function ($value) {
231- if (trim($value) == '') {
232- throw new \Exception('The password can not be empty');
233- }
258+ // ...
259+ public function execute(InputInterface $input, OutputInterface $output)
260+ {
261+ // ...
262+ $helper = $this->getHelper('question');
234263
235- return $value;
236- });
237- $question->setHidden(true);
238- $question->setMaxAttempts(20);
264+ $question = new Question('Please enter your password');
265+ $question->setValidator(function ($value) {
266+ if (trim($value) == '') {
267+ throw new \Exception('The password can not be empty');
268+ }
239269
240- $password = $helper->ask($input, $output, $question);
270+ return $value;
271+ });
272+ $question->setHidden(true);
273+ $question->setMaxAttempts(20);
241274
275+ $password = $helper->ask($input, $output, $question);
276+ }
242277
243278Testing a Command that Expects Input
244279------------------------------------
@@ -276,6 +311,6 @@ from the command line, you need to set the helper input stream::
276311 }
277312
278313By setting the input stream of the ``QuestionHelper ``, you imitate what the
279- console would do internally with all user input through the cli . This way
314+ console would do internally with all user input through the CLI . This way
280315you can test any user interaction (even complex ones) by passing an appropriate
281316input stream.
0 commit comments