From ab3afbfdf5d9318ca746c1fab2500a2e0c767668 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Thu, 11 Dec 2003 05:54:35 +0000 Subject: [PATCH] since nobody has taken any action, fix Console_Getopt to be BC, and update the PEAR core to take advantage of the new way. Use 1.2 to avoid any problems with versioning. 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 | 42 ++++++++++++++++++++++++++++++--- pear/System.php | 2 +- pear/package-Console_Getopt.xml | 13 +++++++--- pear/package-PEAR.xml | 2 +- pear/scripts/pearcmd.php | 4 ++-- 5 files changed, 53 insertions(+), 10 deletions(-) diff --git a/pear/Console/Getopt.php b/pear/Console/Getopt.php index 133a5bf313..ed07753f37 100644 --- a/pear/Console/Getopt.php +++ b/pear/Console/Getopt.php @@ -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(). + * + * WARNING: 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)) { diff --git a/pear/System.php b/pear/System.php index 66f48fe49f..b2b2822588 100644 --- a/pear/System.php +++ b/pear/System.php @@ -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); } /** diff --git a/pear/package-Console_Getopt.xml b/pear/package-Console_Getopt.xml index 73738173c2..6e5f6e1846 100644 --- a/pear/package-Console_Getopt.xml +++ b/pear/package-Console_Getopt.xml @@ -21,11 +21,18 @@ short and long options. Stig Bakken stig@php.net + + cellog + helper + Greg Beaver + cellog@php.net + - 2.0 - 2003-12-06 - Revert of erroneous POSIX compatibility fix (BC break) + 1.2 + 2003-12-11 + Revert of erroneous POSIX compatibility fix in getopt(). +getopt2() must be used for POSIX compatibility (BC break). stable diff --git a/pear/package-PEAR.xml b/pear/package-PEAR.xml index 2b81e10748..50831ac349 100644 --- a/pear/package-PEAR.xml +++ b/pear/package-PEAR.xml @@ -123,7 +123,7 @@ PEAR Installer: Archive_Tar - Console_Getopt + Console_Getopt XML_RPC xmlrpc xml diff --git a/pear/scripts/pearcmd.php b/pear/scripts/pearcmd.php index 09b2e30af9..338ed406c5 100644 --- a/pear/scripts/pearcmd.php +++ b/pear/scripts/pearcmd.php @@ -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; -- 2.50.1