diff --git a/lib/clilib.php b/lib/clilib.php index 962a700f16e..ba243f1029e 100644 --- a/lib/clilib.php +++ b/lib/clilib.php @@ -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); } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 80c1da36bab..5d25e3f6da9 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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: _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 ===