In Symfony there are three types of questions you can use in your CLI scripts:

- Question
- ChoiceQuestion
- ConfirmationQuestion

They can be used like this:

Question

The user can enter any text to answer this question. $userInput will be the entered string.

 

$helper = $this->getHelper('question');
$question = new Question(
    'What\'s your favorite color? '
);
$userInput = $helper->ask($input, $output, $question);

ChoiceQuestion

This is similar to the question above, but the user chooses between a fixed number of answers. When they enter a different answer the error will be displayed and the user has to answer the question again.

 

$colors = [
    'red' => 'red',
    'blue' => 'blue',
    'green' => 'green',
    'yellow' => 'yellow',
];
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
    'What\'s your favorite color? (Default: red)',
    $colors,
    'red'
);
$userInput = $helper->ask($input, $output, $question);
$question->setErrorMessage('The color "%s" does not exist.');

ConfirmationQuestion

The variable $userInput will be "Yes" or "No". The user has to enter something like "Y", "y" or "yes" for the $userInput to be "Yes". In every other case it will be "No".

 

$question = new ConfirmationQuestion(
    'Do you really think that ' . $userInput . ' is your favorite color? (y/n)',
);
$userInput = $helper->ask($input, $output, $question);