Question Helper

Question Helper

The Symfony\Component\Console\Helper\QuestionHelper provides functions to ask the user for more information. It is included in the default helper set and you can get it by calling [getHelper()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Command/Command.php "Symfony\Component\Console\Command\Command::getHelper()"):

  1. $helper = $this->getHelper('question');

The Question Helper has a single method [ask()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php "Symfony\Component\Console\Helper\QuestionHelper::ask()") that needs an Symfony\Component\Console\Input\InputInterface instance as the first argument, an Symfony\Component\Console\Output\OutputInterface instance as the second argument and a Symfony\Component\Console\Question\Question as last argument.

Asking the User for Confirmation

Suppose you want to confirm an action before actually executing it. Add the following to your command:

  1. // ...
  2. use Symfony\Component\Console\Command\Command;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Output\OutputInterface;
  5. use Symfony\Component\Console\Question\ConfirmationQuestion;
  6. class YourCommand extends Command
  7. {
  8. // ...
  9. public function execute(InputInterface $input, OutputInterface $output)
  10. {
  11. $helper = $this->getHelper('question');
  12. $question = new ConfirmationQuestion('Continue with this action?', false);
  13. if (!$helper->ask($input, $output, $question)) {
  14. return 0;
  15. }
  16. }
  17. }

In this case, the user will be asked “Continue with this action?”. If the user answers with y it returns true or false if they answer with n. The second argument to [__construct()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/ConfirmationQuestion.php "Symfony\Component\Console\Question\ConfirmationQuestion::__construct()") is the default value to return if the user doesn’t enter any valid input. If the second argument is not provided, true is assumed.

Tip

You can customize the regex used to check if the answer means “yes” in the third argument of the constructor. For instance, to allow anything that starts with either y or j, you would set it to:

  1. $question = new ConfirmationQuestion(
  2. 'Continue with this action?',
  3. false,
  4. '/^(y|j)/i'
  5. );

The regex defaults to /^y/i.

Asking the User for Information

You can also ask a question with more than a simple yes/no answer. For instance, if you want to know a bundle name, you can add this to your command:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
  7. $bundleName = $helper->ask($input, $output, $question);
  8. }

The user will be asked “Please enter the name of the bundle”. They can type some name which will be returned by the [ask()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Helper/QuestionHelper.php "Symfony\Component\Console\Helper\QuestionHelper::ask()") method. If they leave it empty, the default value (AcmeDemoBundle here) is returned.

Let the User Choose from a List of Answers

If you have a predefined set of answers the user can choose from, you could use a Symfony\Component\Console\Question\ChoiceQuestion which makes sure that the user can only enter a valid string from a predefined list:

  1. use Symfony\Component\Console\Question\ChoiceQuestion;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new ChoiceQuestion(
  8. 'Please select your favorite color (defaults to red)',
  9. ['red', 'blue', 'yellow'],
  10. 0
  11. );
  12. $question->setErrorMessage('Color %s is invalid.');
  13. $color = $helper->ask($input, $output, $question);
  14. $output->writeln('You have just selected: '.$color);
  15. // ... do something with the color
  16. }

The option which should be selected by default is provided with the third argument of the constructor. The default is null, which means that no option is the default one.

If the user enters an invalid string, an error message is shown and the user is asked to provide the answer another time, until they enter a valid string or reach the maximum number of attempts. The default value for the maximum number of attempts is null, which means infinite number of attempts. You can define your own error message using [setErrorMessage()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/ChoiceQuestion.php "Symfony\Component\Console\Question\ChoiceQuestion::setErrorMessage()").

Multiple Choices

Sometimes, multiple answers can be given. The ChoiceQuestion provides this feature using comma separated values. This is disabled by default, to enable this use [setMultiselect()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/ChoiceQuestion.php "Symfony\Component\Console\Question\ChoiceQuestion::setMultiselect()"):

  1. use Symfony\Component\Console\Question\ChoiceQuestion;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new ChoiceQuestion(
  8. 'Please select your favorite colors (defaults to red and blue)',
  9. ['red', 'blue', 'yellow'],
  10. '0,1'
  11. );
  12. $question->setMultiselect(true);
  13. $colors = $helper->ask($input, $output, $question);
  14. $output->writeln('You have just selected: ' . implode(', ', $colors));
  15. }

