From f62a68f322175e45644f1cdcb8900a079df61ba3 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 6 Mar 2012 22:33:45 +0400 Subject: [PATCH] Added default value support for \yii\console\Command::prompt --- framework/console/Command.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/framework/console/Command.php b/framework/console/Command.php index 6f30231..a3c41bc 100644 --- a/framework/console/Command.php +++ b/framework/console/Command.php @@ -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. * * @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 */ - public function prompt($message) + public function prompt($message, $default = null) { + if($default !== null) { + $message .= " [$default] "; + } + else { + $message .= ' '; + } + if(extension_loaded('readline')) { - $input = readline($message.' '); - readline_add_history($input); - return $input; - } else - { - echo $message.' '; - return trim(fgets(STDIN)); + $input = readline($message); + if($input) { + readline_add_history($input); + } + } else { + echo $message; + $input = fgets(STDIN); + if($input === false) { + $input = trim($input); + } + } + if($input === false) { + return false; + } + else { + return empty($input) ? $default : $input; } }