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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 include_once( 'lib/ezutils/classes/ezini.php' );
00054 include_once( 'lib/ezutils/classes/ezdebug.php' );
00055 include_once( 'lib/ezutils/classes/ezdebugsetting.php' );
00056
00057 define( 'EZ_CLI_TERMINAL_ENDOFLINE_STRING', "\n" );
00058
00059 class eZCLI
00060 {
00061
00062
00063
00064 function eZCLI()
00065 {
00066 $endl = "<br/>";
00067 $webOutput = true;
00068 if ( isset( $_SERVER['argv'] ) )
00069 {
00070 $endl = EZ_CLI_TERMINAL_ENDOFLINE_STRING;
00071 $webOutput = false;
00072 }
00073 $this->EndlineString = $endl;
00074 $this->WebOutput = $webOutput;
00075 $this->TerminalStyles = array( 'warning' => "\033[1;35m",
00076 'warning-end' => "\033[0;39m",
00077 'error' => "\033[1;31m",
00078 'error-end' => "\033[0;39m",
00079 'failure' => "\033[1;31m",
00080 'failure-end' => "\033[0;39m",
00081 'notice' => "\033[0;32m",
00082 'notice-end' => "\033[0;39m",
00083 'debug' => "\033[0;30m",
00084 'debug-end' => "\033[0;39m",
00085 'timing' => "\033[1;34m",
00086 'timing-end' => "\033[0;39m",
00087 'success' => "\033[1;32m",
00088 'success-end' => "\033[0;39m",
00089 'file' => "\033[1;38m",
00090 'file-end' => "\033[0;39m",
00091 'dir' => "\033[1;34m",
00092 'dir-end' => "\033[0;39m",
00093 'link' => "\033[0;36m",
00094 'link-end' => "\033[0;39m",
00095 'exe' => "\033[1;32m",
00096 'exe-end' => "\033[0;39m",
00097 'archive' => "\033[1;31m",
00098 'archive-end' => "\033[0;39m",
00099 'image' => "\033[1;35m",
00100 'image-end' => "\033[0;39m",
00101
00102 'red' => "\033[1;31m",
00103 'red-end' => "\033[0;39m",
00104 'green' => "\033[1;32m",
00105 'green-end' => "\033[0;39m",
00106 'yellow' => "\033[1;33m",
00107 'yellow-end' => "\033[0;39m",
00108 'blue' => "\033[1;34m",
00109 'blue-end' => "\033[0;39m",
00110 'magenta' => "\033[1;35m",
00111 'magenta-end' => "\033[0;39m",
00112 'cyan' => "\033[1;36m",
00113 'cyan-end' => "\033[0;39m",
00114 'white' => "\033[1;37m",
00115 'white-end' => "\033[0;39m",
00116 'gray' => "\033[1;30m",
00117 'gray-end' => "\033[0;39m",
00118
00119 'dark-red' => "\033[0;31m",
00120 'dark-red-end' => "\033[0;39m",
00121 'dark-green' => "\033[0;32m",
00122 'dark-green-end' => "\033[0;39m",
00123 'dark-yellow' => "\033[0;33m",
00124 'dark-yellow-end' => "\033[0;39m",
00125 'dark-blue' => "\033[0;34m",
00126 'dark-blue-end' => "\033[0;39m",
00127 'dark-magenta' => "\033[0;35m",
00128 'dark-magenta-end' => "\033[0;39m",
00129 'dark-cyan' => "\033[0;36m",
00130 'dark-cyan-end' => "\033[0;39m",
00131 'dark-white' => "\033[0;37m",
00132 'dark-white-end' => "\033[0;39m",
00133 'dark-gray' => "\033[0;30m",
00134 'dark-gray-end' => "\033[0;39m",
00135
00136 'red-bg' => "\033[1;41m",
00137 'red-bg-end' => "\033[0;39m",
00138 'green-bg' => "\033[1;42m",
00139 'green-bg-end' => "\033[0;39m",
00140 'yellow-bg' => "\033[1;43m",
00141 'yellow-bg-end' => "\033[0;39m",
00142 'blue-bg' => "\033[1;44m",
00143 'blue-bg-end' => "\033[0;39m",
00144 'magenta-bg' => "\033[1;45m",
00145 'magenta-bg-end' => "\033[0;39m",
00146 'cyan-bg' => "\033[1;46m",
00147 'cyan-bg-end' => "\033[0;39m",
00148 'white-bg' => "\033[1;47m",
00149 'white-bg-end' => "\033[0;39m",
00150
00151 'text' => "\033[0;39m",
00152 'text-end' => "\033[0;39m",
00153 'variable' => "\033[1;34m",
00154 'variable-end' => "\033[0;39m",
00155 'symbol' => "\033[0;37m",
00156 'symbol-end' => "\033[0;39m",
00157 'emphasize' => "\033[1;38m",
00158 'emphasize-end' => "\033[0;39m",
00159 'header' => "\033[1;38m",
00160 'header-end' => "\033[0;39m",
00161 'strong' => "\033[1;39m",
00162 'strong-end' => "\033[0;39m",
00163 'mark' => "\033[1;30m",
00164 'mark-end' => "\033[0;39m",
00165 'bold' => "\033[1;38m",
00166 'bold-end' => "\033[0;39m",
00167 'italic' => "\033[0;39m",
00168 'italic-end' => "\033[0;39m",
00169 'underline' => "\033[0;39m",
00170 'underline-end' => "\033[0;39m",
00171 'paragraph' => "\033[0;39m",
00172 'paragraph-end' => "\033[0;39m",
00173 'normal' => "\033[0;39m",
00174 'normal-end' => "\033[0;39m",
00175
00176 );
00177
00178 $this->WebStyles = array( 'warning' => "<font color=\"orange\">",
00179 'warning-end' => "</font>",
00180 'error' => "<font color=\"red\">",
00181 'error-end' => "</font>",
00182 'failure' => "<font color=\"red\">",
00183 'failure-end' => "</font>",
00184 'notice' => "<font color=\"green\">",
00185 'notice-end' => "</font>",
00186 'debug' => "<font color=\"brown\">",
00187 'debug-end' => "</font>",
00188 'timing' => "<font color=\"blue\">",
00189 'timing-end' => "</font>",
00190 'success' => "<font color=\"green\">",
00191 'success-end' => "</font>",
00192 'file' => "<i>",
00193 'file-end' => "</i>",
00194 'dir' => "<font color=\"blue\">",
00195 'dir-end' => "</font>",
00196 'link' => "<font color=\"cyan\">",
00197 'link-end' => "</font>",
00198 'symbol' => "<i>",
00199 'symbol-end' => "</i>",
00200 'emphasize' => "<i>",
00201 'emphasize-end' => "</i>",
00202 'header' => "<h1>",
00203 'header-end' => "</h1>",
00204 'strong' => "<b>",
00205 'strong-end' => "</b>",
00206 'mark' => "",
00207 'mark-end' => "",
00208 'bold' => "<b>",
00209 'bold-end' => "</b>",
00210 'italic' => "<i>",
00211 'italic-end' => "</i>",
00212 'underline' => "<u>",
00213 'underline-end' => "</u>",
00214 'paragraph' => "<p>",
00215 'paragraph-end' => "</p>",
00216 'normal' => "",
00217 'normal-end' => "" );
00218
00219 $this->EmptyStyles = array();
00220 foreach ( $this->TerminalStyles as $styleName => $styleValue )
00221 {
00222 $this->EmptyStyles[$styleName] = false;
00223 }
00224 $this->UseStyles = false;
00225 $this->IsQuiet = false;
00226 }
00227
00228
00229
00230
00231
00232 function endlineString()
00233 {
00234 return $this->EndlineString;
00235 }
00236
00237
00238
00239
00240 function isWebOutput()
00241 {
00242 return $this->WebOutput;
00243 }
00244
00245
00246
00247
00248 function terminalStyle( $name )
00249 {
00250 if ( isset( $this->TerminalStyles[$name] ) )
00251 {
00252 return $this->TerminalStyles[$name];
00253 }
00254 return false;
00255 }
00256
00257
00258
00259
00260 function webStyle( $name )
00261 {
00262 return $this->WebStyles[$name];
00263 }
00264
00265
00266
00267
00268 function terminalStyles()
00269 {
00270 return $this->TerminalStyles;
00271 }
00272
00273
00274
00275
00276 function webStyles()
00277 {
00278 return $this->WebStyles;
00279 }
00280
00281
00282
00283
00284 function emptyStyles()
00285 {
00286 return $this->EmptyStyles;
00287 }
00288
00289
00290
00291
00292 function style( $name )
00293 {
00294 if ( $this->UseStyles )
00295 {
00296 if ( $this->isWebOutput() )
00297 return $this->webStyle( $name );
00298 else
00299 return $this->terminalStyle( $name );
00300 }
00301 return false;
00302 }
00303
00304
00305
00306
00307 function stylize( $styleName, $text )
00308 {
00309 $preStyle = $this->style( $styleName );
00310 $postStyle = $this->style( $styleName . '-end' );
00311 return $preStyle . $text . $postStyle;
00312 }
00313
00314
00315
00316
00317
00318 function storePosition()
00319 {
00320 return "\033[s";
00321 }
00322
00323
00324
00325
00326
00327 function restorePosition()
00328 {
00329 return "\033[u";
00330 }
00331
00332
00333
00334
00335
00336 function gotoColumn( $column )
00337 {
00338 if ( !$this->UseStyles )
00339 {
00340 return "\t\t";
00341 }
00342 else
00343 {
00344 return "\033[" . $column . "G";
00345 }
00346 }
00347
00348
00349
00350
00351
00352
00353 function setUseStyles( $useStyles )
00354 {
00355 if ( getenv('TERM') != '' )
00356 {
00357 $this->UseStyles = $useStyles;
00358 }
00359 }
00360
00361
00362
00363
00364 function useStyles()
00365 {
00366 return $this->UseStyles;
00367 }
00368
00369
00370
00371
00372
00373 function output( $string = false, $addEOL = true )
00374 {
00375 if ( $this->isQuiet() )
00376 return;
00377 print( $string );
00378 if ( $addEOL )
00379 print( $this->endlineString() );
00380 }
00381
00382
00383
00384
00385
00386 function notice( $string = false, $addEOL = true )
00387 {
00388 if ( $this->isQuiet() )
00389 return;
00390 fputs( STDERR, $string );
00391 if ( $addEOL )
00392 fputs( STDERR, $this->endlineString() );
00393 }
00394
00395
00396
00397
00398
00399 function warning( $string = false, $addEOL = true )
00400 {
00401 if ( $this->isQuiet() )
00402 return;
00403 $string = $this->stylize( 'warning', $string );
00404 fputs( STDERR, $string );
00405 if ( $addEOL )
00406 fputs( STDERR, $this->endlineString() );
00407 }
00408
00409
00410
00411
00412
00413 function error( $string = false, $addEOL = true )
00414 {
00415 if ( $this->isQuiet() )
00416 return;
00417 $string = $this->stylize( 'error', $string );
00418 fputs( STDERR, $string );
00419 if ( $addEOL )
00420 fputs( STDERR, $this->endlineString() );
00421 }
00422
00423
00424
00425
00426
00427 function setIsQuiet( $isQuiet )
00428 {
00429 $this->IsQuiet = $isQuiet;
00430 }
00431
00432
00433
00434
00435
00436 function isQuiet()
00437 {
00438 return $this->IsQuiet;
00439 }
00440
00441
00442
00443
00444
00445 function isLoud()
00446 {
00447 return !$this->IsQuiet;
00448 }
00449
00450 function parseOptionString( $configString, &$optionConfig )
00451 {
00452 $len = strlen( $configString );
00453 $i = 0;
00454 if ( !is_array( $optionConfig ) )
00455 {
00456 $optionConfig = array( 'list' => array(),
00457 'short' => array(),
00458 'long' => array() );
00459 }
00460 while ( $i < $len )
00461 {
00462 $option = $configString[$i];
00463 if ( $option == '[' )
00464 {
00465 $end = strpos( $configString, ']', $i + 1 );
00466 if ( $end === false )
00467 {
00468 eZDebug::writeError( "Missing end marker ] in option string at position $i",
00469 'eZCLI::parseOptionString' );
00470 return $optionConfig;
00471 }
00472 $optionList = substr( $configString, $i + 1, $end - $i - 1 );
00473 $i += 1 + ( $end - $i );
00474 $startMarkerPos = strpos( $optionList, '[' );
00475 if ( $startMarkerPos !== false )
00476 {
00477 eZDebug::writeError( "Start marker [ found in option string at position, it should not be present. Skipping current option" . ( $i + 1 + $startMarkerPos ),
00478 'eZCLI::parseOptionString' );
00479 continue;
00480 }
00481 $optionList = explode( '|', $optionList );
00482 }
00483 else
00484 {
00485 $text = $option;
00486 ++$i;
00487 if ( $i < $len and
00488 in_array( $configString[$i], array( ':', ';' ) ) )
00489 {
00490 $text .= $configString[$i];
00491 ++$i;
00492 }
00493 if ( $i < $len and
00494 in_array( $configString[$i], array( '?', '*', '+' ) ) )
00495 {
00496 $text .= $configString[$i];
00497 ++$i;
00498 }
00499 $optionList = array( $text );
00500 }
00501 $optionStoreName = false;
00502 unset( $optionConfigList );
00503 $optionConfigList = array();
00504 foreach ( $optionList as $optionItem )
00505 {
00506 $optionLen = strlen( $optionItem );
00507 $hasValue = false;
00508 $optionName = $optionItem;
00509 $quantifierText = false;
00510 $quantifier = array( 'min' => 0,
00511 'max' => 0 );
00512 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( '?', '*', '+' ) ) )
00513 {
00514 $quantifierText = $optionName[$optionLen - 1];
00515 $optionName = substr( $optionName, 0, $optionLen - 1 );
00516 --$optionLen;
00517 if ( $quantifierText == '?' )
00518 $quantifier = array( 'min' => 0,
00519 'max' => 1 );
00520 else if ( $quantifierText == '*' )
00521 $quantifier = array( 'min' => 0,
00522 'max' => false );
00523 else if ( $quantifierText == '+' )
00524 $quantifier = array( 'min' => 1,
00525 'max' => false );
00526 }
00527 if ( $optionLen > 0 and in_array( $optionName[$optionLen - 1], array( ':', ';' ) ) )
00528 {
00529 $valueText = $optionName[$optionLen - 1];
00530 $optionName = substr( $optionName, 0, $optionLen - 1 );
00531 --$optionLen;
00532 if ( $valueText == ':' )
00533 $hasValue = true;
00534 else if ( $valueText == ';' )
00535 $hasValue = 'optional';
00536 }
00537 $optionLen = strlen( $optionName );
00538 if ( $optionLen == 0 )
00539 continue;
00540 $optionStoreName = $optionName;
00541 $optionConfigItem = array( 'name' => $optionName,
00542 'has-value' => $hasValue,
00543 'quantifier' => $quantifier,
00544 'store-name' => false,
00545 'is-long-option' => strlen( $optionName ) > 1 );
00546 $optionConfigList[] = $optionConfigItem;
00547 }
00548 foreach ( array_keys( $optionConfigList ) as $optionConfigItemKey )
00549 {
00550 $optionConfigItem =& $optionConfigList[$optionConfigItemKey];
00551 $optionName = $optionConfigItem['name'];
00552 $optionConfigItem['store-name'] = $optionStoreName;
00553 $optionConfig['list'][] = $optionConfigItem;
00554 if ( $optionConfigItem['is-long-option'] )
00555 $optionConfig['long'][$optionName] = $optionConfigItem;
00556 else
00557 $optionConfig['short'][$optionName] = $optionConfigItem;
00558 }
00559 }
00560 return $optionConfig;
00561 }
00562
00563 function getOptions( $config, $argumentConfig, $arguments = false )
00564 {
00565 $program = false;
00566 if ( $arguments === false )
00567 {
00568 $arguments = $GLOBALS['argv'];
00569 $program = $arguments[0];
00570 array_shift( $arguments );
00571 }
00572
00573 if ( isset( $this ) and get_class( $this ) == 'ezcli' )
00574 $cli =& $this;
00575 else
00576 $cli =& eZCLI::instance();
00577
00578 if ( is_string( $config ) )
00579 $config = eZCLI::parseOptionString( $config, $optionConfig );
00580 if ( is_string( $argumentConfig ) )
00581 $argumentConfig = eZCLI::parseOptionString( $argumentConfig, $tmpArgumentConfig );
00582
00583 $options = array();
00584
00585 $helpOption = false;
00586 $helpText = false;
00587 if ( isset( $config['short']['h'] ) )
00588 $helpOption = '-h';
00589 else if ( isset( $config['short']['help'] ) )
00590 $helpOption = '--help';
00591 if ( $helpOption )
00592 $helpText = "\n" . "Try `$program $helpOption' for more information.";
00593
00594 $options['arguments'] = array();
00595
00596 $arguments = array_values( $arguments );
00597 $argumentCount = count( $arguments );
00598 for ( $i = 0; $i < $argumentCount; ++$i )
00599 {
00600 $argument = $arguments[$i];
00601 $argumentLen = strlen( $argument );
00602 if ( $argumentLen > 1 and
00603 $argument[0] == '-' )
00604 {
00605 $argumentValue = false;
00606 if ( $argumentLen > 2 and
00607 $argument[1] == '-' )
00608 {
00609 $optionName = substr( $argument, 2 );
00610 $assignPosition = strpos( $optionName, '=' );
00611 if ( $assignPosition !== false )
00612 {
00613 $argumentValue = substr( $optionName, $assignPosition + 1 );
00614 $optionName = substr( $optionName, 0, $assignPosition );
00615 }
00616 $optionType = 'long';
00617 $optionPrefix = '--';
00618 $checkNext = false;
00619 }
00620 else
00621 {
00622 $optionName = $argument[1];
00623 if ( $argumentLen > 2 )
00624 {
00625 $argumentValue = substr( $argument, 2 );
00626 }
00627 $optionType = 'short';
00628 $optionPrefix = '-';
00629 $checkNext = true;
00630 }
00631 $configItem =& $config[$optionType][$optionName];
00632 if ( isset( $configItem ) )
00633 {
00634 $value = true;
00635 $hasValue = $configItem['has-value'];
00636 $hasMultipleValues = ( $configItem['quantifier']['min'] > 1 or
00637 $configItem['quantifier']['max'] === false or
00638 $configItem['quantifier']['max'] > 1 );
00639 if ( $hasValue )
00640 {
00641 $hasArgumentValue = false;
00642 if ( $argumentValue !== false )
00643 {
00644 $value = $argumentValue;
00645 }
00646 else if ( $checkNext and $configItem['has-value'] !== 'optional' )
00647 {
00648 ++$i;
00649 if ( $i < $argumentCount )
00650 {
00651 $hasArgumentValue = true;
00652 }
00653 else
00654 {
00655 --$i;
00656 $cli->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText );
00657 return false;
00658 }
00659 if ( $hasArgumentValue )
00660 $value = $arguments[$i];
00661 }
00662 else
00663 {
00664 if ( $configItem['has-value'] !== 'optional' )
00665 {
00666 $cli->error( "$program: option `$optionPrefix$optionName' requires an argument" . $helpText );
00667 return false;
00668 }
00669 }
00670 }
00671 $optionStoreName = $configItem['store-name'];
00672 if ( !$optionStoreName )
00673 $optionStoreName = $optionName;
00674 if ( $hasMultipleValues )
00675 {
00676 if ( !isset( $options[$optionStoreName] ) )
00677 $options[$optionStoreName] = array();
00678 $options[$optionStoreName][] = $value;
00679 }
00680 else
00681 {
00682 $options[$optionStoreName] = $value;
00683 }
00684 }
00685 else
00686 {
00687 $cli->error( "$program: invalid option `$optionPrefix$optionName'" . $helpText );
00688 return false;
00689 }
00690 }
00691 else
00692 {
00693 $options['arguments'][] = $argument;
00694 }
00695 }
00696 foreach ( $config['list'] as $configItem )
00697 {
00698 $optionStoreName = $configItem['store-name'];
00699 if ( $optionStoreName and !isset( $options[$optionStoreName] ) )
00700 $options[$optionStoreName] = null;
00701 }
00702 return $options;
00703 }
00704
00705
00706
00707
00708 function &instance()
00709 {
00710 $implementation =& $GLOBALS['eZCLIInstance'];
00711 if ( !isset( $implementation ) or
00712 get_class( $implementation ) != 'ezcli' )
00713 {
00714 $implementation = new eZCLI();
00715 }
00716 return $implementation;
00717 }
00718
00719
00720
00721
00722 function hasInstance()
00723 {
00724 $implementation =& $GLOBALS['eZCLIInstance'];
00725 if ( isset( $implementation ) && get_class( $implementation ) == 'ezcli' )
00726 return true;
00727
00728 return false;
00729 }
00730 }
00731
00732 ?>