|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00037 function cli_input($prompt, $default='', array $options=null, $casesensitiveoptions=false) { 00038 echo $prompt; 00039 echo "\n: "; 00040 $input = fread(STDIN, 2048); 00041 $input = trim($input); 00042 if ($input === '') { 00043 $input = $default; 00044 } 00045 if ($options) { 00046 if (!$casesensitiveoptions) { 00047 $input = strtolower($input); 00048 } 00049 if (!in_array($input, $options)) { 00050 echo "Incorrect value, please retry.\n"; // TODO: localize, mark as needed in install 00051 return cli_input($prompt, $default, $options, $casesensitiveoptions); 00052 } 00053 } 00054 return $input; 00055 } 00056 00063 function cli_get_params(array $longoptions, array $shortmapping=null) { 00064 $shortmapping = (array)$shortmapping; 00065 $options = array(); 00066 $unrecognized = array(); 00067 00068 if (empty($_SERVER['argv'])) { 00069 // bad luck, we can continue in interactive mode ;-) 00070 return array($options, $unrecognized); 00071 } 00072 $rawoptions = $_SERVER['argv']; 00073 00074 //remove anything after '--', options can not be there 00075 if (($key = array_search('--', $rawoptions)) !== false) { 00076 $rawoptions = array_slice($rawoptions, 0, $key); 00077 } 00078 00079 //remove script 00080 unset($rawoptions[0]); 00081 foreach ($rawoptions as $raw) { 00082 if (substr($raw, 0, 2) === '--') { 00083 $value = substr($raw, 2); 00084 $parts = explode('=', $value); 00085 if (count($parts) == 1) { 00086 $key = reset($parts); 00087 $value = true; 00088 } else { 00089 $key = array_shift($parts); 00090 $value = implode('=', $parts); 00091 } 00092 if (array_key_exists($key, $longoptions)) { 00093 $options[$key] = $value; 00094 } else { 00095 $unrecognized[] = $raw; 00096 } 00097 00098 } else if (substr($raw, 0, 1) === '-') { 00099 $value = substr($raw, 1); 00100 $parts = explode('=', $value); 00101 if (count($parts) == 1) { 00102 $key = reset($parts); 00103 $value = true; 00104 } else { 00105 $key = array_shift($parts); 00106 $value = implode('=', $parts); 00107 } 00108 if (array_key_exists($key, $shortmapping)) { 00109 $options[$shortmapping[$key]] = $value; 00110 } else { 00111 $unrecognized[] = $raw; 00112 } 00113 } else { 00114 $unrecognized[] = $raw; 00115 continue; 00116 } 00117 } 00118 //apply defaults 00119 foreach ($longoptions as $key=>$default) { 00120 if (!array_key_exists($key, $options)) { 00121 $options[$key] = $default; 00122 } 00123 } 00124 // finished 00125 return array($options, $unrecognized); 00126 } 00127 00133 function cli_separator($return=false) { 00134 $separator = str_repeat('-', 79)."\n"; 00135 if ($return) { 00136 return $separator; 00137 } else { 00138 echo $separator; 00139 } 00140 } 00141 00148 function cli_heading($string, $return=false) { 00149 $string = "== $string ==\n"; 00150 if ($return) { 00151 return $string; 00152 } else { 00153 echo $string; 00154 } 00155 } 00156 00162 function cli_problem($text) { 00163 fwrite(STDERR, $text."\n"); 00164 } 00165 00173 function cli_error($text, $errorcode=1) { 00174 fwrite(STDERR, $text); 00175 fwrite(STDERR, "\n"); 00176 die($errorcode); 00177 }