@@ -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,24 @@ 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\Input\InputInterface;
29+ use Symfony\Component\Console\Output\OutputInterface;
30+ use Symfony\Component\Console\Question\ConfirmationQuestion;
2931
30- $helper = $this->getHelper('question');
31- $question = new ConfirmationQuestion('Continue with this action?', false);
32+ class YourCommand extends Command
33+ {
34+ // ...
35+
36+ public function execute(InputInterface $input, OutputInterface $output)
37+ {
38+ $helper = $this->getHelper('question');
39+ $question = new ConfirmationQuestion('Continue with this action?', false);
3240
33- if (!$helper->ask($input, $output, $question)) {
34- return;
41+ if (!$helper->ask($input, $output, $question)) {
42+ return;
43+ }
44+ }
3545 }
3646
3747In this case, the user will be asked "Continue with this action?". If the user
@@ -66,11 +76,15 @@ You can also ask a question with more than a simple yes/no answer. For instance,
6676if you want to know a bundle name, you can add this to your command::
6777
6878 use Symfony\Component\Console\Question\Question;
69- // ...
7079
71- $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
80+ // ...
81+ public function execute(InputInterface $input, OutputInterface $output)
82+ {
83+ // ...
84+ $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
7285
73- $bundle = $helper->ask($input, $output, $question);
86+ $bundle = $helper->ask($input, $output, $question);
87+ }
7488
7589The user will be asked "Please enter the name of the bundle". They can type
7690some name which will be returned by the
@@ -86,20 +100,24 @@ which makes sure that the user can only enter a valid string
86100from a predefined list::
87101
88102 use Symfony\Component\Console\Question\ChoiceQuestion;
89- // ...
90103
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.');
104+ // ...
105+ public function execute(InputInterface $input, OutputInterface $output)
106+ {
107+ // ...
108+ $helper = $this->getHelper('question');
109+ $question = new ChoiceQuestion(
110+ 'Please select your favorite color (defaults to red)',
111+ array('red', 'blue', 'yellow'),
112+ 0
113+ );
114+ $question->setErrorMessage('Color %s is invalid.');
98115
99- $color = $helper->ask($input, $output, $question);
100- $output->writeln('You have just selected: '.$color);
116+ $color = $helper->ask($input, $output, $question);
117+ $output->writeln('You have just selected: '.$color);
101118
102- // ... do something with the color
119+ // ... do something with the color
120+ }
103121
104122The option which should be selected by default is provided with the third
105123argument of the constructor. The default is ``null ``, which means that no
@@ -120,18 +138,22 @@ feature using comma separated values. This is disabled by default, to enable
120138this use :method: `Symfony\\ Component\\ Console\\ Question\\ ChoiceQuestion::setMultiselect `::
121139
122140 use Symfony\Component\Console\Question\ChoiceQuestion;
123- // ...
124141
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);
142+ // ...
143+ public function execute(InputInterface $input, OutputInterface $output)
144+ {
145+ // ...
146+ $helper = $this->getHelper('question');
147+ $question = new ChoiceQuestion(
148+ 'Please select your favorite colors (defaults to red and blue)',
149+ array('red', 'blue', 'yellow'),
150+ '0,1'
151+ );
152+ $question->setMultiselect(true);
132153
133- $colors = $helper->ask($input, $output, $question);
134- $output->writeln('You have just selected: ' . implode(', ', $colors));
154+ $colors = $helper->ask($input, $output, $question);
155+ $output->writeln('You have just selected: ' . implode(', ', $colors));
156+ }
135157
136158Now, when the user enters ``1,2 ``, the result will be:
137159``You have just selected: blue, yellow ``.
@@ -146,13 +168,17 @@ You can also specify an array of potential answers for a given question. These
146168will be autocompleted as the user types::
147169
148170 use Symfony\Component\Console\Question\Question;
149- // ...
150171
151- $bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
152- $question = new Question('Please enter the name of a bundle', 'FooBundle');
153- $question->setAutocompleterValues($bundles);
172+ // ...
173+ public function execute(InputInterface $input, OutputInterface $output)
174+ {
175+ // ...
176+ $bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');
177+ $question = new Question('Please enter the name of a bundle', 'FooBundle');
178+ $question->setAutocompleterValues($bundles);
154179
155- $name = $helper->ask($input, $output, $question);
180+ $name = $helper->ask($input, $output, $question);
181+ }
156182
157183Hiding the User's Response
158184~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,13 +187,17 @@ You can also ask a question and hide the response. This is particularly
161187convenient for passwords::
162188
163189 use Symfony\Component\Console\Question\Question;
164- // ...
165190
166- $question = new Question('What is the database password?');
167- $question->setHidden(true);
168- $question->setHiddenFallback(false);
191+ // ...
192+ public function execute(InputInterface $input, OutputInterface $output)
193+ {
194+ // ...
195+ $question = new Question('What is the database password?');
196+ $question->setHidden(true);
197+ $question->setHiddenFallback(false);
169198
170- $password = $helper->ask($input, $output, $question);
199+ $password = $helper->ask($input, $output, $question);
200+ }
171201
172202.. caution ::
173203
@@ -189,20 +219,24 @@ be suffixed with ``Bundle``. You can validate that by using the
189219method::
190220
191221 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);
204222
205- $name = $helper->ask($input, $output, $question);
223+ // ...
224+ public function execute(InputInterface $input, OutputInterface $output)
225+ {
226+ // ...
227+ $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
228+ $question->setValidator(function ($answer) {
229+ if ('Bundle' !== substr($answer, -6)) {
230+ throw new \RuntimeException(
231+ 'The name of the bundle should be suffixed with \'Bundle\''
232+ );
233+ }
234+ return $answer;
235+ });
236+ $question->setMaxAttempts(2);
237+
238+ $name = $helper->ask($input, $output, $question);
239+ }
206240
207241The ``$validator `` is a callback which handles the validation. It should
208242throw an exception if there is something wrong. The exception message is displayed
@@ -222,23 +256,26 @@ Validating a Hidden Response
222256You can also use a validator with a hidden question::
223257
224258 use Symfony\Component\Console\Question\Question;
225- // ...
226-
227- $helper = $this->getHelper('question');
228259
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- }
260+ // ...
261+ public function execute(InputInterface $input, OutputInterface $output)
262+ {
263+ // ...
264+ $helper = $this->getHelper('question');
234265
235- return $value;
236- });
237- $question->setHidden(true);
238- $question->setMaxAttempts(20);
266+ $question = new Question('Please enter your password');
267+ $question->setValidator(function ($value) {
268+ if (trim($value) == '') {
269+ throw new \Exception('The password can not be empty');
270+ }
239271
240- $password = $helper->ask($input, $output, $question);
272+ return $value;
273+ });
274+ $question->setHidden(true);
275+ $question->setMaxAttempts(20);
241276
277+ $password = $helper->ask($input, $output, $question);
278+ }
242279
243280Testing a Command that Expects Input
244281------------------------------------
@@ -276,6 +313,6 @@ from the command line, you need to set the helper input stream::
276313 }
277314
278315By 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
316+ console would do internally with all user input through the CLI . This way
280317you can test any user interaction (even complex ones) by passing an appropriate
281318input stream.
0 commit comments