00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00060 class t3lib_cli {
00061
00062 var $cli_args = array();
00063 var $cli_options = array(
00064 array('-s','Silent operation, will only output errors and important messages.'),
00065 array('--silent','Same as -s'),
00066 array('-ss','Super silent, will not even output errors or important messages.'),
00067 );
00068 var $cli_help = array(
00069 'name' => 'CLI base class (overwrite this...)',
00070 'synopsis' => '###OPTIONS###',
00071 'description' => 'Class with basic functionality for CLI scripts (overwrite this...)',
00072 'examples' => 'Give examples...',
00073 'options' => '',
00074 'license' => 'GNU GPL - free software!',
00075 'author' => '[Author name]',
00076 );
00077 var $stdin = NULL;
00078
00079
00086 function t3lib_cli() {
00087
00088 $this->cli_args = $this->cli_getArgIndex();
00089 }
00090
00099 function cli_getArgArray($option,$argv) {
00100 while (count($argv) && strcmp($argv[0],$option)) {
00101 array_shift($argv);
00102 }
00103
00104 if (!strcmp($argv[0],$option)) {
00105 array_shift($argv);
00106 return count($argv) ? $argv : array('');
00107 }
00108 }
00109
00116 function cli_isArg($option) {
00117 return isset($this->cli_args[$option]);
00118 }
00119
00127 function cli_argValue($option,$idx=0) {
00128 return is_array($this->cli_args[$option]) ? $this->cli_args[$option][$idx] : '';
00129 }
00130
00138 function cli_getArgIndex() {
00139 $cli_options = array();
00140 $index = '_DEFAULT';
00141 foreach($_SERVER['argv'] as $token) {
00142 if ($token{0}==='-') {
00143 list($index,$opt) = explode('=',$token,2);
00144 if (isset($cli_options[$index])) {
00145 echo 'ERROR: Option '.$index.' was used twice!'.chr(10);
00146 exit;
00147 }
00148 $cli_options[$index] = array();
00149 if (isset($opt)) {
00150 $cli_options[$index][] = $opt;
00151 }
00152 } else {
00153 $cli_options[$index][] = $token;
00154 }
00155 }
00156 return $cli_options;
00157 }
00158
00162 function cli_validateArgs() {
00163 $cli_args_copy = $this->cli_args;
00164 unset($cli_args_copy['_DEFAULT']);
00165 $allOptions = array();
00166
00167 foreach($this->cli_options as $cfg) {
00168 $allOptions[] = $cfg[0];
00169 $argSplit = t3lib_div::trimExplode(' ',$cfg[0],1);
00170 if (isset($cli_args_copy[$argSplit[0]])) {
00171
00172 foreach($argSplit as $i => $v) {
00173 $ii=$i;
00174 if ($i>0) {
00175 if (!isset($cli_args_copy[$argSplit[0]][$i-1])) {
00176 echo 'ERROR: Option "'.$argSplit[0].'" requires a value ("'.$v.'") on position '.$i.chr(10);
00177 exit;
00178 }
00179 }
00180 }
00181
00182 $ii++;
00183 if (isset($cli_args_copy[$argSplit[0]][$ii-1])) {
00184 echo 'ERROR: Option "'.$argSplit[0].'" does not support a value on position '.$ii.chr(10);
00185 exit;
00186 }
00187
00188 unset($cli_args_copy[$argSplit[0]]);
00189 }
00190 }
00191
00192 if (count($cli_args_copy)) {
00193 echo wordwrap('ERROR: Option '.implode(',',array_keys($cli_args_copy)).' was unknown to this script!'.chr(10).'(Options are: '.implode(', ',$allOptions).')'.chr(10));
00194 exit;
00195 }
00196 }
00197
00203 function cli_keyboardInput() {
00204
00205
00206 if (!$this->stdin) {
00207 $this->stdin = fopen('php:
00208 }
00209
00210 while (FALSE == ($line = fgets($this->stdin,1000))) {}
00211
00212 return trim($line);
00213 }
00214
00221 function cli_keyboardInput_yes($msg='') {
00222 echo $msg.' (Yes/No + return): ';
00223 return t3lib_div::inList('y,yes',strtolower($this->cli_keyboardInput()));
00224 }
00225
00233 function cli_echo($string='',$force=FALSE) {
00234 if (isset($this->cli_args['-ss'])) {
00235
00236 } elseif (isset($this->cli_args['-s']) || isset($this->cli_args['--silent'])) {
00237 if ($force) { echo $string; return TRUE; }
00238 } else { echo $string; return TRUE; }
00239
00240 return FALSE;
00241 }
00242
00248 function cli_help() {
00249 foreach($this->cli_help as $key => $value) {
00250 $this->cli_echo(strtoupper($key).":\n");
00251 switch($key) {
00252 case 'synopsis':
00253 $optStr = '';
00254 foreach ($this->cli_options as $v) {
00255 $optStr.=' ['.$v[0].']';
00256 }
00257 $this->cli_echo($this->cli_indent(str_replace('###OPTIONS###',trim($optStr),$value),4)."\n\n");
00258 break;
00259 case 'options':
00260 $this->cli_echo($this->cli_indent($value,4)."\n");
00261
00262 $maxLen = 0;
00263 foreach ($this->cli_options as $v) {
00264 if (strlen($v[0])>$maxLen) $maxLen=strlen($v[0]);
00265 }
00266
00267 foreach ($this->cli_options as $v) {
00268 $this->cli_echo($v[0].substr($this->cli_indent(rtrim($v[1].chr(10).$v[2]),$maxLen+4),strlen($v[0]))."\n");
00269 }
00270 $this->cli_echo("\n");
00271 break;
00272 default:
00273 $this->cli_echo($this->cli_indent($value,4)."\n\n");
00274 break;
00275 }
00276 }
00277 }
00278
00286 function cli_indent($str,$indent) {
00287 $lines = explode(chr(10),wordwrap($str,75-$indent));
00288 $indentStr = str_pad('',$indent,' ');
00289 foreach($lines as $k => $v) {
00290 $lines[$k] = $indentStr.$lines[$k];
00291 }
00292 return implode(chr(10),$lines);
00293 }
00294 }
00295
00296 ?>