]> granicus.if.org Git - php/commitdiff
since nobody has taken any action, fix Console_Getopt to be BC, and update the PEAR...
authorGreg Beaver <cellog@php.net>
Thu, 11 Dec 2003 05:54:35 +0000 (05:54 +0000)
committerGreg Beaver <cellog@php.net>
Thu, 11 Dec 2003 05:54:35 +0000 (05:54 +0000)
The core passes all unit tests with these changes, so they should work.

Andrei: feel free to change anything you don't like, this is just a make it work fix.

pear/Console/Getopt.php
pear/System.php
pear/package-Console_Getopt.xml
pear/package-PEAR.xml
pear/scripts/pearcmd.php

index 133a5bf3134ea213ba9a110fc7a319dc08aadb8a..ed07753f374f6b70c08127964237780c0a09f8e8 100644 (file)
@@ -52,8 +52,12 @@ class Console_Getopt {
      * Long and short options can be mixed.
      *
      * Most of the semantics of this function are based on GNU getopt_long().
+     * 
+     * <b>WARNING</b>: this function does not maintain full compatibility with GNU getopt_long().
+     * To have full compatibility, use {@link getopt2()}
      *
-     * @param array  $args           an array of command-line arguments
+     * @param array  $args           an array of command-line arguments.  The first argument
+     *                               should be the filename (like $argv[0]), unless it begins with -
      * @param string $short_options  specifies the list of allowed short options
      * @param array  $long_options   specifies the list of allowed long options
      *
@@ -72,15 +76,47 @@ class Console_Getopt {
         if (empty($args)) {
             return array(array(), array());
         }
-        $opts     = array();
-        $non_opts = array();
 
         settype($args, 'array');
 
         if ($long_options) {
             sort($long_options);
         }
+        if (isset($args[0]{0}) && $args[0]{0} != '-') {
+            array_shift($args);
+        }
+        return Console_Getopt::doGetopt($args, $short_options, $long_options);
+    }
 
+    /**
+     * This function expects $args to contain only options and values
+     * @see getopt()
+     */    
+    function getopt2($args, $short_options, $long_options = null)
+    {
+        // in case you pass directly readPHPArgv() as the first arg
+        if (PEAR::isError($args)) {
+            return $args;
+        }
+        if (empty($args)) {
+            return array(array(), array());
+        }
+
+        settype($args, 'array');
+
+        if ($long_options) {
+            sort($long_options);
+        }
+        return Console_Getopt::doGetopt($args, $short_options, $long_options);
+    }
+    
+    /**
+     * The meat of {@link getopt()} and {@link getopt2()}
+     */
+    function doGetopt($args, $short_options, $long_options = null)
+    {
+        $opts     = array();
+        $non_opts = array();
         reset($args);
         while (list($i, $arg) = each($args)) {
 
index 66f48fe49f74051b971b47f289adaad4d564532a..b2b282258877ac1368a27d16d168ccb562d13dec 100644 (file)
@@ -68,7 +68,7 @@ class System
         if (!is_array($argv) && $argv !== null) {
             $argv = preg_split('/\s+/', $argv);
         }
-        return Console_Getopt::getopt($argv, $short_options);
+        return Console_Getopt::getopt2($argv, $short_options);
     }
 
     /**
index 73738173c28e1cb80ceb6116305dfed778a9b14b..6e5f6e1846f2c1e9c71644712d1db89643d079f3 100644 (file)
@@ -21,11 +21,18 @@ short and long options.</description>
       <name>Stig Bakken</name>
       <email>stig@php.net</email>
     </maintainer>
+    <maintainer>
+      <user>cellog</user>
+      <role>helper</role>
+      <name>Greg Beaver</name>
+      <email>cellog@php.net</email>
+    </maintainer>
   </maintainers>
   <release>
-    <version>2.0</version>
-    <date>2003-12-06</date>
-    <notes>Revert of erroneous POSIX compatibility fix (BC break)</notes>
+    <version>1.2</version>
+    <date>2003-12-11</date>
+    <notes>Revert of erroneous POSIX compatibility fix in getopt().
+getopt2() must be used for POSIX compatibility (BC break).</notes>
     <state>stable</state>
     <filelist>
      <dir name="Console">
index 2b81e10748478b44e525de74e8d2bedafb028ac7..50831ac349c5383e721d9dd6730d139c3ab3804c 100644 (file)
@@ -123,7 +123,7 @@ PEAR Installer:
     <deps>
       <dep type="php" rel="ge" version="4.1"/>
       <dep type="pkg" rel="ge" version="1.1">Archive_Tar</dep>
-      <dep type="pkg" rel="ge" version="2.0">Console_Getopt</dep>
+      <dep type="pkg" rel="ge" version="1.2">Console_Getopt</dep>
       <dep type="pkg" rel="ge" version="1.0.4">XML_RPC</dep>
       <dep type="ext" rel="has" optional="yes">xmlrpc</dep>
       <dep type="ext" rel="has">xml</dep>
index 09b2e30af9a8cf4aafa68398aa70a015304fcb1d..338ed406c5cae0aaeefedcbbfc8d1bbe19dce88f 100644 (file)
@@ -49,7 +49,7 @@ $all_commands = PEAR_Command::getCommands();
 $argv = Console_Getopt::readPHPArgv();
 $progname = basename($argv[0]);
 array_shift($argv);
-$options = Console_Getopt::getopt($argv, "c:C:d:D:Gh?sSqu:vV");
+$options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
 if (PEAR::isError($options)) {
     usage($options);
 }
@@ -157,7 +157,7 @@ if ($fetype == 'Gtk') {
     $short_args = $long_args = null;
     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
     array_shift($options[1]);
-    if (PEAR::isError($tmp = Console_Getopt::getopt($options[1], $short_args, $long_args))) {
+    if (PEAR::isError($tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args))) {
         break;
     }
     list($tmpopt, $params) = $tmp;