@@ -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,24 @@ 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\Input\InputInterface;
29
+ use Symfony\Component\Console\Output\OutputInterface;
30
+ use Symfony\Component\Console\Question\ConfirmationQuestion;
29
31
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);
32
40
33
- if (!$helper->ask($input, $output, $question)) {
34
- return;
41
+ if (!$helper->ask($input, $output, $question)) {
42
+ return;
43
+ }
44
+ }
35
45
}
36
46
37
47
In 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,
66
76
if you want to know a bundle name, you can add this to your command::
67
77
68
78
use Symfony\Component\Console\Question\Question;
69
- // ...
70
79
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');
72
85
73
- $bundle = $helper->ask($input, $output, $question);
86
+ $bundle = $helper->ask($input, $output, $question);
87
+ }
74
88
75
89
The user will be asked "Please enter the name of the bundle". They can type
76
90
some name which will be returned by the
@@ -86,20 +100,24 @@ which makes sure that the user can only enter a valid string
86
100
from a predefined list::
87
101
88
102
use Symfony\Component\Console\Question\ChoiceQuestion;
89
- // ...
90
103
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.');
98
115
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);
101
118
102
- // ... do something with the color
119
+ // ... do something with the color
120
+ }
103
121
104
122
The option which should be selected by default is provided with the third
105
123
argument 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
120
138
this use :method: `Symfony\\ Component\\ Console\\ Question\\ ChoiceQuestion::setMultiselect `::
121
139
122
140
use Symfony\Component\Console\Question\ChoiceQuestion;
123
- // ...
124
141
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);
132
153
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
+ }
135
157
136
158
Now, when the user enters ``1,2 ``, the result will be:
137
159
``You have just selected: blue, yellow ``.
@@ -146,13 +168,17 @@ You can also specify an array of potential answers for a given question. These
146
168
will be autocompleted as the user types::
147
169
148
170
use Symfony\Component\Console\Question\Question;
149
- // ...
150
171
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);
154
179
155
- $name = $helper->ask($input, $output, $question);
180
+ $name = $helper->ask($input, $output, $question);
181
+ }
156
182
157
183
Hiding the User's Response
158
184
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -161,13 +187,17 @@ You can also ask a question and hide the response. This is particularly
161
187
convenient for passwords::
162
188
163
189
use Symfony\Component\Console\Question\Question;
164
- // ...
165
190
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);
169
198
170
- $password = $helper->ask($input, $output, $question);
199
+ $password = $helper->ask($input, $output, $question);
200
+ }
171
201
172
202
.. caution ::
173
203
@@ -189,20 +219,24 @@ be suffixed with ``Bundle``. You can validate that by using the
189
219
method::
190
220
191
221
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
222
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
+ }
206
240
207
241
The ``$validator `` is a callback which handles the validation. It should
208
242
throw an exception if there is something wrong. The exception message is displayed
@@ -222,23 +256,26 @@ Validating a Hidden Response
222
256
You can also use a validator with a hidden question::
223
257
224
258
use Symfony\Component\Console\Question\Question;
225
- // ...
226
-
227
- $helper = $this->getHelper('question');
228
259
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');
234
265
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
+ }
239
271
240
- $password = $helper->ask($input, $output, $question);
272
+ return $value;
273
+ });
274
+ $question->setHidden(true);
275
+ $question->setMaxAttempts(20);
241
276
277
+ $password = $helper->ask($input, $output, $question);
278
+ }
242
279
243
280
Testing a Command that Expects Input
244
281
------------------------------------
@@ -276,6 +313,6 @@ from the command line, you need to set the helper input stream::
276
313
}
277
314
278
315
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
316
+ console would do internally with all user input through the CLI . This way
280
317
you can test any user interaction (even complex ones) by passing an appropriate
281
318
input stream.
0 commit comments