public function choose(array $items, $text = 'Enter a number to choose an item:', $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($this->input, $this->output, $question);
$choiceKey = array_search($choice, $items);
if ($choiceKey === false) {
throw new \RuntimeException("Invalid value: {$choice}");
}
return $choiceKey;
}