Commit | Line | Data |
---|---|---|
5d995668 | 1 | <?php |
3b596dbf | 2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5d995668 | 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. | |
9 | // | |
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. | |
3b596dbf | 14 | // |
5d995668 | 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/>. | |
17 | ||
9d068cd6 | 18 | /** |
3b596dbf | 19 | * Command line utility functions and classes |
9d068cd6 | 20 | * |
78bfb562 | 21 | * @package core |
3b596dbf | 22 | * @subpackage cli |
23 | * @copyright 2009 Petr Skoda (http://skodak.org) | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
9d068cd6 | 25 | */ |
9d068cd6 | 26 | |
78bfb562 PS |
27 | defined('MOODLE_INTERNAL') || die(); |
28 | ||
9d068cd6 | 29 | /** |
3b596dbf | 30 | * Get input from user |
1275723f | 31 | * @param string $prompt text prompt, should include possible options |
3b596dbf | 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 | |
9d068cd6 | 36 | */ |
3b596dbf | 37 | function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) { |
38 | echo $prompt; | |
39 | echo "\n: "; | |
40 | $input = fread(STDIN, 2048); | |
41 | $input = trim($input); | |
42 | if ($input === '') { | |
43 | $input = $default; | |
9d068cd6 | 44 | } |
3b596dbf | 45 | if ($options) { |
46 | if (!$casesensitiveoptions) { | |
47 | $input = strtolower($input); | |
9d068cd6 | 48 | } |
3b596dbf | 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); | |
9d068cd6 | 52 | } |
53 | } | |
3b596dbf | 54 | return $input; |
9d068cd6 | 55 | } |
56 | ||
57 | /** | |
3b596dbf | 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 | |
9d068cd6 | 62 | */ |
3b596dbf | 63 | function cli_get_params(array $longoptions, array $shortmapping=null) { |
64 | $shortmapping = (array)$shortmapping; | |
65 | $options = array(); | |
66 | $unrecognized = array(); | |
9d068cd6 | 67 | |
3b596dbf | 68 | if (empty($_SERVER['argv'])) { |
69 | // bad luck, we can continue in interactive mode ;-) | |
70 | return array($options, $unrecognized); | |
9d068cd6 | 71 | } |
3b596dbf | 72 | $rawoptions = $_SERVER['argv']; |
9d068cd6 | 73 | |
3b596dbf | 74 | //remove anything after '--', options can not be there |
75 | if (($key = array_search('--', $rawoptions)) !== false) { | |
76 | $rawoptions = array_slice($rawoptions, 0, $key); | |
9d068cd6 | 77 | } |
9d068cd6 | 78 | |
3b596dbf | 79 | //remove script |
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) { | |
86 | $key = reset($parts); | |
87 | $value = true; | |
88 | } else { | |
89 | $key = array_shift($parts); | |
90 | $value = implode('=', $parts); | |
9d068cd6 | 91 | } |
3b596dbf | 92 | if (array_key_exists($key, $longoptions)) { |
93 | $options[$key] = $value; | |
94 | } else { | |
95 | $unrecognized[] = $raw; | |
9d068cd6 | 96 | } |
9d068cd6 | 97 | |
3b596dbf | 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); | |
103 | $value = true; | |
104 | } else { | |
105 | $key = array_shift($parts); | |
106 | $value = implode('=', $parts); | |
107 | } | |
108 | if (array_key_exists($key, $shortmapping)) { | |
109 | $options[$shortmapping[$key]] = $value; | |
110 | } else { | |
111 | $unrecognized[] = $raw; | |
9d068cd6 | 112 | } |
113 | } else { | |
3b596dbf | 114 | $unrecognized[] = $raw; |
115 | continue; | |
9d068cd6 | 116 | } |
117 | } | |
3b596dbf | 118 | //apply defaults |
119 | foreach ($longoptions as $key=>$default) { | |
120 | if (!array_key_exists($key, $options)) { | |
121 | $options[$key] = $default; | |
9d068cd6 | 122 | } |
9d068cd6 | 123 | } |
3b596dbf | 124 | // finished |
125 | return array($options, $unrecognized); | |
9d068cd6 | 126 | } |
127 | ||
c31d94e8 | 128 | /** |
129 | * Print or return section separator string | |
130 | * @param bool $return false means print, true return as string | |
131 | * @return mixed void or string | |
132 | */ | |
133 | function cli_separator($return=false) { | |
134 | $separator = str_repeat('-', 79)."\n"; | |
135 | if ($return) { | |
136 | return $separator; | |
137 | } else { | |
138 | echo $separator; | |
139 | } | |
140 | } | |
141 | ||
142 | /** | |
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 | |
147 | */ | |
148 | function cli_heading($string, $return=false) { | |
149 | $string = "== $string ==\n"; | |
150 | if ($return) { | |
151 | return $string; | |
152 | } else { | |
153 | echo $string; | |
154 | } | |
155 | } | |
156 | ||
9d068cd6 | 157 | /** |
3b596dbf | 158 | * Write error notification |
159 | * @param $text | |
160 | * @return void | |
9d068cd6 | 161 | */ |
3b596dbf | 162 | function cli_problem($text) { |
3df957d2 | 163 | fwrite(STDERR, $text."\n"); |
9d068cd6 | 164 | } |
3b596dbf | 165 | |
9d068cd6 | 166 | /** |
3b596dbf | 167 | * Write to standard out and error with exit in error. |
9d068cd6 | 168 | * |
3b596dbf | 169 | * @param string $text |
170 | * @param int $errorcode | |
171 | * @return void (does not return) | |
9d068cd6 | 172 | */ |
3b596dbf | 173 | function cli_error($text, $errorcode=1) { |
174 | fwrite(STDERR, $text); | |
175 | fwrite(STDERR, "\n"); | |
176 | die($errorcode); | |
9d068cd6 | 177 | } |