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.

559 lines
18 KiB

  1. #############################################################################
  2. # Pod/Usage.pm -- print usage messages for the running script.
  3. #
  4. # Copyright (C) 1996-2000 by Bradford Appleton. All rights reserved.
  5. # This file is part of "PodParser". PodParser is free software;
  6. # you can redistribute it and/or modify it under the same terms
  7. # as Perl itself.
  8. #############################################################################
  9. package Pod::Usage;
  10. use vars qw($VERSION);
  11. $VERSION = 1.14; ## Current version of this package
  12. require 5.005; ## requires this Perl version or later
  13. =head1 NAME
  14. Pod::Usage, pod2usage() - print a usage message from embedded pod documentation
  15. =head1 SYNOPSIS
  16. use Pod::Usage
  17. my $message_text = "This text precedes the usage message.";
  18. my $exit_status = 2; ## The exit status to use
  19. my $verbose_level = 0; ## The verbose level to use
  20. my $filehandle = \*STDERR; ## The filehandle to write to
  21. pod2usage($message_text);
  22. pod2usage($exit_status);
  23. pod2usage( { -message => $message_text ,
  24. -exitval => $exit_status ,
  25. -verbose => $verbose_level,
  26. -output => $filehandle } );
  27. pod2usage( -msg => $message_text ,
  28. -exitval => $exit_status ,
  29. -verbose => $verbose_level,
  30. -output => $filehandle );
  31. =head1 ARGUMENTS
  32. B<pod2usage> should be given either a single argument, or a list of
  33. arguments corresponding to an associative array (a "hash"). When a single
  34. argument is given, it should correspond to exactly one of the following:
  35. =over 4
  36. =item *
  37. A string containing the text of a message to print I<before> printing
  38. the usage message
  39. =item *
  40. A numeric value corresponding to the desired exit status
  41. =item *
  42. A reference to a hash
  43. =back
  44. If more than one argument is given then the entire argument list is
  45. assumed to be a hash. If a hash is supplied (either as a reference or
  46. as a list) it should contain one or more elements with the following
  47. keys:
  48. =over 4
  49. =item C<-message>
  50. =item C<-msg>
  51. The text of a message to print immediately prior to printing the
  52. program's usage message.
  53. =item C<-exitval>
  54. The desired exit status to pass to the B<exit()> function.
  55. This should be an integer, or else the string "NOEXIT" to
  56. indicate that control should simply be returned without
  57. terminating the invoking process.
  58. =item C<-verbose>
  59. The desired level of "verboseness" to use when printing the usage
  60. message. If the corresponding value is 0, then only the "SYNOPSIS"
  61. section of the pod documentation is printed. If the corresponding value
  62. is 1, then the "SYNOPSIS" section, along with any section entitled
  63. "OPTIONS", "ARGUMENTS", or "OPTIONS AND ARGUMENTS" is printed. If the
  64. corresponding value is 2 or more then the entire manpage is printed.
  65. =item C<-output>
  66. A reference to a filehandle, or the pathname of a file to which the
  67. usage message should be written. The default is C<\*STDERR> unless the
  68. exit value is less than 2 (in which case the default is C<\*STDOUT>).
  69. =item C<-input>
  70. A reference to a filehandle, or the pathname of a file from which the
  71. invoking script's pod documentation should be read. It defaults to the
  72. file indicated by C<$0> (C<$PROGRAM_NAME> for users of F<English.pm>).
  73. =item C<-pathlist>
  74. A list of directory paths. If the input file does not exist, then it
  75. will be searched for in the given directory list (in the order the
  76. directories appear in the list). It defaults to the list of directories
  77. implied by C<$ENV{PATH}>. The list may be specified either by a reference
  78. to an array, or by a string of directory paths which use the same path
  79. separator as C<$ENV{PATH}> on your system (e.g., C<:> for Unix, C<;> for
  80. MSWin32 and DOS).
  81. =back
  82. =head1 DESCRIPTION
  83. B<pod2usage> will print a usage message for the invoking script (using
  84. its embedded pod documentation) and then exit the script with the
  85. desired exit status. The usage message printed may have any one of three
  86. levels of "verboseness": If the verbose level is 0, then only a synopsis
  87. is printed. If the verbose level is 1, then the synopsis is printed
  88. along with a description (if present) of the command line options and
  89. arguments. If the verbose level is 2, then the entire manual page is
  90. printed.
  91. Unless they are explicitly specified, the default values for the exit
  92. status, verbose level, and output stream to use are determined as
  93. follows:
  94. =over 4
  95. =item *
  96. If neither the exit status nor the verbose level is specified, then the
  97. default is to use an exit status of 2 with a verbose level of 0.
  98. =item *
  99. If an exit status I<is> specified but the verbose level is I<not>, then the
  100. verbose level will default to 1 if the exit status is less than 2 and
  101. will default to 0 otherwise.
  102. =item *
  103. If an exit status is I<not> specified but verbose level I<is> given, then
  104. the exit status will default to 2 if the verbose level is 0 and will
  105. default to 1 otherwise.
  106. =item *
  107. If the exit status used is less than 2, then output is printed on
  108. C<STDOUT>. Otherwise output is printed on C<STDERR>.
  109. =back
  110. Although the above may seem a bit confusing at first, it generally does
  111. "the right thing" in most situations. This determination of the default
  112. values to use is based upon the following typical Unix conventions:
  113. =over 4
  114. =item *
  115. An exit status of 0 implies "success". For example, B<diff(1)> exits
  116. with a status of 0 if the two files have the same contents.
  117. =item *
  118. An exit status of 1 implies possibly abnormal, but non-defective, program
  119. termination. For example, B<grep(1)> exits with a status of 1 if
  120. it did I<not> find a matching line for the given regular expression.
  121. =item *
  122. An exit status of 2 or more implies a fatal error. For example, B<ls(1)>
  123. exits with a status of 2 if you specify an illegal (unknown) option on
  124. the command line.
  125. =item *
  126. Usage messages issued as a result of bad command-line syntax should go
  127. to C<STDERR>. However, usage messages issued due to an explicit request
  128. to print usage (like specifying B<-help> on the command line) should go
  129. to C<STDOUT>, just in case the user wants to pipe the output to a pager
  130. (such as B<more(1)>).
  131. =item *
  132. If program usage has been explicitly requested by the user, it is often
  133. desireable to exit with a status of 1 (as opposed to 0) after issuing
  134. the user-requested usage message. It is also desireable to give a
  135. more verbose description of program usage in this case.
  136. =back
  137. B<pod2usage> doesn't force the above conventions upon you, but it will
  138. use them by default if you don't expressly tell it to do otherwise. The
  139. ability of B<pod2usage()> to accept a single number or a string makes it
  140. convenient to use as an innocent looking error message handling function:
  141. use Pod::Usage;
  142. use Getopt::Long;
  143. ## Parse options
  144. GetOptions("help", "man", "flag1") || pod2usage(2);
  145. pod2usage(1) if ($opt_help);
  146. pod2usage(-verbose => 2) if ($opt_man);
  147. ## Check for too many filenames
  148. pod2usage("$0: Too many files given.\n") if (@ARGV > 1);
  149. Some user's however may feel that the above "economy of expression" is
  150. not particularly readable nor consistent and may instead choose to do
  151. something more like the following:
  152. use Pod::Usage;
  153. use Getopt::Long;
  154. ## Parse options
  155. GetOptions("help", "man", "flag1") || pod2usage(-verbose => 0);
  156. pod2usage(-verbose => 1) if ($opt_help);
  157. pod2usage(-verbose => 2) if ($opt_man);
  158. ## Check for too many filenames
  159. pod2usage(-verbose => 2, -message => "$0: Too many files given.\n")
  160. if (@ARGV > 1);
  161. As with all things in Perl, I<there's more than one way to do it>, and
  162. B<pod2usage()> adheres to this philosophy. If you are interested in
  163. seeing a number of different ways to invoke B<pod2usage> (although by no
  164. means exhaustive), please refer to L<"EXAMPLES">.
  165. =head1 EXAMPLES
  166. Each of the following invocations of C<pod2usage()> will print just the
  167. "SYNOPSIS" section to C<STDERR> and will exit with a status of 2:
  168. pod2usage();
  169. pod2usage(2);
  170. pod2usage(-verbose => 0);
  171. pod2usage(-exitval => 2);
  172. pod2usage({-exitval => 2, -output => \*STDERR});
  173. pod2usage({-verbose => 0, -output => \*STDERR});
  174. pod2usage(-exitval => 2, -verbose => 0);
  175. pod2usage(-exitval => 2, -verbose => 0, -output => \*STDERR);
  176. Each of the following invocations of C<pod2usage()> will print a message
  177. of "Syntax error." (followed by a newline) to C<STDERR>, immediately
  178. followed by just the "SYNOPSIS" section (also printed to C<STDERR>) and
  179. will exit with a status of 2:
  180. pod2usage("Syntax error.");
  181. pod2usage(-message => "Syntax error.", -verbose => 0);
  182. pod2usage(-msg => "Syntax error.", -exitval => 2);
  183. pod2usage({-msg => "Syntax error.", -exitval => 2, -output => \*STDERR});
  184. pod2usage({-msg => "Syntax error.", -verbose => 0, -output => \*STDERR});
  185. pod2usage(-msg => "Syntax error.", -exitval => 2, -verbose => 0);
  186. pod2usage(-message => "Syntax error.",
  187. -exitval => 2,
  188. -verbose => 0,
  189. -output => \*STDERR);
  190. Each of the following invocations of C<pod2usage()> will print the
  191. "SYNOPSIS" section and any "OPTIONS" and/or "ARGUMENTS" sections to
  192. C<STDOUT> and will exit with a status of 1:
  193. pod2usage(1);
  194. pod2usage(-verbose => 1);
  195. pod2usage(-exitval => 1);
  196. pod2usage({-exitval => 1, -output => \*STDOUT});
  197. pod2usage({-verbose => 1, -output => \*STDOUT});
  198. pod2usage(-exitval => 1, -verbose => 1);
  199. pod2usage(-exitval => 1, -verbose => 1, -output => \*STDOUT});
  200. Each of the following invocations of C<pod2usage()> will print the
  201. entire manual page to C<STDOUT> and will exit with a status of 1:
  202. pod2usage(-verbose => 2);
  203. pod2usage({-verbose => 2, -output => \*STDOUT});
  204. pod2usage(-exitval => 1, -verbose => 2);
  205. pod2usage({-exitval => 1, -verbose => 2, -output => \*STDOUT});
  206. =head2 Recommended Use
  207. Most scripts should print some type of usage message to C<STDERR> when a
  208. command line syntax error is detected. They should also provide an
  209. option (usually C<-H> or C<-help>) to print a (possibly more verbose)
  210. usage message to C<STDOUT>. Some scripts may even wish to go so far as to
  211. provide a means of printing their complete documentation to C<STDOUT>
  212. (perhaps by allowing a C<-man> option). The following complete example
  213. uses B<Pod::Usage> in combination with B<Getopt::Long> to do all of these
  214. things:
  215. use Getopt::Long;
  216. use Pod::Usage;
  217. my $man = 0;
  218. my $help = 0;
  219. ## Parse options and print usage if there is a syntax error,
  220. ## or if usage was explicitly requested.
  221. GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
  222. pod2usage(1) if $help;
  223. pod2usage(-verbose => 2) if $man;
  224. ## If no arguments were given, then allow STDIN to be used only
  225. ## if it's not connected to a terminal (otherwise print usage)
  226. pod2usage("$0: No files given.") if ((@ARGV == 0) && (-t STDIN));
  227. __END__
  228. =head1 NAME
  229. sample - Using GetOpt::Long and Pod::Usage
  230. =head1 SYNOPSIS
  231. sample [options] [file ...]
  232. Options:
  233. -help brief help message
  234. -man full documentation
  235. =head1 OPTIONS
  236. =over 8
  237. =item B<-help>
  238. Print a brief help message and exits.
  239. =item B<-man>
  240. Prints the manual page and exits.
  241. =back
  242. =head1 DESCRIPTION
  243. B<This program> will read the given input file(s) and do something
  244. useful with the contents thereof.
  245. =cut
  246. =head1 CAVEATS
  247. By default, B<pod2usage()> will use C<$0> as the path to the pod input
  248. file. Unfortunately, not all systems on which Perl runs will set C<$0>
  249. properly (although if C<$0> isn't found, B<pod2usage()> will search
  250. C<$ENV{PATH}> or else the list specified by the C<-pathlist> option).
  251. If this is the case for your system, you may need to explicitly specify
  252. the path to the pod docs for the invoking script using something
  253. similar to the following:
  254. pod2usage(-exitval => 2, -input => "/path/to/your/pod/docs");
  255. =head1 AUTHOR
  256. Brad Appleton E<lt>bradapp@enteract.comE<gt>
  257. Based on code for B<Pod::Text::pod2text()> written by
  258. Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>
  259. =head1 ACKNOWLEDGEMENTS
  260. Steven McDougall E<lt>swmcd@world.std.comE<gt> for his help and patience
  261. with re-writing this manpage.
  262. =cut
  263. #############################################################################
  264. use strict;
  265. #use diagnostics;
  266. use Carp;
  267. use Config;
  268. use Exporter;
  269. use File::Spec;
  270. use vars qw(@ISA @EXPORT);
  271. @EXPORT = qw(&pod2usage);
  272. BEGIN {
  273. if ( $] >= 5.005_58 ) {
  274. require Pod::Text;
  275. @ISA = qw( Pod::Text );
  276. }
  277. else {
  278. require Pod::PlainText;
  279. @ISA = qw( Pod::PlainText );
  280. }
  281. }
  282. ##---------------------------------------------------------------------------
  283. ##---------------------------------
  284. ## Function definitions begin here
  285. ##---------------------------------
  286. sub pod2usage {
  287. local($_) = shift || "";
  288. my %opts;
  289. ## Collect arguments
  290. if (@_ > 0) {
  291. ## Too many arguments - assume that this is a hash and
  292. ## the user forgot to pass a reference to it.
  293. %opts = ($_, @_);
  294. }
  295. elsif (ref $_) {
  296. ## User passed a ref to a hash
  297. %opts = %{$_} if (ref($_) eq 'HASH');
  298. }
  299. elsif (/^[-+]?\d+$/) {
  300. ## User passed in the exit value to use
  301. $opts{"-exitval"} = $_;
  302. }
  303. else {
  304. ## User passed in a message to print before issuing usage.
  305. $_ and $opts{"-message"} = $_;
  306. }
  307. ## Need this for backward compatibility since we formerly used
  308. ## options that were all uppercase words rather than ones that
  309. ## looked like Unix command-line options.
  310. ## to be uppercase keywords)
  311. %opts = map {
  312. my $val = $opts{$_};
  313. s/^(?=\w)/-/;
  314. /^-msg/i and $_ = '-message';
  315. /^-exit/i and $_ = '-exitval';
  316. lc($_) => $val;
  317. } (keys %opts);
  318. ## Now determine default -exitval and -verbose values to use
  319. if ((! defined $opts{"-exitval"}) && (! defined $opts{"-verbose"})) {
  320. $opts{"-exitval"} = 2;
  321. $opts{"-verbose"} = 0;
  322. }
  323. elsif (! defined $opts{"-exitval"}) {
  324. $opts{"-exitval"} = ($opts{"-verbose"} > 0) ? 1 : 2;
  325. }
  326. elsif (! defined $opts{"-verbose"}) {
  327. $opts{"-verbose"} = ($opts{"-exitval"} < 2);
  328. }
  329. ## Default the output file
  330. $opts{"-output"} = ($opts{"-exitval"} < 2) ? \*STDOUT : \*STDERR
  331. unless (defined $opts{"-output"});
  332. ## Default the input file
  333. $opts{"-input"} = $0 unless (defined $opts{"-input"});
  334. ## Look up input file in path if it doesnt exist.
  335. unless ((ref $opts{"-input"}) || (-e $opts{"-input"})) {
  336. my ($dirname, $basename) = ('', $opts{"-input"});
  337. my $pathsep = ($^O =~ /^(?:dos|os2|MSWin32)$/) ? ";"
  338. : (($^O eq 'MacOS') ? ',' : ":");
  339. my $pathspec = $opts{"-pathlist"} || $ENV{PATH} || $ENV{PERL5LIB};
  340. my @paths = (ref $pathspec) ? @$pathspec : split($pathsep, $pathspec);
  341. for $dirname (@paths) {
  342. $_ = File::Spec->catfile($dirname, $basename) if length;
  343. last if (-e $_) && ($opts{"-input"} = $_);
  344. }
  345. }
  346. ## Now create a pod reader and constrain it to the desired sections.
  347. my $parser = new Pod::Usage(USAGE_OPTIONS => \%opts);
  348. if ($opts{"-verbose"} == 0) {
  349. $parser->select("SYNOPSIS");
  350. }
  351. elsif ($opts{"-verbose"} == 1) {
  352. my $opt_re = '(?i)' .
  353. '(?:OPTIONS|ARGUMENTS)' .
  354. '(?:\s*(?:AND|\/)\s*(?:OPTIONS|ARGUMENTS))?';
  355. $parser->select( 'SYNOPSIS', $opt_re, "DESCRIPTION/$opt_re" );
  356. }
  357. ## Now translate the pod document and then exit with the desired status
  358. if ( $opts{"-verbose"} >= 2
  359. and !ref($opts{"-input"})
  360. and $opts{"-output"} == \*STDOUT )
  361. {
  362. ## spit out the entire PODs. Might as well invoke perldoc
  363. my $progpath = File::Spec->catfile($Config{bin}, "perldoc");
  364. system($progpath, $opts{"-input"});
  365. }
  366. else {
  367. $parser->parse_from_file($opts{"-input"}, $opts{"-output"});
  368. }
  369. exit($opts{"-exitval"}) unless (lc($opts{"-exitval"}) eq 'noexit');
  370. }
  371. ##---------------------------------------------------------------------------
  372. ##-------------------------------
  373. ## Method definitions begin here
  374. ##-------------------------------
  375. sub new {
  376. my $this = shift;
  377. my $class = ref($this) || $this;
  378. my %params = @_;
  379. my $self = {%params};
  380. bless $self, $class;
  381. $self->initialize();
  382. return $self;
  383. }
  384. sub begin_pod {
  385. my $self = shift;
  386. $self->SUPER::begin_pod(); ## Have to call superclass
  387. my $msg = $self->{USAGE_OPTIONS}->{-message} or return 1;
  388. my $out_fh = $self->output_handle();
  389. print $out_fh "$msg\n";
  390. }
  391. sub preprocess_paragraph {
  392. my $self = shift;
  393. local $_ = shift;
  394. my $line = shift;
  395. ## See if this is a heading and we arent printing the entire manpage.
  396. if (($self->{USAGE_OPTIONS}->{-verbose} < 2) && /^=head/) {
  397. ## Change the title of the SYNOPSIS section to USAGE
  398. s/^=head1\s+SYNOPSIS\s*$/=head1 USAGE/;
  399. ## Try to do some lowercasing instead of all-caps in headings
  400. s{([A-Z])([A-Z]+)}{((length($2) > 2) ? $1 : lc($1)) . lc($2)}ge;
  401. ## Use a colon to end all headings
  402. s/\s*$/:/ unless (/:\s*$/);
  403. $_ .= "\n";
  404. }
  405. return $self->SUPER::preprocess_paragraph($_);
  406. }