Symfony\Component\Console\Helper\DialogHelper::ask PHP Méthode

ask() public méthode

Asks a question to the user.
public ask ( Symfony\Component\Console\Output\OutputInterface $output, string | array $question, string $default = null ) : string
$output Symfony\Component\Console\Output\OutputInterface
$question string | array The question to ask
$default string The default answer if none is given by the user
Résultat string The user answer
    public function ask(OutputInterface $output, $question, $default = null)
    {
        $output->write($question);

        if (false === $ret = stream_get_line(null === $this->inputStream ? STDIN : $this->inputStream, 4096, "\n")) {
            throw new \RuntimeException('Aborted');
        }
        $ret = trim($ret);

        return strlen($ret) > 0 ? $ret : $default;
    }

Usage Example

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->app = $this->getApplication();
     $dialog = new DialogHelper();
     $case = $input->getArgument('case');
     $note = $input->getArgument('note');
     // fb note "string message" and so we swap case and note
     if (!is_numeric($case)) {
         $note = $case;
         $case = $this->app->getCurrent();
         if (empty($case)) {
             $case = $dialog->ask($output, 'Enter a case number:');
         }
     }
     if (empty($note)) {
         $note = $dialog->ask($output, sprintf("Please supply a note for Case %d:\n", $case));
     }
     try {
         $this->app->fogbugz->edit(array('ixBug' => $case, 'sEvent' => $note));
         $output->writeln(sprintf('Left a note on case %s', $case), $this->app->outputFormat);
     } catch (ApiError $e) {
         $output->writeln(sprintf('<error>%s</error>', $e->getMessage()), $this->app->outputFormat);
         exit(1);
     }
 }
All Usage Examples Of Symfony\Component\Console\Helper\DialogHelper::ask