]> granicus.if.org Git - php/commitdiff
* Try again: fixed Console_Getopt::getopt so it does not steal options
authorStig Bakken <ssb@php.net>
Sun, 12 May 2002 07:13:49 +0000 (07:13 +0000)
committerStig Bakken <ssb@php.net>
Sun, 12 May 2002 07:13:49 +0000 (07:13 +0000)
  after the first non-option argument.  Added test.

pear/Console/Getopt.php
pear/Console/tests/001-getopt.phpt [new file with mode: 0644]

index da6f1dd1c0f9ef278cf9ca6a8e244c2e951e0f4f..d678a5b238e2035a7650d7e14579ebd1b6e27363 100644 (file)
@@ -89,9 +89,8 @@ class Console_Getopt {
             }
 
             if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
-                $non_opts[] = $arg;
-                //$non_opts = array_merge($non_opts, array_slice($args, $i));
-                //break;
+                $non_opts = array_merge($non_opts, array_slice($args, $i));
+                break;
             } elseif (strlen($arg) > 1 && $arg{1} == '-') {
                 $error = Console_Getopt::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
                 if (PEAR::isError($error))
diff --git a/pear/Console/tests/001-getopt.phpt b/pear/Console/tests/001-getopt.phpt
new file mode 100644 (file)
index 0000000..440a883
--- /dev/null
@@ -0,0 +1,66 @@
+--TEST--
+Console_Getopt
+--FILE--
+<?php
+
+error_reporting(E_ALL);
+chdir(dirname(__FILE__));
+include "../Getopt.php";
+PEAR::setErrorHandling(PEAR_ERROR_PRINT, "%s\n\n");
+
+function test($argstr, $optstr) {
+    $argv = split('[[:space:]]+', $argstr);
+    if (PEAR::isError($options = Console_Getopt::getopt($argv, $optstr))) {
+        return;
+    }
+    $opts = $options[0];
+    $non_opts = $options[1];
+    $i = 0;
+    print "options: ";
+    foreach ($opts as $o => $d) {
+        if ($i++ > 0) {
+            print ", ";
+        }
+        print $d[0] . '=' . $d[1];
+    }
+    print "\n";
+    print "params: " . implode(", ", $non_opts) . "\n";
+    print "\n";
+}
+
+test("-abc", "abc");
+test("-abc foo", "abc");
+test("-abc foo", "abc:");
+test("-abc foo bar gazonk", "abc");
+test("-abc foo bar gazonk", "abc:");
+test("-a -b -c", "abc");
+test("-a -b -c", "abc:");
+test("-abc", "ab:c");
+test("-abc foo -bar gazonk", "abc");
+?>
+--EXPECT--
+options: a=, b=, c=
+params: 
+
+options: a=, b=, c=
+params: foo
+
+options: a=, b=, c=foo
+params: 
+
+options: a=, b=, c=
+params: foo, bar, gazonk
+
+options: a=, b=, c=foo
+params: bar, gazonk
+
+options: a=, b=, c=
+params: 
+
+Console_Getopt: option requires an argument -- c
+
+options: a=, b=c
+params: 
+
+options: a=, b=, c=
+params: foo, -bar, gazonk