001-getopt.phpt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. function test($argstr, $optstr) {
  8. $argv = preg_split('/[[:space:]]+/', $argstr);
  9. if (PEAR::isError($options = Console_Getopt::getopt($argv, $optstr))) {
  10. return;
  11. }
  12. $opts = $options[0];
  13. $non_opts = $options[1];
  14. $i = 0;
  15. print "options: ";
  16. foreach ($opts as $o => $d) {
  17. if ($i++ > 0) {
  18. print ", ";
  19. }
  20. print $d[0] . '=' . $d[1];
  21. }
  22. print "\n";
  23. print "params: " . implode(", ", $non_opts) . "\n";
  24. print "\n";
  25. }
  26. test("-abc", "abc");
  27. test("-abc foo", "abc");
  28. test("-abc foo", "abc:");
  29. test("-abc foo bar gazonk", "abc");
  30. test("-abc foo bar gazonk", "abc:");
  31. test("-a -b -c", "abc");
  32. test("-a -b -c", "abc:");
  33. test("-abc", "ab:c");
  34. test("-abc foo -bar gazonk", "abc");
  35. ?>
  36. --EXPECT--
  37. options: a=, b=, c=
  38. params:
  39. options: a=, b=, c=
  40. params: foo
  41. options: a=, b=, c=foo
  42. params:
  43. options: a=, b=, c=
  44. params: foo, bar, gazonk
  45. options: a=, b=, c=foo
  46. params: bar, gazonk
  47. options: a=, b=, c=
  48. params:
  49. Console_Getopt: option requires an argument --c
  50. options: a=, b=c
  51. params:
  52. options: a=, b=, c=
  53. params: foo, -bar, gazonk