/** * Presents a user with a multiple choice question, useful for 'yes/no' type * questions (which this public static function defaults too). * * @param string $question The question to ask the user. * @param string $valid A string of characters allowed as a response. Case * is ignored. * @param string $default The default choice. NULL if a default is not allowed. * @return string The users choice. * @see cli\prompt() */ public static function choose($question, $choice = 'yn', $default = 'n') { if (!is_string($choice)) { $choice = join('', $choice); } // Make every choice character lowercase except the default $choice = str_ireplace($default, strtoupper($default), strtolower($choice)); // Seperate each choice with a forward-slash $choices = trim(join('/', preg_split('//', $choice)), '/'); while (true) { $line = \cli\Streams::prompt(sprintf('%s? [%s]', $question, $choices), $default, ''); if (stripos($choice, $line) !== false) { return strtolower($line); } if (!empty($default)) { return strtolower($default); } } }
/** * Displays an input prompt. If no default value is provided the prompt will * continue displaying until input is received. * * @param string $question The question to ask the user. * @param string $default A default value if the user provides no input. * @param string $marker A string to append to the question and default value * on display. * @return string The users input. * @see cli\input() */ function prompt($question, $default = false, $marker = ': ') { return \cli\Streams::prompt($question, $default, $marker); }