Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
942 B

  1. ;# getopts.pl - a better getopt.pl
  2. ;# Usage:
  3. ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
  4. ;# # side effect.
  5. sub Getopts {
  6. local($argumentative) = @_;
  7. local(@args,$_,$first,$rest);
  8. local($errs) = 0;
  9. @args = split( / */, $argumentative );
  10. while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  11. ($first,$rest) = ($1,$2);
  12. $pos = index($argumentative,$first);
  13. if($pos >= 0) {
  14. if($pos < $#args && $args[$pos+1] eq ':') {
  15. shift(@ARGV);
  16. if($rest eq '') {
  17. ++$errs unless @ARGV;
  18. $rest = shift(@ARGV);
  19. }
  20. ${"opt_$first"} = $rest;
  21. }
  22. else {
  23. ${"opt_$first"} = 1;
  24. if($rest eq '') {
  25. shift(@ARGV);
  26. }
  27. else {
  28. $ARGV[0] = "-$rest";
  29. }
  30. }
  31. }
  32. else {
  33. print STDERR "Unknown option: $first\n";
  34. ++$errs;
  35. if($rest ne '') {
  36. $ARGV[0] = "-$rest";
  37. }
  38. else {
  39. shift(@ARGV);
  40. }
  41. }
  42. }
  43. $errs == 0;
  44. }
  45. 1;