diff options
author | Stig Bakken <ssb@php.net> | 2002-05-12 07:13:49 +0000 |
---|---|---|
committer | Stig Bakken <ssb@php.net> | 2002-05-12 07:13:49 +0000 |
commit | ebe26ec36c71b4bc72fe9da74aa48114d49dac1d (patch) | |
tree | 519cb4375606c321cd5c5bc612742261561574fe /pear/Console | |
parent | f0a3e7262eeb093eceb25901b16ecbe9f30505a7 (diff) | |
download | php-git-ebe26ec36c71b4bc72fe9da74aa48114d49dac1d.tar.gz |
* Try again: fixed Console_Getopt::getopt so it does not steal options
after the first non-option argument. Added test.
Diffstat (limited to 'pear/Console')
-rw-r--r-- | pear/Console/Getopt.php | 5 | ||||
-rw-r--r-- | pear/Console/tests/001-getopt.phpt | 66 |
2 files changed, 68 insertions, 3 deletions
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-- +<?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 |