summaryrefslogtreecommitdiff
path: root/pear/Console
diff options
context:
space:
mode:
authorGreg Beaver <cellog@php.net>2003-12-11 05:54:35 +0000
committerGreg Beaver <cellog@php.net>2003-12-11 05:54:35 +0000
commitab3afbfdf5d9318ca746c1fab2500a2e0c767668 (patch)
tree831dc99ee6688f4dfaeb1138c153ab8680509121 /pear/Console
parent3cd16913a8c61e03b39234ba207874e62b8bb263 (diff)
downloadphp-git-ab3afbfdf5d9318ca746c1fab2500a2e0c767668.tar.gz
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.
Diffstat (limited to 'pear/Console')
-rw-r--r--pear/Console/Getopt.php42
1 files changed, 39 insertions, 3 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().
+ *
+ * <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)) {