From ebe26ec36c71b4bc72fe9da74aa48114d49dac1d Mon Sep 17 00:00:00 2001 From: Stig Bakken Date: Sun, 12 May 2002 07:13:49 +0000 Subject: * Try again: fixed Console_Getopt::getopt so it does not steal options after the first non-option argument. Added test. --- pear/Console/Getopt.php | 5 ++- pear/Console/tests/001-getopt.phpt | 66 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 pear/Console/tests/001-getopt.phpt diff --git a/pear/Console/Getopt.php b/pear/Console/Getopt.php index da6f1dd1c0..d678a5b238 100644 --- a/pear/Console/Getopt.php +++ b/pear/Console/Getopt.php @@ -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 index 0000000000..440a8834c9 --- /dev/null +++ b/pear/Console/tests/001-getopt.phpt @@ -0,0 +1,66 @@ +--TEST-- +Console_Getopt +--FILE-- + $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 -- cgit v1.2.1