Now, when the user enters 1,2, the result will be: You have just selected: blue, yellow.

If the user does not enter anything, the result will be: You have just selected: red, blue.

Autocompletion

You can also specify an array of potential answers for a given question. These will be autocompleted as the user types:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $bundles = ['AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle'];
  8. $question = new Question('Please enter the name of a bundle', 'FooBundle');
  9. $question->setAutocompleterValues($bundles);
  10. $bundleName = $helper->ask($input, $output, $question);
  11. }

In more complex use cases, it may be necessary to generate suggestions on the fly, for instance if you wish to autocomplete a file path. In that case, you can provide a callback function to dynamically generate suggestions:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. $helper = $this->getHelper('question');
  6. // This function is called whenever the input changes and new
  7. // suggestions are needed.
  8. $callback = function (string $userInput): array {
  9. // Strip any characters from the last slash to the end of the string
  10. // to keep only the last directory and generate suggestions for it
  11. $inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $userInput);
  12. $inputPath = '' === $inputPath ? '.' : $inputPath;
  13. // CAUTION - this example code allows unrestricted access to the
  14. // entire filesystem. In real applications, restrict the directories
  15. // where files and dirs can be found
  16. $foundFilesAndDirs = @scandir($inputPath) ?: [];
  17. return array_map(function ($dirOrFile) use ($inputPath) {
  18. return $inputPath.$dirOrFile;
  19. }, $foundFilesAndDirs);
  20. };
  21. $question = new Question('Please provide the full path of a file to parse');
  22. $question->setAutocompleterCallback($callback);
  23. $filePath = $helper->ask($input, $output, $question);
  24. }

New in version 4.3: The setAutocompleterCallback() method was introduced in Symfony 4.3.

Do not Trim the Answer

You can also specify if you want to not trim the answer by setting it directly with [setTrimmable()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/Question.php "Symfony\Component\Console\Question\Question::setTrimmable()"):

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new Question('What is the name of the child?');
  8. $question->setTrimmable(false);
  9. // if the users inputs 'elsa ' it will not be trimmed and you will get 'elsa ' as value
  10. $name = $helper->ask($input, $output, $question);
  11. }

New in version 4.4: The setTrimmable() method was introduced in Symfony 4.4.

Hiding the User’s Response

You can also ask a question and hide the response. This is particularly convenient for passwords:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new Question('What is the database password?');
  8. $question->setHidden(true);
  9. $question->setHiddenFallback(false);
  10. $password = $helper->ask($input, $output, $question);
  11. }

Caution

When you ask for a hidden response, Symfony will use either a binary, change stty mode or use another trick to hide the response. If none is available, it will fallback and allow the response to be visible unless you set this behavior to false using [setHiddenFallback()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/Question.php "Symfony\Component\Console\Question\Question::setHiddenFallback()") like in the example above. In this case, a RuntimeException would be thrown.

Note

The stty command is used to get and set properties of the command line (such as getting the number of rows and columns or hiding the input text). On Windows systems, this stty command may generate gibberish output and mangle the input text. If that’s your case, disable it with this command:

  1. use Symfony\Component\Console\Helper\QuestionHelper;
  2. use Symfony\Component\Console\Question\ChoiceQuestion;
  3. // ...
  4. public function execute(InputInterface $input, OutputInterface $output)
  5. {
  6. // ...
  7. $helper = $this->getHelper('question');
  8. QuestionHelper::disableStty();
  9. // ...
  10. }

Normalizing the Answer

