コード例 #1
0
ファイル: Getopt.php プロジェクト: rixrix/agnos
 /**
  * The actual implementation of the argument parsing code.
  */
 function doGetopt($version, $args, $short_options, $long_options = null)
 {
     // in case you pass directly readPHPArgv() as the first arg
     if ($args == -1) {
         return $args;
     }
     if (empty($args)) {
         return array(array(), array());
     }
     $opts = array();
     $non_opts = array();
     settype($args, 'array');
     if ($long_options) {
         sort($long_options);
     }
     /*
      * Preserve backwards compatibility with callers that relied on
      * erroneous POSIX fix.
      */
     if ($version < 2) {
         if (isset($args[0][0]) && $args[0][0] != '-') {
             array_shift($args);
         }
     }
     reset($args);
     while (list($i, $arg) = each($args)) {
         /* The special element '--' means explicit end of
            options. Treat the rest of the arguments as non-options
            and end the loop. */
         if ($arg == '--') {
             $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
             break;
         }
         if ($arg[0] != '-' || strlen($arg) > 1 && $arg[1] == '-' && !$long_options) {
             $non_opts = array_merge($non_opts, array_slice($args, $i));
             break;
         } elseif (strlen($arg) > 1 && $arg[1] == '-') {
             $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
             if ($error == -1) {
                 return $error;
             }
         } elseif ($arg == '-') {
             // - is stdin
             $non_opts = array_merge($non_opts, array_slice($args, $i));
             break;
         } else {
             $error = Console_Getopt::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
             if ($error == -1) {
                 return $error;
             }
         }
     }
     $error = Console_Getopt::_parseCloneOption($opts, $short_options, $long_options);
     if ($error == false) {
         return -1;
     }
     return array($opts, $non_opts);
 }