| 1 |
--TEST-- |
| 2 |
Console_Getopt |
| 3 |
--FILE-- |
| 4 |
<?php |
| 5 |
require_once 'Console/Getopt.php'; |
| 6 |
PEAR::setErrorHandling(PEAR_ERROR_PRINT, "%s\n\n"); |
| 7 |
|
| 8 |
function test($argstr, $optstr) { |
| 9 |
$argv = preg_split('/[[:space:]]+/', $argstr); |
| 10 |
if (PEAR::isError($options = Console_Getopt::getopt($argv, $optstr))) { |
| 11 |
return; |
| 12 |
} |
| 13 |
$opts = $options[0]; |
| 14 |
$non_opts = $options[1]; |
| 15 |
$i = 0; |
| 16 |
print "options: "; |
| 17 |
foreach ($opts as $o => $d) { |
| 18 |
if ($i++ > 0) { |
| 19 |
print ", "; |
| 20 |
} |
| 21 |
print $d[0] . '=' . $d[1]; |
| 22 |
} |
| 23 |
print "\n"; |
| 24 |
print "params: " . implode(", ", $non_opts) . "\n"; |
| 25 |
print "\n"; |
| 26 |
} |
| 27 |
|
| 28 |
test("-abc", "abc"); |
| 29 |
test("-abc foo", "abc"); |
| 30 |
test("-abc foo", "abc:"); |
| 31 |
test("-abc foo bar gazonk", "abc"); |
| 32 |
test("-abc foo bar gazonk", "abc:"); |
| 33 |
test("-a -b -c", "abc"); |
| 34 |
test("-a -b -c", "abc:"); |
| 35 |
test("-abc", "ab:c"); |
| 36 |
test("-abc foo -bar gazonk", "abc"); |
| 37 |
?> |
| 38 |
--EXPECT-- |
| 39 |
options: a=, b=, c= |
| 40 |
params: |
| 41 |
|
| 42 |
options: a=, b=, c= |
| 43 |
params: foo |
| 44 |
|
| 45 |
options: a=, b=, c=foo |
| 46 |
params: |
| 47 |
|
| 48 |
options: a=, b=, c= |
| 49 |
params: foo, bar, gazonk |
| 50 |
|
| 51 |
options: a=, b=, c=foo |
| 52 |
params: bar, gazonk |
| 53 |
|
| 54 |
options: a=, b=, c= |
| 55 |
params: |
| 56 |
|
| 57 |
Console_Getopt: option requires an argument --c |
| 58 |
|
| 59 |
options: a=, b=c |
| 60 |
params: |
| 61 |
|
| 62 |
options: a=, b=, c= |
| 63 |
params: foo, -bar, gazonk |