@@ -2,11 +2,9 @@ Question Helper
2
2
===============
3
3
4
4
The :class: `Symfony\\ Component\\ Console\\ Helper\\ QuestionHelper ` provides
5
- functions to ask the user for more information. It is included in the default
6
- helper set and you can get it by calling
7
- :method: `Symfony\\ Component\\ Console\\ Command\\ Command::getHelper `::
5
+ functions to ask the user for more information::
8
6
9
- $helper = $this->getHelper('question' );
7
+ $helper = new QuestionHelper( );
10
8
11
9
The Question Helper has a single method
12
10
:method: `Symfony\\ Component\\ Console\\ Helper\\ QuestionHelper::ask ` that needs an
@@ -27,6 +25,7 @@ Suppose you want to confirm an action before actually executing it. Add
27
25
the following to your command::
28
26
29
27
// ...
28
+ use Symfony\Component\Console\Application;
30
29
use Symfony\Component\Console\Attribute\AsCommand;
31
30
use Symfony\Component\Console\Command\Command;
32
31
use Symfony\Component\Console\Input\InputInterface;
@@ -38,7 +37,7 @@ the following to your command::
38
37
{
39
38
public function __invoke(InputInterface $input, OutputInterface $output): int
40
39
{
41
- $helper = $this->getHelper('question' );
40
+ $helper = new QuestionHelper( );
42
41
$question = new ConfirmationQuestion('Continue with this action?', false);
43
42
44
43
if (!$helper->ask($input, $output, $question)) {
@@ -91,7 +90,7 @@ if you want to know a bundle name, you can add this to your command::
91
90
use Symfony\Component\Console\Question\Question;
92
91
93
92
// ...
94
- public function execute (InputInterface $input, OutputInterface $output): int
93
+ public function __invoke (InputInterface $input, OutputInterface $output): int
95
94
{
96
95
// ...
97
96
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
@@ -121,10 +120,10 @@ but ``red`` could be set instead (could be more explicit)::
121
120
use Symfony\Component\Console\Question\ChoiceQuestion;
122
121
123
122
// ...
124
- public function execute (InputInterface $input, OutputInterface $output): int
123
+ public function __invoke (InputInterface $input, OutputInterface $output): int
125
124
{
126
125
// ...
127
- $helper = $this->getHelper('question' );
126
+ $helper = new QuestionHelper( );
128
127
$question = new ChoiceQuestion(
129
128
'Please select your favorite color (defaults to red)',
130
129
// choices can also be PHP objects that implement __toString() method
@@ -184,10 +183,10 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
184
183
use Symfony\Component\Console\Question\ChoiceQuestion;
185
184
186
185
// ...
187
- public function execute (InputInterface $input, OutputInterface $output): int
186
+ public function __invoke (InputInterface $input, OutputInterface $output): int
188
187
{
189
188
// ...
190
- $helper = $this->getHelper('question' );
189
+ $helper = new QuestionHelper( );
191
190
$question = new ChoiceQuestion(
192
191
'Please select your favorite colors (defaults to red and blue)',
193
192
['red', 'blue', 'yellow'],
@@ -218,10 +217,10 @@ will be autocompleted as the user types::
218
217
use Symfony\Component\Console\Question\Question;
219
218
220
219
// ...
221
- public function execute (InputInterface $input, OutputInterface $output): int
220
+ public function __invoke (InputInterface $input, OutputInterface $output): int
222
221
{
223
222
// ...
224
- $helper = $this->getHelper('question' );
223
+ $helper = new QuestionHelper( );
225
224
226
225
$bundles = ['AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle'];
227
226
$question = new Question('Please enter the name of a bundle', 'FooBundle');
@@ -241,9 +240,9 @@ provide a callback function to dynamically generate suggestions::
241
240
use Symfony\Component\Console\Question\Question;
242
241
243
242
// ...
244
- public function execute (InputInterface $input, OutputInterface $output): int
243
+ public function __invoke (InputInterface $input, OutputInterface $output): int
245
244
{
246
- $helper = $this->getHelper('question' );
245
+ $helper = new QuestionHelper( );
247
246
248
247
// This function is called whenever the input changes and new
249
248
// suggestions are needed.
@@ -282,10 +281,10 @@ You can also specify if you want to not trim the answer by setting it directly w
282
281
use Symfony\Component\Console\Question\Question;
283
282
284
283
// ...
285
- public function execute (InputInterface $input, OutputInterface $output): int
284
+ public function __invoke (InputInterface $input, OutputInterface $output): int
286
285
{
287
286
// ...
288
- $helper = $this->getHelper('question' );
287
+ $helper = new QuestionHelper( );
289
288
290
289
$question = new Question('What is the name of the child?');
291
290
$question->setTrimmable(false);
@@ -308,10 +307,10 @@ the response to a question should allow multiline answers by passing ``true`` to
308
307
use Symfony\Component\Console\Question\Question;
309
308
310
309
// ...
311
- public function execute (InputInterface $input, OutputInterface $output): int
310
+ public function __invoke (InputInterface $input, OutputInterface $output): int
312
311
{
313
312
// ...
314
- $helper = $this->getHelper('question' );
313
+ $helper = new QuestionHelper( );
315
314
316
315
$question = new Question('How do you solve world peace?');
317
316
$question->setMultiline(true);
@@ -335,10 +334,10 @@ convenient for passwords::
335
334
use Symfony\Component\Console\Question\Question;
336
335
337
336
// ...
338
- public function execute (InputInterface $input, OutputInterface $output): int
337
+ public function __invoke (InputInterface $input, OutputInterface $output): int
339
338
{
340
339
// ...
341
- $helper = $this->getHelper('question' );
340
+ $helper = new QuestionHelper( );
342
341
343
342
$question = new Question('What is the database password?');
344
343
$question->setHidden(true);
@@ -372,10 +371,10 @@ convenient for passwords::
372
371
use Symfony\Component\Console\Question\ChoiceQuestion;
373
372
374
373
// ...
375
- public function execute (InputInterface $input, OutputInterface $output): int
374
+ public function __invoke (InputInterface $input, OutputInterface $output): int
376
375
{
377
376
// ...
378
- $helper = $this->getHelper('question' );
377
+ $helper = new QuestionHelper( );
379
378
QuestionHelper::disableStty();
380
379
381
380
// ...
@@ -396,10 +395,10 @@ method::
396
395
use Symfony\Component\Console\Question\Question;
397
396
398
397
// ...
399
- public function execute (InputInterface $input, OutputInterface $output): int
398
+ public function __invoke (InputInterface $input, OutputInterface $output): int
400
399
{
401
400
// ...
402
- $helper = $this->getHelper('question' );
401
+ $helper = new QuestionHelper( );
403
402
404
403
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
405
404
$question->setNormalizer(function (string $value): string {
@@ -434,10 +433,10 @@ method::
434
433
use Symfony\Component\Console\Question\Question;
435
434
436
435
// ...
437
- public function execute (InputInterface $input, OutputInterface $output): int
436
+ public function __invoke (InputInterface $input, OutputInterface $output): int
438
437
{
439
438
// ...
440
- $helper = $this->getHelper('question' );
439
+ $helper = new QuestionHelper( );
441
440
442
441
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
443
442
$question->setValidator(function (string $answer): string {
@@ -494,10 +493,10 @@ You can also use a validator with a hidden question::
494
493
use Symfony\Component\Console\Question\Question;
495
494
496
495
// ...
497
- public function execute (InputInterface $input, OutputInterface $output): int
496
+ public function __invoke (InputInterface $input, OutputInterface $output): int
498
497
{
499
498
// ...
500
- $helper = $this->getHelper('question' );
499
+ $helper = new QuestionHelper( );
501
500
502
501
$question = new Question('Please enter your password');
503
502
$question->setNormalizer(function (?string $value): string {
0 commit comments