]> granicus.if.org Git - php/commitdiff
MFB
authorAndrei Zmievski <andrei@php.net>
Thu, 11 Dec 2003 19:37:03 +0000 (19:37 +0000)
committerAndrei Zmievski <andrei@php.net>
Thu, 11 Dec 2003 19:37:03 +0000 (19:37 +0000)
pear/Console/Getopt.php
pear/package-Console_Getopt.xml

index ed07753f374f6b70c08127964237780c0a09f8e8..52b2dea049ddf36bc4a1b993ae00f28bb103e22e 100644 (file)
@@ -52,12 +52,8 @@ 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.  The first argument
-     *                               should be the filename (like $argv[0]), unless it begins with -
+     * @param array  $args           an array of command-line arguments
      * @param string $short_options  specifies the list of allowed short options
      * @param array  $long_options   specifies the list of allowed long options
      *
@@ -67,32 +63,25 @@ class Console_Getopt {
      * @access public
      *
      */
-    function getopt($args, $short_options, $long_options = null)
+    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);
-        }
-        if (isset($args[0]{0}) && $args[0]{0} != '-') {
-            array_shift($args);
-        }
-        return Console_Getopt::doGetopt($args, $short_options, $long_options);
+        return Console_Getopt::doGetopt(2, $args, $short_options, $long_options);
     }
 
     /**
-     * This function expects $args to contain only options and values
-     * @see getopt()
+     * This function expects $args to start with the script name (POSIX-style).
+     * Preserved for backwards compatibility.
+     * @see getopt2()
      */    
-    function getopt2($args, $short_options, $long_options = null)
+    function getopt($args, $short_options, $long_options = null)
+    {
+        return Console_Getopt::doGetopt(1, $args, $short_options, $long_options);
+    }
+
+    /**
+     * 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 (PEAR::isError($args)) {
@@ -101,22 +90,25 @@ class Console_Getopt {
         if (empty($args)) {
             return array(array(), array());
         }
+        $opts     = array();
+        $non_opts = 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();
+
+        /*
+         * 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)) {
 
index 6e5f6e1846f2c1e9c71644712d1db89643d079f3..57769e6b9291cf2de530dec3a5523ec76fe4069e 100644 (file)
@@ -31,17 +31,27 @@ short and long options.</description>
   <release>
     <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>
+    <notes>Fix to preserve BC with 1.0 and allow correct behaviour for new users</notes>
     <state>stable</state>
     <filelist>
-     <dir name="Console">
-      <file role="php" name="Getopt.php"/>
-     </dir>
+      <dir name="Console">
+        <file role="php" name="Getopt.php"/>
+      </dir>
     </filelist>
   </release>
   <changelog>
     <release>
+      <version>1.0</version>
+      <date>2002-09-13</date>
+      <notes>Stable release</notes>
+      <state>stable</state>
+      <filelist>
+        <dir name="Console">
+          <file role="php" name="Getopt.php"/>
+        </dir>
+      </filelist>
+    </release>
+   <release>
       <version>0.11</version>
       <date>2002-05-26</date>
       <notes>POSIX getopt compatibility fix: treat first element of args