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.
* 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
*
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)) {
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);
}
/**
<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">
<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>
$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);
}
$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;