@@ -34,7 +34,7 @@ the following to your command::
34
34
{
35
35
// ...
36
36
37
- public function execute(InputInterface $input, OutputInterface $output)
37
+ public function execute(InputInterface $input, OutputInterface $output): int
38
38
{
39
39
$helper = $this->getHelper('question');
40
40
$question = new ConfirmationQuestion('Continue with this action?', false);
@@ -75,12 +75,16 @@ if you want to know a bundle name, you can add this to your command::
75
75
use Symfony\Component\Console\Question\Question;
76
76
77
77
// ...
78
- public function execute(InputInterface $input, OutputInterface $output)
78
+ public function execute(InputInterface $input, OutputInterface $output): int
79
79
{
80
80
// ...
81
81
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
82
82
83
83
$bundleName = $helper->ask($input, $output, $question);
84
+
85
+ // ... do something with the bundleName
86
+
87
+ return Commande::SUCCESS;
84
88
}
85
89
86
90
The user will be asked "Please enter the name of the bundle". They can type
@@ -99,7 +103,7 @@ from a predefined list::
99
103
use Symfony\Component\Console\Question\ChoiceQuestion;
100
104
101
105
// ...
102
- public function execute(InputInterface $input, OutputInterface $output)
106
+ public function execute(InputInterface $input, OutputInterface $output): int
103
107
{
104
108
// ...
105
109
$helper = $this->getHelper('question');
@@ -115,6 +119,8 @@ from a predefined list::
115
119
$output->writeln('You have just selected: '.$color);
116
120
117
121
// ... do something with the color
122
+
123
+ return Commande::SUCCESS;
118
124
}
119
125
120
126
.. versionadded :: 5.2
@@ -142,7 +148,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
142
148
use Symfony\Component\Console\Question\ChoiceQuestion;
143
149
144
150
// ...
145
- public function execute(InputInterface $input, OutputInterface $output)
151
+ public function execute(InputInterface $input, OutputInterface $output): int
146
152
{
147
153
// ...
148
154
$helper = $this->getHelper('question');
@@ -155,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
155
161
156
162
$colors = $helper->ask($input, $output, $question);
157
163
$output->writeln('You have just selected: ' . implode(', ', $colors));
164
+
165
+ return Commande::SUCCESS;
158
166
}
159
167
160
168
Now, when the user enters ``1,2 ``, the result will be:
@@ -172,7 +180,7 @@ will be autocompleted as the user types::
172
180
use Symfony\Component\Console\Question\Question;
173
181
174
182
// ...
175
- public function execute(InputInterface $input, OutputInterface $output)
183
+ public function execute(InputInterface $input, OutputInterface $output): int
176
184
{
177
185
// ...
178
186
$helper = $this->getHelper('question');
@@ -182,6 +190,10 @@ will be autocompleted as the user types::
182
190
$question->setAutocompleterValues($bundles);
183
191
184
192
$bundleName = $helper->ask($input, $output, $question);
193
+
194
+ // ... do something with the bundleName
195
+
196
+ return Commande::SUCCESS;
185
197
}
186
198
187
199
In more complex use cases, it may be necessary to generate suggestions on the
@@ -191,7 +203,7 @@ provide a callback function to dynamically generate suggestions::
191
203
use Symfony\Component\Console\Question\Question;
192
204
193
205
// ...
194
- public function execute(InputInterface $input, OutputInterface $output)
206
+ public function execute(InputInterface $input, OutputInterface $output): int
195
207
{
196
208
$helper = $this->getHelper('question');
197
209
@@ -217,6 +229,10 @@ provide a callback function to dynamically generate suggestions::
217
229
$question->setAutocompleterCallback($callback);
218
230
219
231
$filePath = $helper->ask($input, $output, $question);
232
+
233
+ // ... do something with the filePath
234
+
235
+ return Commande::SUCCESS;
220
236
}
221
237
222
238
Do not Trim the Answer
@@ -228,7 +244,7 @@ You can also specify if you want to not trim the answer by setting it directly w
228
244
use Symfony\Component\Console\Question\Question;
229
245
230
246
// ...
231
- public function execute(InputInterface $input, OutputInterface $output)
247
+ public function execute(InputInterface $input, OutputInterface $output): int
232
248
{
233
249
// ...
234
250
$helper = $this->getHelper('question');
@@ -237,6 +253,10 @@ You can also specify if you want to not trim the answer by setting it directly w
237
253
$question->setTrimmable(false);
238
254
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
239
255
$name = $helper->ask($input, $output, $question);
256
+
257
+ // ... do something with the name
258
+
259
+ return Commande::SUCCESS;
240
260
}
241
261
242
262
Accept Multiline Answers
@@ -255,7 +275,7 @@ the response to a question should allow multiline answers by passing ``true`` to
255
275
use Symfony\Component\Console\Question\Question;
256
276
257
277
// ...
258
- public function execute(InputInterface $input, OutputInterface $output)
278
+ public function execute(InputInterface $input, OutputInterface $output): int
259
279
{
260
280
// ...
261
281
$helper = $this->getHelper('question');
@@ -264,6 +284,10 @@ the response to a question should allow multiline answers by passing ``true`` to
264
284
$question->setMultiline(true);
265
285
266
286
$answer = $helper->ask($input, $output, $question);
287
+
288
+ // ... do something with the answer
289
+
290
+ return Commande::SUCCESS;
267
291
}
268
292
269
293
Multiline questions stop reading user input after receiving an end-of-transmission
@@ -278,7 +302,7 @@ convenient for passwords::
278
302
use Symfony\Component\Console\Question\Question;
279
303
280
304
// ...
281
- public function execute(InputInterface $input, OutputInterface $output)
305
+ public function execute(InputInterface $input, OutputInterface $output): int
282
306
{
283
307
// ...
284
308
$helper = $this->getHelper('question');
@@ -288,6 +312,10 @@ convenient for passwords::
288
312
$question->setHiddenFallback(false);
289
313
290
314
$password = $helper->ask($input, $output, $question);
315
+
316
+ // ... do something with the password
317
+
318
+ return Commande::SUCCESS;
291
319
}
292
320
293
321
.. caution ::
@@ -311,13 +339,15 @@ convenient for passwords::
311
339
use Symfony\Component\Console\Question\ChoiceQuestion;
312
340
313
341
// ...
314
- public function execute(InputInterface $input, OutputInterface $output)
342
+ public function execute(InputInterface $input, OutputInterface $output): int
315
343
{
316
344
// ...
317
345
$helper = $this->getHelper('question');
318
346
QuestionHelper::disableStty();
319
347
320
348
// ...
349
+
350
+ return Commande::SUCCESS;
321
351
}
322
352
323
353
Normalizing the Answer
@@ -333,7 +363,7 @@ method::
333
363
use Symfony\Component\Console\Question\Question;
334
364
335
365
// ...
336
- public function execute(InputInterface $input, OutputInterface $output)
366
+ public function execute(InputInterface $input, OutputInterface $output): int
337
367
{
338
368
// ...
339
369
$helper = $this->getHelper('question');
@@ -345,6 +375,10 @@ method::
345
375
});
346
376
347
377
$bundleName = $helper->ask($input, $output, $question);
378
+
379
+ // ... do something with the bundleName
380
+
381
+ return Commande::SUCCESS;
348
382
}
349
383
350
384
.. caution ::
@@ -367,7 +401,7 @@ method::
367
401
use Symfony\Component\Console\Question\Question;
368
402
369
403
// ...
370
- public function execute(InputInterface $input, OutputInterface $output)
404
+ public function execute(InputInterface $input, OutputInterface $output): int
371
405
{
372
406
// ...
373
407
$helper = $this->getHelper('question');
@@ -385,6 +419,10 @@ method::
385
419
$question->setMaxAttempts(2);
386
420
387
421
$bundleName = $helper->ask($input, $output, $question);
422
+
423
+ // ... do something with the bundleName
424
+
425
+ return Commande::SUCCESS;
388
426
}
389
427
390
428
The ``$validator `` is a callback which handles the validation. It should
@@ -423,7 +461,7 @@ You can also use a validator with a hidden question::
423
461
use Symfony\Component\Console\Question\Question;
424
462
425
463
// ...
426
- public function execute(InputInterface $input, OutputInterface $output)
464
+ public function execute(InputInterface $input, OutputInterface $output): int
427
465
{
428
466
// ...
429
467
$helper = $this->getHelper('question');
@@ -443,6 +481,10 @@ You can also use a validator with a hidden question::
443
481
$question->setMaxAttempts(20);
444
482
445
483
$password = $helper->ask($input, $output, $question);
484
+
485
+ // ... do something with the password
486
+
487
+ return Commande::SUCCESS;
446
488
}
447
489
448
490
Testing a Command that Expects Input
0 commit comments