[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/ -> cliopts.php (source)

   1  <?php
   2  /**
   3  * Brutally chopped and modified from http://pear.php.net/package/Console_Getopts
   4  */
   5  // +----------------------------------------------------------------------+
   6  // | PHP Version 4                                                        |
   7  // +----------------------------------------------------------------------+
   8  // | Copyright (c) 1997-2003 The PHP Group                                |
   9  // +----------------------------------------------------------------------+
  10  // | This source file is subject to version 3.0 of the PHP license,       |
  11  // | that is bundled with this package in the file LICENSE, and is        |
  12  // | available through the world-wide-web at the following url:           |
  13  // | http://www.php.net/license/3_0.txt.                                  |
  14  // | If you did not receive a copy of the PHP license and are unable to   |
  15  // | obtain it through the world-wide-web, please send a note to          |
  16  // | license@php.net so we can mail you a copy immediately.               |
  17  // +----------------------------------------------------------------------+
  18  // | Author: Andrei Zmievski <andrei@php.net>                             |
  19  // | Modified: Harry Fuecks hfuecks  gmail.com                   |
  20  // +----------------------------------------------------------------------+
  21  //
  22  
  23  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  24  
  25  //------------------------------------------------------------------------------
  26  /**
  27  * Sets up CLI environment based on SAPI and PHP version
  28  * Helps resolve some issues between the CGI and CLI SAPIs
  29  * as well is inconsistencies between PHP 4.3+ and older versions
  30  */
  31  if (version_compare(phpversion(), '4.3.0', '<') || php_sapi_name() == 'cgi') {
  32      // Handle output buffering
  33      @ob_end_flush();
  34      ob_implicit_flush(TRUE);
  35  
  36      // PHP ini settings
  37      set_time_limit(0);
  38      ini_set('track_errors', TRUE);
  39      ini_set('html_errors', FALSE);
  40      ini_set('magic_quotes_runtime', FALSE);
  41  
  42      // Define stream constants
  43      define('STDIN', fopen('php://stdin', 'r'));
  44      define('STDOUT', fopen('php://stdout', 'w'));
  45      define('STDERR', fopen('php://stderr', 'w'));
  46  
  47      // Close the streams on script termination
  48      register_shutdown_function(
  49          create_function('',
  50          'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')
  51          );
  52  }
  53  
  54  //------------------------------------------------------------------------------
  55  /**
  56  * Error codes
  57  */
  58  define('DOKU_CLI_OPTS_UNKNOWN_OPT',1); //Unrecognized option
  59  define('DOKU_CLI_OPTS_OPT_ARG_REQUIRED',2); //Option requires argument
  60  define('DOKU_CLI_OPTS_OPT_ARG_DENIED',3); //Option not allowed argument
  61  define('DOKU_CLI_OPTS_OPT_ABIGUOUS',4);//Option abiguous
  62  define('DOKU_CLI_OPTS_ARG_READ',5);//Could not read argv
  63  
  64  //------------------------------------------------------------------------------
  65  /**
  66   * Command-line options parsing class.
  67   *
  68   * @author Andrei Zmievski <andrei@php.net>
  69   *
  70   */
  71   class Doku_Cli_Opts {
  72  
  73      /**
  74      * <?php ?>
  75      * @see http://www.sitepoint.com/article/php-command-line-1/3
  76      * @param string executing file name - this MUST be passed the __FILE__ constant
  77      * @param string short options
  78      * @param array (optional) long options
  79      * @return Doku_Cli_Opts_Container or Doku_Cli_Opts_Error
  80      */
  81      function & getOptions($bin_file, $short_options, $long_options = null) {
  82          $args = Doku_Cli_Opts::readPHPArgv();
  83  
  84          if ( Doku_Cli_Opts::isError($args) ) {
  85              return $args;
  86          }
  87  
  88          // Compatibility between "php extensions.php" and "./extensions.php"
  89          if ( realpath($_SERVER['argv'][0]) == $bin_file ) {
  90              $options = Doku_Cli_Opts::getOpt($args,$short_options,$long_options);
  91          } else {
  92              $options = Doku_Cli_Opts::getOpt2($args,$short_options,$long_options);
  93          }
  94  
  95          if ( Doku_Cli_Opts::isError($options) ) {
  96              return $options;
  97          }
  98  
  99          $container = new Doku_Cli_Opts_Container($options);
 100          return $container;
 101      }
 102  
 103      function getopt2($args, $short_options, $long_options = null) {
 104          return Doku_Cli_Opts::doGetopt(
 105              2, $args, $short_options, $long_options
 106              );
 107      }
 108  
 109      function getopt($args, $short_options, $long_options = null) {
 110          return Doku_Cli_Opts::doGetopt(
 111              1, $args, $short_options, $long_options
 112              );
 113      }
 114  
 115      function doGetopt($version, $args, $short_options, $long_options = null) {
 116  
 117          // in case you pass directly readPHPArgv() as the first arg
 118          if (Doku_Cli_Opts::isError($args)) {
 119              return $args;
 120          }
 121          if (empty($args)) {
 122              return array(array(), array());
 123          }
 124          $opts     = array();
 125          $non_opts = array();
 126  
 127          settype($args, 'array');
 128  
 129          if ($long_options && is_array($long_options)) {
 130              sort($long_options);
 131          }
 132  
 133          /*
 134           * Preserve backwards compatibility with callers that relied on
 135           * erroneous POSIX fix.
 136           */
 137          if ($version < 2) {
 138              if (isset($args[0]{0}) && $args[0]{0} != '-') {
 139                  array_shift($args);
 140              }
 141          }
 142  
 143          reset($args);
 144          while (list($i, $arg) = each($args)) {
 145  
 146              /* The special element '--' means explicit end of
 147                 options. Treat the rest of the arguments as non-options
 148                 and end the loop. */
 149              if ($arg == '--') {
 150                  $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
 151                  break;
 152              }
 153  
 154              if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
 155                  $non_opts = array_merge($non_opts, array_slice($args, $i));
 156                  break;
 157              } elseif (strlen($arg) > 1 && $arg{1} == '-') {
 158                  $error = Doku_Cli_Opts::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
 159                  if (Doku_Cli_Opts::isError($error))
 160                      return $error;
 161              } else {
 162                  $error = Doku_Cli_Opts::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
 163                  if (Doku_Cli_Opts::isError($error))
 164                      return $error;
 165              }
 166          }
 167  
 168          return array($opts, $non_opts);
 169      }
 170  
 171      function _parseShortOption($arg, $short_options, &$opts, &$args) {
 172          for ($i = 0; $i < strlen($arg); $i++) {
 173              $opt = $arg{$i};
 174              $opt_arg = null;
 175  
 176              /* Try to find the short option in the specifier string. */
 177              if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
 178              {
 179                  return Doku_Cli_Opts::raiseError(
 180                      DOKU_CLI_OPTS_UNKNOWN_OPT,
 181                      "Unrecognized option -- $opt"
 182                      );
 183              }
 184  
 185              if (strlen($spec) > 1 && $spec{1} == ':') {
 186                  if (strlen($spec) > 2 && $spec{2} == ':') {
 187                      if ($i + 1 < strlen($arg)) {
 188                          /* Option takes an optional argument. Use the remainder of
 189                             the arg string if there is anything left. */
 190                          $opts[] = array($opt, substr($arg, $i + 1));
 191                          break;
 192                      }
 193                  } else {
 194                      /* Option requires an argument. Use the remainder of the arg
 195                         string if there is anything left. */
 196                      if ($i + 1 < strlen($arg)) {
 197                          $opts[] = array($opt,  substr($arg, $i + 1));
 198                          break;
 199                      } else if (list(, $opt_arg) = each($args))
 200                          /* Else use the next argument. */;
 201                      else
 202                          return Doku_Cli_Opts::raiseError(
 203                              DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
 204                              "Option requires an argument -- $opt"
 205                              );
 206                  }
 207              }
 208  
 209              $opts[] = array($opt, $opt_arg);
 210          }
 211      }
 212  
 213      function _parseLongOption($arg, $long_options, &$opts, &$args) {
 214          @list($opt, $opt_arg) = explode('=', $arg);
 215          $opt_len = strlen($opt);
 216  
 217          for ($i = 0; $i < count($long_options); $i++) {
 218              $long_opt  = $long_options[$i];
 219              $opt_start = substr($long_opt, 0, $opt_len);
 220  
 221              /* Option doesn't match. Go on to the next one. */
 222              if ($opt_start != $opt)
 223                  continue;
 224  
 225              $opt_rest  = substr($long_opt, $opt_len);
 226  
 227              /* Check that the options uniquely matches one of the allowed
 228                 options. */
 229              if ($opt_rest != '' && $opt{0} != '=' &&
 230                  $i + 1 < count($long_options) &&
 231                  $opt == substr($long_options[$i+1], 0, $opt_len)) {
 232                  return Doku_Cli_Opts::raiseError(
 233                      DOKU_CLI_OPTS_OPT_ABIGUOUS,
 234                      "Option --$opt is ambiguous"
 235                      );
 236              }
 237  
 238              if (substr($long_opt, -1) == '=') {
 239                  if (substr($long_opt, -2) != '==') {
 240                      /* Long option requires an argument.
 241                         Take the next argument if one wasn't specified. */;
 242                      if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
 243                          return Doku_Cli_Opts::raiseError(
 244                              DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
 245                              "Option --$opt requires an argument"
 246                              );
 247                      }
 248                  }
 249              } else if ($opt_arg) {
 250                  return Doku_Cli_Opts::raiseError(
 251                      DOKU_CLI_OPTS_OPT_ARG_DENIED,
 252                      "Option --$opt doesn't allow an argument"
 253                      );
 254              }
 255  
 256              $opts[] = array('--' . $opt, $opt_arg);
 257              return;
 258          }
 259  
 260          return Doku_Cli_Opts::raiseError(
 261              DOKU_CLI_OPTS_UNKNOWN_OPT,
 262              "Unrecognized option --$opt"
 263              );
 264      }
 265  
 266      function readPHPArgv() {
 267          global $argv;
 268          if (!is_array($argv)) {
 269              if (!@is_array($_SERVER['argv'])) {
 270                  if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
 271                      return Doku_Cli_Opts::raiseError(
 272                          DOKU_CLI_OPTS_ARG_READ,
 273                          "Could not read cmd args (register_argc_argv=Off?)"
 274                          );
 275                  }
 276                  return $GLOBALS['HTTP_SERVER_VARS']['argv'];
 277              }
 278              return $_SERVER['argv'];
 279          }
 280          return $argv;
 281      }
 282  
 283      function raiseError($code, $msg) {
 284          return new Doku_Cli_Opts_Error($code, $msg);
 285      }
 286  
 287      function isError($obj) {
 288          return is_a($obj, 'Doku_Cli_Opts_Error');
 289      }
 290  
 291  }
 292  
 293  //------------------------------------------------------------------------------
 294  class Doku_Cli_Opts_Error {
 295  
 296      var $code;
 297      var $msg;
 298  
 299      function Doku_Cli_Opts_Error($code, $msg) {
 300          $this->code = $code;
 301          $this->msg = $msg;
 302      }
 303  
 304      function getMessage() {
 305          return $this->msg;
 306      }
 307  
 308      function isError() {
 309          return TRUE;
 310      }
 311  
 312  }
 313  
 314  //------------------------------------------------------------------------------
 315  class Doku_Cli_Opts_Container {
 316  
 317      var $options = array();
 318      var $args = array();
 319  
 320      function Doku_Cli_Opts_Container($options) {
 321          foreach ( $options[0] as $option ) {
 322              if ( FALSE !== ( strpos($option[0], '--') ) ) {
 323                  $opt_name = substr($option[0], 2);
 324              } else {
 325                  $opt_name = $option[0];
 326              }
 327              $this->options[$opt_name] = $option[1];
 328          }
 329  
 330  
 331          $this->args = $options[1];
 332      }
 333  
 334      function has($option) {
 335          return array_key_exists($option, $this->options);
 336      }
 337  
 338      function get($option) {
 339          if ( isset($this->options[$option]) ) {
 340              return ( $this->options[$option] ) ;
 341          }
 342      }
 343  
 344      function arg($index) {
 345          if ( isset($this->args[$index]) ) {
 346              return $this->args[$index];
 347          }
 348      }
 349  
 350      function numArgs() {
 351          return count($this->args);
 352      }
 353  
 354      function hasArgs() {
 355          return count($this->args) !== 0;
 356      }
 357  
 358      function isError() {
 359          return FALSE;
 360      }
 361  
 362  }


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7