4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Exception\LogicException;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Console\Question\ChoiceQuestion;
18 use Symfony\Component\Console\Question\ConfirmationQuestion;
19 use Symfony\Component\Console\Question\Question;
20 use Symfony\Component\Console\Style\SymfonyStyle;
21 use Symfony\Component\Console\Formatter\OutputFormatter;
24 * Symfony Style Guide compliant question helper.
26 * @author Kevin Bond <kevinbond@gmail.com>
28 class SymfonyQuestionHelper extends QuestionHelper
33 * To be removed in 4.0
35 public function ask(InputInterface $input, OutputInterface $output, Question $question)
37 $validator = $question->getValidator();
38 $question->setValidator(function ($value) use ($validator) {
39 if (null !== $validator) {
40 $value = $validator($value);
43 if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
44 @trigger_error('The default question validator is deprecated since Symfony 3.3 and will not be used anymore in version 4.0. Set a custom question validator if needed.', E_USER_DEPRECATED);
46 throw new LogicException('A value is required.');
53 return parent::ask($input, $output, $question);
59 protected function writePrompt(OutputInterface $output, Question $question)
61 $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
62 $default = $question->getDefault();
65 case null === $default:
66 $text = sprintf(' <info>%s</info>:', $text);
70 case $question instanceof ConfirmationQuestion:
71 $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
75 case $question instanceof ChoiceQuestion && $question->isMultiselect():
76 $choices = $question->getChoices();
77 $default = explode(',', $default);
79 foreach ($default as $key => $value) {
80 $default[$key] = $choices[trim($value)];
83 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
87 case $question instanceof ChoiceQuestion:
88 $choices = $question->getChoices();
89 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
94 $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
97 $output->writeln($text);
99 if ($question instanceof ChoiceQuestion) {
100 $width = max(array_map('strlen', array_keys($question->getChoices())));
102 foreach ($question->getChoices() as $key => $value) {
103 $output->writeln(sprintf(" [<comment>%-${width}s</comment>] %s", $key, $value));
107 $output->write(' > ');
113 protected function writeError(OutputInterface $output, \Exception $error)
115 if ($output instanceof SymfonyStyle) {
117 $output->error($error->getMessage());
122 parent::writeError($output, $error);