mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Added default value support for \yii\console\Command::prompt
This commit is contained in:
@ -304,19 +304,36 @@ abstract class Command extends \yii\base\Component
|
|||||||
* Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
|
* Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
|
||||||
*
|
*
|
||||||
* @param string $message to echo out before waiting for user input
|
* @param string $message to echo out before waiting for user input
|
||||||
|
* @param string $default the default string to be returned when user does not write anything. Defaults to null.
|
||||||
* @return mixed line read as a string, or false if input has been closed
|
* @return mixed line read as a string, or false if input has been closed
|
||||||
*/
|
*/
|
||||||
public function prompt($message)
|
public function prompt($message, $default = null)
|
||||||
{
|
{
|
||||||
|
if($default !== null) {
|
||||||
|
$message .= " [$default] ";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$message .= ' ';
|
||||||
|
}
|
||||||
|
|
||||||
if(extension_loaded('readline'))
|
if(extension_loaded('readline'))
|
||||||
{
|
{
|
||||||
$input = readline($message.' ');
|
$input = readline($message);
|
||||||
|
if($input) {
|
||||||
readline_add_history($input);
|
readline_add_history($input);
|
||||||
return $input;
|
}
|
||||||
} else
|
} else {
|
||||||
{
|
echo $message;
|
||||||
echo $message.' ';
|
$input = fgets(STDIN);
|
||||||
return trim(fgets(STDIN));
|
if($input === false) {
|
||||||
|
$input = trim($input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($input === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return empty($input) ? $default : $input;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user