3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Command line utility functions and classes
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 // NOTE: no MOODLE_INTERNAL test here, sometimes we use this before requiring Moodle libs!
31 * @param string $prompt text prompt, should include possible options
32 * @param string $default default value when enter pressed
33 * @param array $options list of allowed options, empty means any text
34 * @param bool $casesensitive true if options are case sensitive
35 * @return string entered text
37 function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) {
40 $input = fread(STDIN
, 2048);
41 $input = trim($input);
46 if (!$casesensitiveoptions) {
47 $input = strtolower($input);
49 if (!in_array($input, $options)) {
50 echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install
51 return cli_input($prompt, $default, $options, $casesensitiveoptions);
58 * Returns cli script parameters.
59 * @param array $longoptions array of --style options ex:('verbose'=>false)
60 * @param array $shortmapping array describing mapping of short to long style options ex:('h'=>'help', 'v'=>'verbose')
61 * @return array array of arrays, options, unrecognised as optionlongname=>value
63 function cli_get_params(array $longoptions, array $shortmapping=null) {
64 $shortmapping = (array)$shortmapping;
66 $unrecognized = array();
68 if (empty($_SERVER['argv'])) {
69 // bad luck, we can continue in interactive mode ;-)
70 return array($options, $unrecognized);
72 $rawoptions = $_SERVER['argv'];
74 //remove anything after '--', options can not be there
75 if (($key = array_search('--', $rawoptions)) !== false) {
76 $rawoptions = array_slice($rawoptions, 0, $key);
80 unset($rawoptions[0]);
81 foreach ($rawoptions as $raw) {
82 if (substr($raw, 0, 2) === '--') {
83 $value = substr($raw, 2);
84 $parts = explode('=', $value);
85 if (count($parts) == 1) {
89 $key = array_shift($parts);
90 $value = implode('=', $parts);
92 if (array_key_exists($key, $longoptions)) {
93 $options[$key] = $value;
95 $unrecognized[] = $raw;
98 } else if (substr($raw, 0, 1) === '-') {
99 $value = substr($raw, 1);
100 $parts = explode('=', $value);
101 if (count($parts) == 1) {
102 $key = reset($parts);
105 $key = array_shift($parts);
106 $value = implode('=', $parts);
108 if (array_key_exists($key, $shortmapping)) {
109 $options[$shortmapping[$key]] = $value;
111 $unrecognized[] = $raw;
114 $unrecognized[] = $raw;
119 foreach ($longoptions as $key=>$default) {
120 if (!array_key_exists($key, $options)) {
121 $options[$key] = $default;
125 return array($options, $unrecognized);
129 * Print or return section separator string
130 * @param bool $return false means print, true return as string
131 * @return mixed void or string
133 function cli_separator($return=false) {
134 $separator = str_repeat('-', 79)."\n";
143 * Print or return section heading string
144 * @param string $string text
145 * @param bool $return false means print, true return as string
146 * @return mixed void or string
148 function cli_heading($string, $return=false) {
149 $string = "== $string ==\n";
158 * Write error notification
162 function cli_problem($text) {
163 fwrite(STDERR
, $text."\n");
167 * Write to standard out and error with exit in error.
169 * @param string $text
170 * @param int $errorcode
171 * @return void (does not return)
173 function cli_error($text, $errorcode=1) {
174 fwrite(STDERR
, $text);
175 fwrite(STDERR
, "\n");
180 * Print an ASCII version of the Moodle logo.
182 * @param int $padding left padding of the logo
183 * @param bool $return should we print directly (false) or return the string (true)
184 * @return mixed void or string
186 function cli_logo($padding=2, $return=false) {
191 '/____/-.---_ .---. .---. .-.| || | .---. ',
192 '| | _ _ |/ _ \\/ _ \\/ _ || |/ __ \\',
193 '* | | | | | || |_| || |_| || |_| || || |___/',
194 ' |_| |_| |_|\\_____/\\_____/\\_____||_|\\_____)',
199 foreach ($lines as $line) {
200 $logo .= str_repeat(' ', $padding);