@@ -13,7 +13,7 @@ helper set, which you can get by calling
13
13
14
14
The Question Helper has a single method
15
15
: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
17
17
first argument, an :class: `Symfony\\ Component\\ Console\\ Output\\ OutputInterface `
18
18
instance as the second argument and a
19
19
:class: `Symfony\\ Component\\ Console\\ Question\\ Question ` as last argument.
@@ -24,14 +24,22 @@ Asking the User for Confirmation
24
24
Suppose you want to confirm an action before actually executing it. Add
25
25
the following to your command::
26
26
27
- use Symfony\Component\Console\Question\ConfirmationQuestion;
28
27
// ...
28
+ use Symfony\Component\Console\Question\ConfirmationQuestion;
29
29
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);
32
38
33
- if (!$helper->ask($input, $output, $question)) {
34
- return;
39
+ if (!$helper->ask($input, $output, $question)) {
40
+ return;
41
+ }
42
+ }
35
43
}
36
44
37
45
In 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,
66
74
if you want to know a bundle name, you can add this to your command::
67
75
68
76
use Symfony\Component\Console\Question\Question;
69
- // ...
70
77
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');
72
83
73
- $bundle = $helper->ask($input, $output, $question);
84
+ $bundle = $helper->ask($input, $output, $question);
85
+ }
74
86
75
87
The user will be asked "Please enter the name of the bundle". They can type
76
88
some name which will be returned by the
@@ -86,20 +98,24 @@ which makes sure that the user can only enter a valid string
86
98
from a predefined list::
87
99
88
100
use Symfony\Component\Console\Question\ChoiceQuestion;
89
- // ...
90
101
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.');
98
113
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);
101
116
102
- // ... do something with the color
117
+ // ... do something with the color
118
+ }
103
119
104
120
The option which should be selected by default is provided with the third
105
121
argument 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
120
136
this use :method: `Symfony\\ Component\\ Console\\ Question\\ ChoiceQuestion::setMultiselect `::
121
137
122
138
use Symfony\Component\Console\Question\ChoiceQuestion;
123
- // ...
124
139
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);
132
151
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
+ }
135
155
136
156
Now, when the user enters ``1,2 ``, the result will be:
137
157
``You have just selected: blue, yellow ``.
@@ -146,13 +166,17 @@ You can also specify an array of potential answers for a given question. These
146
166
will be autocompleted as the user types::
147
167
148
168
use Symfony\Component\Console\Question\Question;
149
- // ...
150
169
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);
154
177
155
- $name = $helper->ask($input, $output, $question);
178
+ $name = $helper->ask($input, $output, $question);
179
+ }
156
180
157
181
Hiding the User's Response
158
182
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,13 +185,17 @@ You can also ask a question and hide the response. This is particularly
161
185
convenient for passwords::
162
186
163
187
use Symfony\Component\Console\Question\Question;
164
- // ...
165
188
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);
169
196
170
- $password = $helper->ask($input, $output, $question);
197
+ $password = $helper->ask($input, $output, $question);
198
+ }
171
199
172
200
.. caution ::
173
201
@@ -189,20 +217,24 @@ be suffixed with ``Bundle``. You can validate that by using the
189
217
method::
190
218
191
219
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);
204
220
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
+ }
206
238
207
239
The ``$validator `` is a callback which handles the validation. It should
208
240
throw an exception if there is something wrong. The exception message is displayed
@@ -222,23 +254,26 @@ Validating a Hidden Response
222
254
You can also use a validator with a hidden question::
223
255
224
256
use Symfony\Component\Console\Question\Question;
225
- // ...
226
-
227
- $helper = $this->getHelper('question');
228
257
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');
234
263
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
+ }
239
269
240
- $password = $helper->ask($input, $output, $question);
270
+ return $value;
271
+ });
272
+ $question->setHidden(true);
273
+ $question->setMaxAttempts(20);
241
274
275
+ $password = $helper->ask($input, $output, $question);
276
+ }
242
277
243
278
Testing a Command that Expects Input
244
279
------------------------------------
@@ -276,6 +311,6 @@ from the command line, you need to set the helper input stream::
276
311
}
277
312
278
313
By 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
280
315
you can test any user interaction (even complex ones) by passing an appropriate
281
316
input stream.
0 commit comments