Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Console.php #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Clio/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,37 @@ public static function confirm($text)
/**
* Gives the user an option to choose from. Giving '?' as an input will show
* a list of options to choose from and their explanations.
* If no input is given, the function does ask again.
*
* @param string $text Prompt string
* @param array $options Key-value array of options to choose from
* @param string $returntype select if function should return the key or the value from the options-array
*
* @return string An option character the user chose
*/
public static function select($text, $options = array())
public static function select($text, $options = array(), $returntype = 'key')
{
top:
static::stdout("$text [" . implode(',', array_keys($options)) . ",?]: ");
$input = static::stdin();
if (!$input) {goto top;}
if ($input === '?') {
foreach ($options as $key => $value) {
echo " $key - $value\n";
}
echo " ? - Show help\n";
goto top;
} elseif (!in_array($input, array_keys($options))) goto top;
return $input;

if ($returntype === 'key') {
return $input;
}
elseif ($returntype === 'value') {
$value = $options[$input];
return $value;
}


}

/**
Expand Down