Horde_Cli::prompt PHP Method

prompt() public method

Prompts for a user response.
public prompt ( string $prompt, array $choices = null, string $default = null ) : mixed
$prompt string The message to display when prompting the user.
$choices array The choices available to the user or null for a text input.
$default string The default value if no value specified.
return mixed The user's response to the prompt.
    public function prompt($prompt, $choices = null, $default = null)
    {
        // Main event loop to capture top level command.
        while (true) {
            // Print out the prompt message.
            if (is_array($choices) && !empty($choices)) {
                $this->writeln(wordwrap($prompt) . ' ', !is_array($choices));
                foreach ($choices as $key => $choice) {
                    $this->writeln($this->indent('(' . $this->bold($key) . ') ' . $choice));
                }
                $question = Horde_Cli_Translation::t("Type your choice");
                if ($default !== null) {
                    $question .= ' [' . $default . ']';
                }
                $this->writeln($question . ': ', true);
                @ob_flush();
                // Get the user choice.
                $response = trim(fgets(STDIN));
                if ($response === '' && $default !== null) {
                    $response = $default;
                }
                if (isset($choices[$response])) {
                    return $response;
                } else {
                    $this->writeln($this->red(sprintf(Horde_Cli_Translation::t("\"%s\" is not a valid choice."), $response)));
                }
            } else {
                if ($default !== null) {
                    $prompt .= ' [' . $default . ']';
                }
                $this->writeln(wordwrap($prompt) . ' ', true);
                @ob_flush();
                $response = trim(fgets(STDIN));
                if ($response === '' && $default !== null) {
                    $response = $default;
                }
                return $response;
            }
        }
        return true;
    }

Usage Example

Example #1
0
$argv->addOption('-i', '--install-dir', array('action' => 'store', 'dest' => 'horde_dir', 'help' => 'Horde install directory'));
$argv->addOption('-l', '--log', array('action' => 'store', 'dest' => 'log', 'help' => 'Log filename'));
list($values, ) = $argv->parseArgs();
$version = phpversion();
if (version_compare($version, '5.3.0') === -1) {
    $c->fatal('You need at least PHP version 5.3.0 to run Horde.');
}
$c->writeln();
$c->message('PHP version: ' . $version, 'cli.message');
exec('which pear', $pear_output);
if (empty($pear_output)) {
    $c->fatal('Could not find PEAR script in your include path.');
}
$c->message('PEAR location: ' . $pear_output[0], 'cli.message');
while (!strlen($values->horde_dir)) {
    $values->horde_dir = $c->prompt('The directory to install Horde into:');
}
$hi = new HordeInstaller();
$logfile = isset($values->log) ? realpath($values->log) : dirname(Phar::running(false)) . '/horde-installer.log';
$c->writeln();
try {
    $hi->initLogger($logfile);
    $c->message('Log file: ' . $logfile, 'cli.message');
} catch (Exception $e) {
    $c->message(sprintf('Cannot write to log file "%s".', $logfile), 'cli.warning');
}
$c->writeln();
$c->message('Upgrading PEAR to the latest version...', 'cli.message');
$hi->pear('pear upgrade PEAR');
$c->message('Success.', 'cli.success');
$c->writeln();