@@ -34,14 +34,18 @@ the following to your command::
3434 {
3535 // ...
3636
37- public function execute(InputInterface $input, OutputInterface $output)
37+ public function execute(InputInterface $input, OutputInterface $output): int
3838 {
3939 $helper = $this->getHelper('question');
4040 $question = new ConfirmationQuestion('Continue with this action?', false);
4141
4242 if (!$helper->ask($input, $output, $question)) {
4343 return Command::SUCCESS;
4444 }
45+
46+ // ... do something here
47+
48+ return Command::SUCCESS;
4549 }
4650 }
4751
@@ -75,12 +79,16 @@ if you want to know a bundle name, you can add this to your command::
7579 use Symfony\Component\Console\Question\Question;
7680
7781 // ...
78- public function execute(InputInterface $input, OutputInterface $output)
82+ public function execute(InputInterface $input, OutputInterface $output): int
7983 {
8084 // ...
8185 $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
8286
8387 $bundleName = $helper->ask($input, $output, $question);
88+
89+ // ... do something with the bundleName
90+
91+ return Commande::SUCCESS;
8492 }
8593
8694The user will be asked "Please enter the name of the bundle". They can type
@@ -99,7 +107,7 @@ from a predefined list::
99107 use Symfony\Component\Console\Question\ChoiceQuestion;
100108
101109 // ...
102- public function execute(InputInterface $input, OutputInterface $output)
110+ public function execute(InputInterface $input, OutputInterface $output): int
103111 {
104112 // ...
105113 $helper = $this->getHelper('question');
@@ -115,6 +123,8 @@ from a predefined list::
115123 $output->writeln('You have just selected: '.$color);
116124
117125 // ... do something with the color
126+
127+ return Commande::SUCCESS;
118128 }
119129
120130The 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
138148 use Symfony\Component\Console\Question\ChoiceQuestion;
139149
140150 // ...
141- public function execute(InputInterface $input, OutputInterface $output)
151+ public function execute(InputInterface $input, OutputInterface $output): int
142152 {
143153 // ...
144154 $helper = $this->getHelper('question');
@@ -151,6 +161,8 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
151161
152162 $colors = $helper->ask($input, $output, $question);
153163 $output->writeln('You have just selected: ' . implode(', ', $colors));
164+
165+ return Commande::SUCCESS;
154166 }
155167
156168Now, when the user enters ``1,2 ``, the result will be:
@@ -168,7 +180,7 @@ will be autocompleted as the user types::
168180 use Symfony\Component\Console\Question\Question;
169181
170182 // ...
171- public function execute(InputInterface $input, OutputInterface $output)
183+ public function execute(InputInterface $input, OutputInterface $output): int
172184 {
173185 // ...
174186 $helper = $this->getHelper('question');
@@ -178,6 +190,10 @@ will be autocompleted as the user types::
178190 $question->setAutocompleterValues($bundles);
179191
180192 $bundleName = $helper->ask($input, $output, $question);
193+
194+ // ... do something with the bundleName
195+
196+ return Commande::SUCCESS;
181197 }
182198
183199In 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::
187203 use Symfony\Component\Console\Question\Question;
188204
189205 // ...
190- public function execute(InputInterface $input, OutputInterface $output)
206+ public function execute(InputInterface $input, OutputInterface $output): int
191207 {
192208 $helper = $this->getHelper('question');
193209
@@ -213,6 +229,10 @@ provide a callback function to dynamically generate suggestions::
213229 $question->setAutocompleterCallback($callback);
214230
215231 $filePath = $helper->ask($input, $output, $question);
232+
233+ // ... do something with the filePath
234+
235+ return Commande::SUCCESS;
216236 }
217237
218238Do 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
224244 use Symfony\Component\Console\Question\Question;
225245
226246 // ...
227- public function execute(InputInterface $input, OutputInterface $output)
247+ public function execute(InputInterface $input, OutputInterface $output): int
228248 {
229249 // ...
230250 $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
233253 $question->setTrimmable(false);
234254 // if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
235255 $name = $helper->ask($input, $output, $question);
256+
257+ // ... do something with the name
258+
259+ return Commande::SUCCESS;
236260 }
237261
238262Accept Multiline Answers
@@ -246,7 +270,7 @@ the response to a question should allow multiline answers by passing ``true`` to
246270 use Symfony\Component\Console\Question\Question;
247271
248272 // ...
249- public function execute(InputInterface $input, OutputInterface $output)
273+ public function execute(InputInterface $input, OutputInterface $output): int
250274 {
251275 // ...
252276 $helper = $this->getHelper('question');
@@ -255,6 +279,10 @@ the response to a question should allow multiline answers by passing ``true`` to
255279 $question->setMultiline(true);
256280
257281 $answer = $helper->ask($input, $output, $question);
282+
283+ // ... do something with the answer
284+
285+ return Commande::SUCCESS;
258286 }
259287
260288Multiline questions stop reading user input after receiving an end-of-transmission
@@ -269,7 +297,7 @@ convenient for passwords::
269297 use Symfony\Component\Console\Question\Question;
270298
271299 // ...
272- public function execute(InputInterface $input, OutputInterface $output)
300+ public function execute(InputInterface $input, OutputInterface $output): int
273301 {
274302 // ...
275303 $helper = $this->getHelper('question');
@@ -279,6 +307,10 @@ convenient for passwords::
279307 $question->setHiddenFallback(false);
280308
281309 $password = $helper->ask($input, $output, $question);
310+
311+ // ... do something with the password
312+
313+ return Commande::SUCCESS;
282314 }
283315
284316.. caution ::
@@ -302,13 +334,15 @@ convenient for passwords::
302334 use Symfony\Component\Console\Question\ChoiceQuestion;
303335
304336 // ...
305- public function execute(InputInterface $input, OutputInterface $output)
337+ public function execute(InputInterface $input, OutputInterface $output): int
306338 {
307339 // ...
308340 $helper = $this->getHelper('question');
309341 QuestionHelper::disableStty();
310342
311343 // ...
344+
345+ return Commande::SUCCESS;
312346 }
313347
314348Normalizing the Answer
@@ -324,7 +358,7 @@ method::
324358 use Symfony\Component\Console\Question\Question;
325359
326360 // ...
327- public function execute(InputInterface $input, OutputInterface $output)
361+ public function execute(InputInterface $input, OutputInterface $output): int
328362 {
329363 // ...
330364 $helper = $this->getHelper('question');
@@ -336,6 +370,10 @@ method::
336370 });
337371
338372 $bundleName = $helper->ask($input, $output, $question);
373+
374+ // ... do something with the bundleName
375+
376+ return Commande::SUCCESS;
339377 }
340378
341379.. caution ::
@@ -358,7 +396,7 @@ method::
358396 use Symfony\Component\Console\Question\Question;
359397
360398 // ...
361- public function execute(InputInterface $input, OutputInterface $output)
399+ public function execute(InputInterface $input, OutputInterface $output): int
362400 {
363401 // ...
364402 $helper = $this->getHelper('question');
@@ -376,6 +414,10 @@ method::
376414 $question->setMaxAttempts(2);
377415
378416 $bundleName = $helper->ask($input, $output, $question);
417+
418+ // ... do something with the bundleName
419+
420+ return Commande::SUCCESS;
379421 }
380422
381423The ``$validator `` is a callback which handles the validation. It should
@@ -414,7 +456,7 @@ You can also use a validator with a hidden question::
414456 use Symfony\Component\Console\Question\Question;
415457
416458 // ...
417- public function execute(InputInterface $input, OutputInterface $output)
459+ public function execute(InputInterface $input, OutputInterface $output): int
418460 {
419461 // ...
420462 $helper = $this->getHelper('question');
@@ -434,6 +476,10 @@ You can also use a validator with a hidden question::
434476 $question->setMaxAttempts(20);
435477
436478 $password = $helper->ask($input, $output, $question);
479+
480+ // ... do something with the password
481+
482+ return Commande::SUCCESS;
437483 }
438484
439485Testing a Command that Expects Input
0 commit comments