@@ -34,14 +34,18 @@ 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);
41
41
42
42
if (!$helper->ask($input, $output, $question)) {
43
43
return Command::SUCCESS;
44
44
}
45
+
46
+ // ... do something here
47
+
48
+ return Command::SUCCESS;
45
49
}
46
50
}
47
51
@@ -75,12 +79,16 @@ if you want to know a bundle name, you can add this to your command::
75
79
use Symfony\Component\Console\Question\Question;
76
80
77
81
// ...
78
- public function execute(InputInterface $input, OutputInterface $output)
82
+ public function execute(InputInterface $input, OutputInterface $output): int
79
83
{
80
84
// ...
81
85
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
82
86
83
87
$bundleName = $helper->ask($input, $output, $question);
88
+
89
+ // ... do something with the bundleName
90
+
91
+ return Commande::SUCCESS;
84
92
}
85
93
86
94
The user will be asked "Please enter the name of the bundle". They can type
@@ -99,7 +107,7 @@ from a predefined list::
99
107
use Symfony\Component\Console\Question\ChoiceQuestion;
100
108
101
109
// ...
102
- public function execute(InputInterface $input, OutputInterface $output)
110
+ public function execute(InputInterface $input, OutputInterface $output): int
103
111
{
104
112
// ...
105
113
$helper = $this->getHelper('question');
@@ -115,6 +123,8 @@ from a predefined list::
115
123
$output->writeln('You have just selected: '.$color);
116
124
117
125
// ... do something with the color
126
+
127
+ return Commande::SUCCESS;
118
128
}
119
129
120
130
The option which should be selected by default is provided with the third
@@ -138,7 +148,7 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
138
148
use Symfony\Component\Console\Question\ChoiceQuestion;
139
149
140
150
// ...
141
- public function execute(InputInterface $input, OutputInterface $output)
151
+ public function execute(InputInterface $input, OutputInterface $output): int
142
152
{
143
153
// ...
144
154
$helper = $this->getHelper('question');
@@ -151,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
151
161
152
162
$colors = $helper->ask($input, $output, $question);
153
163
$output->writeln('You have just selected: ' . implode(', ', $colors));
164
+
165
+ return Commande::SUCCESS;
154
166
}
155
167
156
168
Now, when the user enters ``1,2 ``, the result will be:
@@ -168,7 +180,7 @@ will be autocompleted as the user types::
168
180
use Symfony\Component\Console\Question\Question;
169
181
170
182
// ...
171
- public function execute(InputInterface $input, OutputInterface $output)
183
+ public function execute(InputInterface $input, OutputInterface $output): int
172
184
{
173
185
// ...
174
186
$helper = $this->getHelper('question');
@@ -178,6 +190,10 @@ will be autocompleted as the user types::
178
190
$question->setAutocompleterValues($bundles);
179
191
180
192
$bundleName = $helper->ask($input, $output, $question);
193
+
194
+ // ... do something with the bundleName
195
+
196
+ return Commande::SUCCESS;
181
197
}
182
198
183
199
In more complex use cases, it may be necessary to generate suggestions on the
@@ -187,7 +203,7 @@ provide a callback function to dynamically generate suggestions::
187
203
use Symfony\Component\Console\Question\Question;
188
204
189
205
// ...
190
- public function execute(InputInterface $input, OutputInterface $output)
206
+ public function execute(InputInterface $input, OutputInterface $output): int
191
207
{
192
208
$helper = $this->getHelper('question');
193
209
@@ -213,6 +229,10 @@ provide a callback function to dynamically generate suggestions::
213
229
$question->setAutocompleterCallback($callback);
214
230
215
231
$filePath = $helper->ask($input, $output, $question);
232
+
233
+ // ... do something with the filePath
234
+
235
+ return Commande::SUCCESS;
216
236
}
217
237
218
238
Do not Trim the Answer
@@ -224,7 +244,7 @@ You can also specify if you want to not trim the answer by setting it directly w
224
244
use Symfony\Component\Console\Question\Question;
225
245
226
246
// ...
227
- public function execute(InputInterface $input, OutputInterface $output)
247
+ public function execute(InputInterface $input, OutputInterface $output): int
228
248
{
229
249
// ...
230
250
$helper = $this->getHelper('question');
@@ -233,6 +253,10 @@ You can also specify if you want to not trim the answer by setting it directly w
233
253
$question->setTrimmable(false);
234
254
// if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
235
255
$name = $helper->ask($input, $output, $question);
256
+
257
+ // ... do something with the name
258
+
259
+ return Commande::SUCCESS;
236
260
}
237
261
238
262
Accept Multiline Answers
@@ -246,7 +270,7 @@ the response to a question should allow multiline answers by passing ``true`` to
246
270
use Symfony\Component\Console\Question\Question;
247
271
248
272
// ...
249
- public function execute(InputInterface $input, OutputInterface $output)
273
+ public function execute(InputInterface $input, OutputInterface $output): int
250
274
{
251
275
// ...
252
276
$helper = $this->getHelper('question');
@@ -255,6 +279,10 @@ the response to a question should allow multiline answers by passing ``true`` to
255
279
$question->setMultiline(true);
256
280
257
281
$answer = $helper->ask($input, $output, $question);
282
+
283
+ // ... do something with the answer
284
+
285
+ return Commande::SUCCESS;
258
286
}
259
287
260
288
Multiline questions stop reading user input after receiving an end-of-transmission
@@ -269,7 +297,7 @@ convenient for passwords::
269
297
use Symfony\Component\Console\Question\Question;
270
298
271
299
// ...
272
- public function execute(InputInterface $input, OutputInterface $output)
300
+ public function execute(InputInterface $input, OutputInterface $output): int
273
301
{
274
302
// ...
275
303
$helper = $this->getHelper('question');
@@ -279,6 +307,10 @@ convenient for passwords::
279
307
$question->setHiddenFallback(false);
280
308
281
309
$password = $helper->ask($input, $output, $question);
310
+
311
+ // ... do something with the password
312
+
313
+ return Commande::SUCCESS;
282
314
}
283
315
284
316
.. caution ::
@@ -302,13 +334,15 @@ convenient for passwords::
302
334
use Symfony\Component\Console\Question\ChoiceQuestion;
303
335
304
336
// ...
305
- public function execute(InputInterface $input, OutputInterface $output)
337
+ public function execute(InputInterface $input, OutputInterface $output): int
306
338
{
307
339
// ...
308
340
$helper = $this->getHelper('question');
309
341
QuestionHelper::disableStty();
310
342
311
343
// ...
344
+
345
+ return Commande::SUCCESS;
312
346
}
313
347
314
348
Normalizing the Answer
@@ -324,7 +358,7 @@ method::
324
358
use Symfony\Component\Console\Question\Question;
325
359
326
360
// ...
327
- public function execute(InputInterface $input, OutputInterface $output)
361
+ public function execute(InputInterface $input, OutputInterface $output): int
328
362
{
329
363
// ...
330
364
$helper = $this->getHelper('question');
@@ -336,6 +370,10 @@ method::
336
370
});
337
371
338
372
$bundleName = $helper->ask($input, $output, $question);
373
+
374
+ // ... do something with the bundleName
375
+
376
+ return Commande::SUCCESS;
339
377
}
340
378
341
379
.. caution ::
@@ -358,7 +396,7 @@ method::
358
396
use Symfony\Component\Console\Question\Question;
359
397
360
398
// ...
361
- public function execute(InputInterface $input, OutputInterface $output)
399
+ public function execute(InputInterface $input, OutputInterface $output): int
362
400
{
363
401
// ...
364
402
$helper = $this->getHelper('question');
@@ -376,6 +414,10 @@ method::
376
414
$question->setMaxAttempts(2);
377
415
378
416
$bundleName = $helper->ask($input, $output, $question);
417
+
418
+ // ... do something with the bundleName
419
+
420
+ return Commande::SUCCESS;
379
421
}
380
422
381
423
The ``$validator `` is a callback which handles the validation. It should
@@ -414,7 +456,7 @@ You can also use a validator with a hidden question::
414
456
use Symfony\Component\Console\Question\Question;
415
457
416
458
// ...
417
- public function execute(InputInterface $input, OutputInterface $output)
459
+ public function execute(InputInterface $input, OutputInterface $output): int
418
460
{
419
461
// ...
420
462
$helper = $this->getHelper('question');
@@ -434,6 +476,10 @@ You can also use a validator with a hidden question::
434
476
$question->setMaxAttempts(20);
435
477
436
478
$password = $helper->ask($input, $output, $question);
479
+
480
+ // ... do something with the password
481
+
482
+ return Commande::SUCCESS;
437
483
}
438
484
439
485
Testing a Command that Expects Input
0 commit comments