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.

165 lines
4.2 KiB

  1. package Getopt::Std;
  2. require 5.000;
  3. require Exporter;
  4. =head1 NAME
  5. getopt - Process single-character switches with switch clustering
  6. getopts - Process single-character switches with switch clustering
  7. =head1 SYNOPSIS
  8. use Getopt::Std;
  9. getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
  10. getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
  11. getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
  12. # Sets opt_* as a side effect.
  13. getopts('oif:', \%opts); # options as above. Values in %opts
  14. =head1 DESCRIPTION
  15. The getopt() functions processes single-character switches with switch
  16. clustering. Pass one argument which is a string containing all switches
  17. that take an argument. For each switch found, sets $opt_x (where x is the
  18. switch name) to the value of the argument, or 1 if no argument. Switches
  19. which take an argument don't care whether there is a space between the
  20. switch and the argument.
  21. Note that, if your code is running under the recommended C<use strict
  22. 'vars'> pragma, it may be helpful to declare these package variables
  23. via C<use vars> perhaps something like this:
  24. use vars qw/ $opt_foo $opt_bar /;
  25. For those of you who don't like additional variables being created, getopt()
  26. and getopts() will also accept a hash reference as an optional second argument.
  27. Hash keys will be x (where x is the switch name) with key values the value of
  28. the argument or 1 if no argument is specified.
  29. =cut
  30. @ISA = qw(Exporter);
  31. @EXPORT = qw(getopt getopts);
  32. $VERSION = $VERSION = '1.01';
  33. # Process single-character switches with switch clustering. Pass one argument
  34. # which is a string containing all switches that take an argument. For each
  35. # switch found, sets $opt_x (where x is the switch name) to the value of the
  36. # argument, or 1 if no argument. Switches which take an argument don't care
  37. # whether there is a space between the switch and the argument.
  38. # Usage:
  39. # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
  40. sub getopt ($;$) {
  41. local($argumentative, $hash) = @_;
  42. local($_,$first,$rest);
  43. local @EXPORT;
  44. while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  45. ($first,$rest) = ($1,$2);
  46. if (index($argumentative,$first) >= 0) {
  47. if ($rest ne '') {
  48. shift(@ARGV);
  49. }
  50. else {
  51. shift(@ARGV);
  52. $rest = shift(@ARGV);
  53. }
  54. if (ref $hash) {
  55. $$hash{$first} = $rest;
  56. }
  57. else {
  58. ${"opt_$first"} = $rest;
  59. push( @EXPORT, "\$opt_$first" );
  60. }
  61. }
  62. else {
  63. if (ref $hash) {
  64. $$hash{$first} = 1;
  65. }
  66. else {
  67. ${"opt_$first"} = 1;
  68. push( @EXPORT, "\$opt_$first" );
  69. }
  70. if ($rest ne '') {
  71. $ARGV[0] = "-$rest";
  72. }
  73. else {
  74. shift(@ARGV);
  75. }
  76. }
  77. }
  78. unless (ref $hash) {
  79. local $Exporter::ExportLevel = 1;
  80. import Getopt::Std;
  81. }
  82. }
  83. # Usage:
  84. # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
  85. # # side effect.
  86. sub getopts ($;$) {
  87. local($argumentative, $hash) = @_;
  88. local(@args,$_,$first,$rest);
  89. local($errs) = 0;
  90. local @EXPORT;
  91. @args = split( / */, $argumentative );
  92. while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  93. ($first,$rest) = ($1,$2);
  94. $pos = index($argumentative,$first);
  95. if($pos >= 0) {
  96. if(defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
  97. shift(@ARGV);
  98. if($rest eq '') {
  99. ++$errs unless @ARGV;
  100. $rest = shift(@ARGV);
  101. }
  102. if (ref $hash) {
  103. $$hash{$first} = $rest;
  104. }
  105. else {
  106. ${"opt_$first"} = $rest;
  107. push( @EXPORT, "\$opt_$first" );
  108. }
  109. }
  110. else {
  111. if (ref $hash) {
  112. $$hash{$first} = 1;
  113. }
  114. else {
  115. ${"opt_$first"} = 1;
  116. push( @EXPORT, "\$opt_$first" );
  117. }
  118. if($rest eq '') {
  119. shift(@ARGV);
  120. }
  121. else {
  122. $ARGV[0] = "-$rest";
  123. }
  124. }
  125. }
  126. else {
  127. warn "Unknown option: $first\n";
  128. ++$errs;
  129. if($rest ne '') {
  130. $ARGV[0] = "-$rest";
  131. }
  132. else {
  133. shift(@ARGV);
  134. }
  135. }
  136. }
  137. unless (ref $hash) {
  138. local $Exporter::ExportLevel = 1;
  139. import Getopt::Std;
  140. }
  141. $errs == 0;
  142. }
  143. 1;