Esempio n. 1
0
 /**
  * Parses command line-arguments and returns found options/arguments.
  *
  *  input (for tests only, by default it takes real command-line option list):
  *      array("scriptname.php", "--help", "-d", "foo=bar", "--config=xref.ini", "filename.php")
  *
  *  output:
  *      array(
  *          array( "help" => true, "define" => array("foo=bar"), "config" => "xref.ini"),
  *          array( "filename.php" )
  *      );
  *
  * @return array(array $commandLineOptions, array $commandLineArguments)
  */
 public static function getCmdOptions($test_args = null)
 {
     if (is_null($test_args)) {
         if (self::$options) {
             return array(self::$options, self::$arguments);
         }
         if (php_sapi_name() != 'cli') {
             return array(array(), array());
         }
     }
     $short_options_list = array();
     // array( 'h', 'v', 'c:' )
     $long_options_list = array();
     // array( 'help', 'verbose', 'config=' )
     $rename_map = array();
     // array( 'h' => 'help', 'c' => 'config' )
     $is_array_map = array();
     // array( 'help' => false, 'define' => true, )
     foreach (self::$optionsList as $o) {
         $short = $o[0];
         $long = $o[1];
         if ($short) {
             $short_options_list[] = $short;
         }
         if ($long) {
             $long_options_list[] = $long;
         }
         $short = preg_replace('/\\W+$/', '', $short);
         // remove ':' and '=' at the end of specificatios
         $long = preg_replace('/\\W+$/', '', $long);
         if ($short && $long) {
             $rename_map[$short] = $long;
         }
         $is_array_map[$long ? $long : $short] = $o[4];
     }
     // * DONE: write a better command-line parser
     // * too bad: the code below (from official Console/Getopt documentation) doesn't work in E_STRICT mode
     // * and Console_GetoptPlus is not installed by default on most systems :(
     // $error_reporting = error_reporting();
     // error_reporting($error_reporting & ~E_STRICT);
     // require_once 'Console/Getopt.php';
     // $getopt = new Console_Getopt();
     // $args = ($test_args) ? $test_args : $getopt->readPHPArgv();
     // $getoptResult = $getopt->getopt( $args, implode('', $short_options_list), $long_options_list);
     // if (PEAR::isError($getoptResult)) {
     //     throw new Exception( $getoptResult->getMessage() );
     // }
     // error_reporting($error_reporting);
     // * end of Console_Getopt dependent code
     global $argv;
     $args = $test_args ? $test_args : $argv;
     $getopt_result = self::myGetopt($args, implode('', $short_options_list), $long_options_list);
     if (!is_array($getopt_result)) {
         throw new Exception($getopt_result);
     }
     $options = array();
     list($opt_list, $arguments) = $getopt_result;
     foreach ($opt_list as $o) {
         list($k, $v) = $o;
         // change default value for command-line options that doesn't require a value
         if (is_null($v)) {
             $v = true;
         }
         // '-a' --> 'a', '--foo' -> 'foo'
         $k = preg_replace('#^-+#', '', $k);
         // force long option names
         if (isset($rename_map[$k])) {
             $k = $rename_map[$k];
         }
         if ($is_array_map[$k]) {
             if (!isset($options[$k])) {
                 $options[$k] = array();
             }
             $options[$k][] = $v;
         } else {
             $options[$k] = $v;
         }
     }
     self::$options = $options;
     self::$arguments = $arguments;
     return array($options, $arguments);
 }