1Pod::Usage(3) User Contributed Perl Documentation Pod::Usage(3)
2
3
4
6 Pod::Usage, pod2usage() - print a usage message from embedded pod
7 documentation
8
10 use Pod::Usage
11
12 my $message_text = "This text precedes the usage message.";
13 my $exit_status = 2; ## The exit status to use
14 my $verbose_level = 0; ## The verbose level to use
15 my $filehandle = \*STDERR; ## The filehandle to write to
16
17 pod2usage($message_text);
18
19 pod2usage($exit_status);
20
21 pod2usage( { -message => $message_text ,
22 -exitval => $exit_status ,
23 -verbose => $verbose_level,
24 -output => $filehandle } );
25
26 pod2usage( -msg => $message_text ,
27 -exitval => $exit_status ,
28 -verbose => $verbose_level,
29 -output => $filehandle );
30
31 pod2usage( -verbose => 2,
32 -noperldoc => 1 )
33
35 pod2usage should be given either a single argument, or a list of
36 arguments corresponding to an associative array (a "hash"). When a
37 single argument is given, it should correspond to exactly one of the
38 following:
39
40 · A string containing the text of a message to print before printing
41 the usage message
42
43 · A numeric value corresponding to the desired exit status
44
45 · A reference to a hash
46
47 If more than one argument is given then the entire argument list is
48 assumed to be a hash. If a hash is supplied (either as a reference or
49 as a list) it should contain one or more elements with the following
50 keys:
51
52 "-message"
53 "-msg"
54 The text of a message to print immediately prior to printing the
55 program's usage message.
56
57 "-exitval"
58 The desired exit status to pass to the exit() function. This
59 should be an integer, or else the string "NOEXIT" to indicate that
60 control should simply be returned without terminating the invoking
61 process.
62
63 "-verbose"
64 The desired level of "verboseness" to use when printing the usage
65 message. If the corresponding value is 0, then only the "SYNOPSIS"
66 section of the pod documentation is printed. If the corresponding
67 value is 1, then the "SYNOPSIS" section, along with any section
68 entitled "OPTIONS", "ARGUMENTS", or "OPTIONS AND ARGUMENTS" is
69 printed. If the corresponding value is 2 or more then the entire
70 manpage is printed.
71
72 The special verbosity level 99 requires to also specify the
73 -sections parameter; then these sections are extracted and printed.
74
75 "-sections"
76 A string representing a selection list for sections to be printed
77 when -verbose is set to 99, e.g.
78 "NAME|SYNOPSIS|DESCRIPTION|VERSION".
79
80 Alternatively, an array reference of section specifications can be
81 used:
82
83 pod2usage(-verbose => 99,
84 -sections => [ qw(fred fred/subsection) ] );
85
86 "-output"
87 A reference to a filehandle, or the pathname of a file to which the
88 usage message should be written. The default is "\*STDERR" unless
89 the exit value is less than 2 (in which case the default is
90 "\*STDOUT").
91
92 "-input"
93 A reference to a filehandle, or the pathname of a file from which
94 the invoking script's pod documentation should be read. It
95 defaults to the file indicated by $0 ($PROGRAM_NAME for users of
96 English.pm).
97
98 If you are calling pod2usage() from a module and want to display
99 that module's POD, you can use this:
100
101 use Pod::Find qw(pod_where);
102 pod2usage( -input => pod_where({-inc => 1}, __PACKAGE__) );
103
104 "-pathlist"
105 A list of directory paths. If the input file does not exist, then
106 it will be searched for in the given directory list (in the order
107 the directories appear in the list). It defaults to the list of
108 directories implied by $ENV{PATH}. The list may be specified either
109 by a reference to an array, or by a string of directory paths which
110 use the same path separator as $ENV{PATH} on your system (e.g., ":"
111 for Unix, ";" for MSWin32 and DOS).
112
113 "-noperldoc"
114 By default, Pod::Usage will call perldoc when -verbose >= 2 is
115 specified. This does not work well e.g. if the script was packed
116 with PAR. The -noperldoc option suppresses the external call to
117 perldoc and uses the simple text formatter (Pod::Text) to output
118 the POD.
119
120 Formatting base class
121 The default text formatter is Pod::Text. The base class for Pod::Usage
122 can be defined by pre-setting $Pod::Usage::Formatter before loading
123 Pod::Usage, e.g.:
124
125 BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
126 use Pod::Usage qw(pod2usage);
127
128 Pass-through options
129 The following options are passed through to the underlying text
130 formatter. See the manual pages of these modules for more information.
131
132 alt code indent loose margin quotes sentence stderr utf8 width
133
135 pod2usage will print a usage message for the invoking script (using its
136 embedded pod documentation) and then exit the script with the desired
137 exit status. The usage message printed may have any one of three levels
138 of "verboseness": If the verbose level is 0, then only a synopsis is
139 printed. If the verbose level is 1, then the synopsis is printed along
140 with a description (if present) of the command line options and
141 arguments. If the verbose level is 2, then the entire manual page is
142 printed.
143
144 Unless they are explicitly specified, the default values for the exit
145 status, verbose level, and output stream to use are determined as
146 follows:
147
148 · If neither the exit status nor the verbose level is specified, then
149 the default is to use an exit status of 2 with a verbose level of
150 0.
151
152 · If an exit status is specified but the verbose level is not, then
153 the verbose level will default to 1 if the exit status is less than
154 2 and will default to 0 otherwise.
155
156 · If an exit status is not specified but verbose level is given, then
157 the exit status will default to 2 if the verbose level is 0 and
158 will default to 1 otherwise.
159
160 · If the exit status used is less than 2, then output is printed on
161 "STDOUT". Otherwise output is printed on "STDERR".
162
163 Although the above may seem a bit confusing at first, it generally does
164 "the right thing" in most situations. This determination of the
165 default values to use is based upon the following typical Unix
166 conventions:
167
168 · An exit status of 0 implies "success". For example, diff(1) exits
169 with a status of 0 if the two files have the same contents.
170
171 · An exit status of 1 implies possibly abnormal, but non-defective,
172 program termination. For example, grep(1) exits with a status of 1
173 if it did not find a matching line for the given regular
174 expression.
175
176 · An exit status of 2 or more implies a fatal error. For example,
177 ls(1) exits with a status of 2 if you specify an illegal (unknown)
178 option on the command line.
179
180 · Usage messages issued as a result of bad command-line syntax should
181 go to "STDERR". However, usage messages issued due to an explicit
182 request to print usage (like specifying -help on the command line)
183 should go to "STDOUT", just in case the user wants to pipe the
184 output to a pager (such as more(1)).
185
186 · If program usage has been explicitly requested by the user, it is
187 often desirable to exit with a status of 1 (as opposed to 0) after
188 issuing the user-requested usage message. It is also desirable to
189 give a more verbose description of program usage in this case.
190
191 pod2usage doesn't force the above conventions upon you, but it will use
192 them by default if you don't expressly tell it to do otherwise. The
193 ability of pod2usage() to accept a single number or a string makes it
194 convenient to use as an innocent looking error message handling
195 function:
196
197 use Pod::Usage;
198 use Getopt::Long;
199
200 ## Parse options
201 GetOptions("help", "man", "flag1") || pod2usage(2);
202 pod2usage(1) if ($opt_help);
203 pod2usage(-verbose => 2) if ($opt_man);
204
205 ## Check for too many filenames
206 pod2usage("$0: Too many files given.\n") if (@ARGV > 1);
207
208 Some user's however may feel that the above "economy of expression" is
209 not particularly readable nor consistent and may instead choose to do
210 something more like the following:
211
212 use Pod::Usage;
213 use Getopt::Long;
214
215 ## Parse options
216 GetOptions("help", "man", "flag1") || pod2usage(-verbose => 0);
217 pod2usage(-verbose => 1) if ($opt_help);
218 pod2usage(-verbose => 2) if ($opt_man);
219
220 ## Check for too many filenames
221 pod2usage(-verbose => 2, -message => "$0: Too many files given.\n")
222 if (@ARGV > 1);
223
224 As with all things in Perl, there's more than one way to do it, and
225 pod2usage() adheres to this philosophy. If you are interested in
226 seeing a number of different ways to invoke pod2usage (although by no
227 means exhaustive), please refer to "EXAMPLES".
228
230 Each of the following invocations of "pod2usage()" will print just the
231 "SYNOPSIS" section to "STDERR" and will exit with a status of 2:
232
233 pod2usage();
234
235 pod2usage(2);
236
237 pod2usage(-verbose => 0);
238
239 pod2usage(-exitval => 2);
240
241 pod2usage({-exitval => 2, -output => \*STDERR});
242
243 pod2usage({-verbose => 0, -output => \*STDERR});
244
245 pod2usage(-exitval => 2, -verbose => 0);
246
247 pod2usage(-exitval => 2, -verbose => 0, -output => \*STDERR);
248
249 Each of the following invocations of "pod2usage()" will print a message
250 of "Syntax error." (followed by a newline) to "STDERR", immediately
251 followed by just the "SYNOPSIS" section (also printed to "STDERR") and
252 will exit with a status of 2:
253
254 pod2usage("Syntax error.");
255
256 pod2usage(-message => "Syntax error.", -verbose => 0);
257
258 pod2usage(-msg => "Syntax error.", -exitval => 2);
259
260 pod2usage({-msg => "Syntax error.", -exitval => 2, -output => \*STDERR});
261
262 pod2usage({-msg => "Syntax error.", -verbose => 0, -output => \*STDERR});
263
264 pod2usage(-msg => "Syntax error.", -exitval => 2, -verbose => 0);
265
266 pod2usage(-message => "Syntax error.",
267 -exitval => 2,
268 -verbose => 0,
269 -output => \*STDERR);
270
271 Each of the following invocations of "pod2usage()" will print the
272 "SYNOPSIS" section and any "OPTIONS" and/or "ARGUMENTS" sections to
273 "STDOUT" and will exit with a status of 1:
274
275 pod2usage(1);
276
277 pod2usage(-verbose => 1);
278
279 pod2usage(-exitval => 1);
280
281 pod2usage({-exitval => 1, -output => \*STDOUT});
282
283 pod2usage({-verbose => 1, -output => \*STDOUT});
284
285 pod2usage(-exitval => 1, -verbose => 1);
286
287 pod2usage(-exitval => 1, -verbose => 1, -output => \*STDOUT});
288
289 Each of the following invocations of "pod2usage()" will print the
290 entire manual page to "STDOUT" and will exit with a status of 1:
291
292 pod2usage(-verbose => 2);
293
294 pod2usage({-verbose => 2, -output => \*STDOUT});
295
296 pod2usage(-exitval => 1, -verbose => 2);
297
298 pod2usage({-exitval => 1, -verbose => 2, -output => \*STDOUT});
299
300 Recommended Use
301 Most scripts should print some type of usage message to "STDERR" when a
302 command line syntax error is detected. They should also provide an
303 option (usually "-H" or "-help") to print a (possibly more verbose)
304 usage message to "STDOUT". Some scripts may even wish to go so far as
305 to provide a means of printing their complete documentation to "STDOUT"
306 (perhaps by allowing a "-man" option). The following complete example
307 uses Pod::Usage in combination with Getopt::Long to do all of these
308 things:
309
310 use Getopt::Long;
311 use Pod::Usage;
312
313 my $man = 0;
314 my $help = 0;
315 ## Parse options and print usage if there is a syntax error,
316 ## or if usage was explicitly requested.
317 GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
318 pod2usage(1) if $help;
319 pod2usage(-verbose => 2) if $man;
320
321 ## If no arguments were given, then allow STDIN to be used only
322 ## if it's not connected to a terminal (otherwise print usage)
323 pod2usage("$0: No files given.") if ((@ARGV == 0) && (-t STDIN));
324 __END__
325
326 =head1 NAME
327
328 sample - Using GetOpt::Long and Pod::Usage
329
330 =head1 SYNOPSIS
331
332 sample [options] [file ...]
333
334 Options:
335 -help brief help message
336 -man full documentation
337
338 =head1 OPTIONS
339
340 =over 8
341
342 =item B<-help>
343
344 Print a brief help message and exits.
345
346 =item B<-man>
347
348 Prints the manual page and exits.
349
350 =back
351
352 =head1 DESCRIPTION
353
354 B<This program> will read the given input file(s) and do something
355 useful with the contents thereof.
356
357 =cut
358
360 By default, pod2usage() will use $0 as the path to the pod input file.
361 Unfortunately, not all systems on which Perl runs will set $0 properly
362 (although if $0 isn't found, pod2usage() will search $ENV{PATH} or else
363 the list specified by the "-pathlist" option). If this is the case for
364 your system, you may need to explicitly specify the path to the pod
365 docs for the invoking script using something similar to the following:
366
367 pod2usage(-exitval => 2, -input => "/path/to/your/pod/docs");
368
369 In the pathological case that a script is called via a relative path
370 and the script itself changes the current working directory (see
371 "chdir" in perlfunc) before calling pod2usage, Pod::Usage will fail
372 even on robust platforms. Don't do that. Or use FindBin to locate the
373 script:
374
375 use FindBin;
376 pod2usage(-input => $FindBin::Bin . "/" . $FindBin::Script);
377
379 Please report bugs using <http://rt.cpan.org>.
380
381 Marek Rouchal <marekr@cpan.org>
382
383 Brad Appleton <bradapp@enteract.com>
384
385 Based on code for Pod::Text::pod2text() written by Tom Christiansen
386 <tchrist@mox.perl.com>
387
389 rjbs for refactoring Pod::Usage to not use Pod::Parser any more.
390
391 Steven McDougall <swmcd@world.std.com> for his help and patience with
392 re-writing this manpage.
393
395 Pod::Usage is now a standalone distribution, depending on Pod::Text
396 which in turn depends on Pod::Simple.
397
398 Pod::Perldoc, Getopt::Long, Pod::Find, FindBin, Pod::Text,
399 Pod::Text::Termcap, Pod::Simple
400
401
402
403perl v5.16.3 2013-06-02 Pod::Usage(3)