Merge branch 'MDL-51423-master-cliout' of git://github.com/mudrd8mz/moodle

This commit is contained in:
Andrew Nicols 2015-09-23 09:42:29 +08:00
commit b8ceed62d2
2 changed files with 33 additions and 12 deletions

View File

@ -26,6 +26,26 @@
// NOTE: no MOODLE_INTERNAL test here, sometimes we use this before requiring Moodle libs!
/**
* Write a text to the given stream
*
* @param string $text text to be written
* @param resource $stream output stream to be written to, defaults to STDOUT
*/
function cli_write($text, $stream=STDOUT) {
fwrite($stream, $text);
}
/**
* Write a text followed by an end of line symbol to the given stream
*
* @param string $text text to be written
* @param resource $stream output stream to be written to, defaults to STDOUT
*/
function cli_writeln($text, $stream=STDOUT) {
cli_write($text.PHP_EOL, $stream);
}
/**
* Get input from user
* @param string $prompt text prompt, should include possible options
@ -35,8 +55,8 @@
* @return string entered text
*/
function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) {
echo $prompt;
echo "\n: ";
cli_writeln($prompt);
cli_write(': ');
$input = fread(STDIN, 2048);
$input = trim($input);
if ($input === '') {
@ -47,7 +67,7 @@ function cli_input($prompt, $default='', array $options=null, $casesensitiveopti
$input = strtolower($input);
}
if (!in_array($input, $options)) {
echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install
cli_writeln(get_string('cliincorrectvalueretry', 'admin'));
return cli_input($prompt, $default, $options, $casesensitiveoptions);
}
}
@ -131,11 +151,11 @@ function cli_get_params(array $longoptions, array $shortmapping=null) {
* @return mixed void or string
*/
function cli_separator($return=false) {
$separator = str_repeat('-', 79)."\n";
$separator = str_repeat('-', 79).PHP_EOL;
if ($return) {
return $separator;
} else {
echo $separator;
cli_write($separator);
}
}
@ -146,11 +166,11 @@ function cli_separator($return=false) {
* @return mixed void or string
*/
function cli_heading($string, $return=false) {
$string = "== $string ==\n";
$string = "== $string ==".PHP_EOL;
if ($return) {
return $string;
} else {
echo $string;
cli_write($string);
}
}
@ -160,19 +180,18 @@ function cli_heading($string, $return=false) {
* @return void
*/
function cli_problem($text) {
fwrite(STDERR, $text."\n");
cli_writeln($text, STDERR);
}
/**
* Write to standard out and error with exit in error.
* Write to standard error output and exit with the given code
*
* @param string $text
* @param int $errorcode
* @return void (does not return)
*/
function cli_error($text, $errorcode=1) {
fwrite(STDERR, $text);
fwrite(STDERR, "\n");
cli_writeln($text.PHP_EOL, STDERR);
die($errorcode);
}
@ -205,6 +224,6 @@ function cli_logo($padding=2, $return=false) {
if ($return) {
return $logo;
} else {
echo $logo;
cli_write($logo);
}
}

View File

@ -141,6 +141,8 @@ information provided here is intended especially for developers.
unless other status is specified.
* Plugins can extend the navigation for categories settings by declaring the following callback:
<frankenstyle>_extend_navigation_category_settings(navigation_node, context_coursecat)
* The clilib.php provides two new functions cli_write() and cli_writeln() that should be used for outputting texts from the command
line interface scripts.
=== 2.9.1 ===