Leaked source code of windows server 2003
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.

176 lines
4.4 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, you will need to declare these package variables
  23. with "our":
  24. our($opt_foo, $opt_bar);
  25. For those of you who don't like additional global 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. To allow programs to process arguments that look like switches, but aren't,
  30. both functions will stop processing switches when they see the argument
  31. C<-->. The C<--> will be removed from @ARGV.
  32. =cut
  33. @ISA = qw(Exporter);
  34. @EXPORT = qw(getopt getopts);
  35. $VERSION = '1.02';
  36. # Process single-character switches with switch clustering. Pass one argument
  37. # which is a string containing all switches that take an argument. For each
  38. # switch found, sets $opt_x (where x is the switch name) to the value of the
  39. # argument, or 1 if no argument. Switches which take an argument don't care
  40. # whether there is a space between the switch and the argument.
  41. # Usage:
  42. # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
  43. sub getopt ($;$) {
  44. local($argumentative, $hash) = @_;
  45. local($_,$first,$rest);
  46. local @EXPORT;
  47. while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  48. ($first,$rest) = ($1,$2);
  49. if (/^--$/) { # early exit if --
  50. shift @ARGV;
  51. last;
  52. }
  53. if (index($argumentative,$first) >= 0) {
  54. if ($rest ne '') {
  55. shift(@ARGV);
  56. }
  57. else {
  58. shift(@ARGV);
  59. $rest = shift(@ARGV);
  60. }
  61. if (ref $hash) {
  62. $$hash{$first} = $rest;
  63. }
  64. else {
  65. ${"opt_$first"} = $rest;
  66. push( @EXPORT, "\$opt_$first" );
  67. }
  68. }
  69. else {
  70. if (ref $hash) {
  71. $$hash{$first} = 1;
  72. }
  73. else {
  74. ${"opt_$first"} = 1;
  75. push( @EXPORT, "\$opt_$first" );
  76. }
  77. if ($rest ne '') {
  78. $ARGV[0] = "-$rest";
  79. }
  80. else {
  81. shift(@ARGV);
  82. }
  83. }
  84. }
  85. unless (ref $hash) {
  86. local $Exporter::ExportLevel = 1;
  87. import Getopt::Std;
  88. }
  89. }
  90. # Usage:
  91. # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
  92. # # side effect.
  93. sub getopts ($;$) {
  94. local($argumentative, $hash) = @_;
  95. local(@args,$_,$first,$rest);
  96. local($errs) = 0;
  97. local @EXPORT;
  98. @args = split( / */, $argumentative );
  99. while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  100. ($first,$rest) = ($1,$2);
  101. if (/^--$/) { # early exit if --
  102. shift @ARGV;
  103. last;
  104. }
  105. $pos = index($argumentative,$first);
  106. if ($pos >= 0) {
  107. if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
  108. shift(@ARGV);
  109. if ($rest eq '') {
  110. ++$errs unless @ARGV;
  111. $rest = shift(@ARGV);
  112. }
  113. if (ref $hash) {
  114. $$hash{$first} = $rest;
  115. }
  116. else {
  117. ${"opt_$first"} = $rest;
  118. push( @EXPORT, "\$opt_$first" );
  119. }
  120. }
  121. else {
  122. if (ref $hash) {
  123. $$hash{$first} = 1;
  124. }
  125. else {
  126. ${"opt_$first"} = 1;
  127. push( @EXPORT, "\$opt_$first" );
  128. }
  129. if ($rest eq '') {
  130. shift(@ARGV);
  131. }
  132. else {
  133. $ARGV[0] = "-$rest";
  134. }
  135. }
  136. }
  137. else {
  138. warn "Unknown option: $first\n";
  139. ++$errs;
  140. if ($rest ne '') {
  141. $ARGV[0] = "-$rest";
  142. }
  143. else {
  144. shift(@ARGV);
  145. }
  146. }
  147. }
  148. unless (ref $hash) {
  149. local $Exporter::ExportLevel = 1;
  150. import Getopt::Std;
  151. }
  152. $errs == 0;
  153. }
  154. 1;