Before validating the answer, you can “normalize” it to fix minor errors or tweak it as needed. For instance, in a previous example you asked for the bundle name. In case the user adds white spaces around the name by mistake, you can trim the name before validating it. To do so, configure a normalizer using the [setNormalizer()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/Question.php "Symfony\Component\Console\Question\Question::setNormalizer()") method:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
  8. $question->setNormalizer(function ($value) {
  9. // $value can be null here
  10. return $value ? trim($value) : '';
  11. });
  12. $bundleName = $helper->ask($input, $output, $question);
  13. }

Caution

The normalizer is called first and the returned value is used as the input of the validator. If the answer is invalid, don’t throw exceptions in the normalizer and let the validator handle those errors.

Validating the Answer

You can even validate the answer. For instance, in a previous example you asked for the bundle name. Following the Symfony naming conventions, it should be suffixed with Bundle. You can validate that by using the [setValidator()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/Question.php "Symfony\Component\Console\Question\Question::setValidator()") method:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
  8. $question->setValidator(function ($answer) {
  9. if (!is_string($answer) || 'Bundle' !== substr($answer, -6)) {
  10. throw new \RuntimeException(
  11. 'The name of the bundle should be suffixed with \'Bundle\''
  12. );
  13. }
  14. return $answer;
  15. });
  16. $question->setMaxAttempts(2);
  17. $bundleName = $helper->ask($input, $output, $question);
  18. }

The $validator is a callback which handles the validation. It should throw an exception if there is something wrong. The exception message is displayed in the console, so it is a good practice to put some useful information in it. The callback function should also return the value of the user’s input if the validation was successful.

You can set the max number of times to ask with the [setMaxAttempts()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Question/Question.php "Symfony\Component\Console\Question\Question::setMaxAttempts()") method. If you reach this max number it will use the default value. Using null means the amount of attempts is infinite. The user will be asked as long as they provide an invalid answer and will only be able to proceed if their input is valid.

Validating a Hidden Response

You can also use a validator with a hidden question:

  1. use Symfony\Component\Console\Question\Question;
  2. // ...
  3. public function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. // ...
  6. $helper = $this->getHelper('question');
  7. $question = new Question('Please enter your password');
  8. $question->setValidator(function ($value) {
  9. if (trim($value) == '') {
  10. throw new \Exception('The password cannot be empty');
  11. }
  12. return $value;
  13. });
  14. $question->setHidden(true);
  15. $question->setMaxAttempts(20);
  16. $password = $helper->ask($input, $output, $question);
  17. }

Testing a Command that Expects Input

If you want to write a unit test for a command which expects some kind of input from the command line, you need to set the inputs that the command expects:

  1. use Symfony\Component\Console\Helper\HelperSet;
  2. use Symfony\Component\Console\Helper\QuestionHelper;
  3. use Symfony\Component\Console\Tester\CommandTester;
  4. // ...
  5. public function testExecute()
  6. {
  7. // ...
  8. $commandTester = new CommandTester($command);
  9. // Equals to a user inputting "Test" and hitting ENTER
  10. $commandTester->setInputs(['Test']);
  11. // Equals to a user inputting "This", "That" and hitting ENTER
  12. // This can be used for answering two separated questions for instance
  13. $commandTester->setInputs(['This', 'That']);
  14. // For simulating a positive answer to a confirmation question, adding an
  15. // additional input saying "yes" will work
  16. $commandTester->setInputs(['yes']);
  17. $commandTester->execute(['command' => $command->getName()]);
  18. // $this->assertRegExp('/.../', $commandTester->getDisplay());
  19. }

By calling [setInputs()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Tester/CommandTester.php "Symfony\Component\Console\Tester\CommandTester::setInputs()"), you imitate what the console would do internally with all user input through the CLI. This method takes an array as only argument with, for each input that the command expects, a string representing what the user would have typed. This way you can test any user interaction (even complex ones) by passing the appropriate inputs.

Note

The Symfony\Component\Console\Tester\CommandTester automatically simulates a user hitting ENTER after each input, no need for passing an additional input.

Caution

On Windows systems Symfony uses a special binary to implement hidden questions. This means that those questions don’t use the default Input console object and therefore you can’t test them on Windows.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.