/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { /** @var StorageInterface $storage */ $storage = $this->getContainer()->get('storage'); $files = $input->getArgument('files'); if ($input->getOption('latest')) { $remoteFiles = $storage->remoteListing(); if (count($remoteFiles) > 0) { $files[] = end($remoteFiles); $input->setArgument('files', $files); } } if ($input->hasArgument('files') && !empty($files)) { return; } $remoteFiles = $storage->remoteListing(); $localFiles = $storage->localListing(); if (count(array_diff($remoteFiles, $localFiles)) === 0) { $output->writeln('All files fetched'); return; } $helper = $this->getHelper('question'); $question = new ChoiceQuestion('Which backup', array_values(array_diff($remoteFiles, $localFiles))); $question->setMultiselect(true); $question->setErrorMessage('Backup %s is invalid.'); $question->setAutocompleterValues([]); $input->setArgument('files', $helper->ask($input, $output, $question)); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { if ($input->getArgument('file')) { return; } /** @var StorageInterface $storage */ $storage = $this->getContainer()->get('storage'); $localFiles = $storage->localListing(); $helper = $this->getHelper('question'); $question = new ChoiceQuestion('Which backup', $localFiles); $question->setErrorMessage('Backup %s is invalid.'); $question->setAutocompleterValues([]); $input->setArgument('file', $helper->ask($input, $output, $question)); $output->writeln(''); }
/** * @param array $items An associative array of choices. * @param string $text Some text to precede the choices. * @param InputInterface $input * @param OutputInterface $output * @param mixed $default A default (as a key in $items). * * @throws \RuntimeException on failure * * @return mixed * The chosen item (as a key in $items). */ public function choose(array $items, $text = 'Enter a number to choose an item:', InputInterface $input, OutputInterface $output, $default = null) { if (count($items) === 1) { return key($items); } $itemList = array_values($items); $defaultKey = $default !== null ? array_search($default, $itemList) : null; $question = new ChoiceQuestion($text, $itemList, $defaultKey); $question->setMaxAttempts(5); // Unfortunately the default autocompletion can cause '2' to be // completed to '20', etc. $question->setAutocompleterValues(null); $choice = $this->ask($input, $output, $question); $choiceKey = array_search($choice, $items); if ($choiceKey === false) { throw new \RuntimeException("Invalid value: {$choice}"); } return $choiceKey; }
/** * Ask questions * * @param Set $set A Certificationy questions Set instance * @param InputInterface $input A Symfony Console input instance * @param OutputInterface $output A Symfony Console output instance */ protected function askQuestions(Set $set, InputInterface $input, OutputInterface $output) { $questionHelper = $this->getHelper('question'); $showMultipleChoice = $input->getOption('show-multiple-choice'); $questionCount = 1; foreach ($set->getQuestions() as $i => $question) { $choiceQuestion = new ChoiceQuestion(sprintf('Question <comment>#%d</comment> [<info>%s</info>] %s' . ($showMultipleChoice === true ? "\n" . 'This question <comment>' . ($question->isMultipleChoice() === true ? 'IS' : 'IS NOT') . "</comment> multiple choice." : ""), $questionCount++, $question->getCategory(), $question->getQuestion()), $question->getAnswersLabels()); $multiSelect = $showMultipleChoice === true ? $question->isMultipleChoice() : true; $numericOnly = 1 === array_product(array_map('is_numeric', $question->getAnswersLabels())); $choiceQuestion->setMultiselect($multiSelect); $choiceQuestion->setErrorMessage('Answer %s is invalid.'); $choiceQuestion->setAutocompleterValues($numericOnly ? null : $question->getAnswersLabels()); $answer = $questionHelper->ask($input, $output, $choiceQuestion); $answers = true === $multiSelect ? $answer : array($answer); $answer = true === $multiSelect ? implode(', ', $answer) : $answer; $set->setAnswer($i, $answers); if ($input->getOption("training")) { $uniqueSet = new Set(array($i => $question)); $uniqueSet->setAnswer($i, $answers); $this->displayResults($uniqueSet, $output); } $output->writeln('<comment>✎ Your answer</comment>: ' . $answer . "\n"); } }
private function chooseIncludeExamples() { $default = 'i'; $choices = ['i' => 'Include examples', 'e' => 'Export to a separate file', 's' => 'Skip examples']; $question = new ChoiceQuestion("Do you want to include code examples (default: {$default})?", $choices, $default); $question->setMultiselect(false); $question->setAutocompleterValues(null); $question->setValidator(function ($val) { return $val; }); return $this->helper->ask($this->input, $this->output, $question); }
private function getTogglApi() { $tokens = $this->_config['toggl_token']; if (is_array($tokens)) { $question = new ChoiceQuestion('<question>For which team member do you want to invoice time entries?</question>', $tokens); $question->setAutocompleterValues(array_keys($tokens)); $question->setErrorMessage('Answer is invalid.'); $answer = $this->_questionHelper->ask($this->_input, $this->_output, $question); $token = $tokens[$answer]; } else { $token = $tokens; } return TogglClient::factory(array('api_key' => $token, 'debug' => self::DEBUG_MODE)); }