Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / Getopt::Long.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Getopt::Long 3"
132.TH Getopt::Long 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134Getopt::Long \- Extended processing of command line options
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 7
138\& use Getopt::Long;
139\& my $data = "file.dat";
140\& my $length = 24;
141\& my $verbose;
142\& $result = GetOptions ("length=i" => \e$length, # numeric
143\& "file=s" => \e$data, # string
144\& "verbose" => \e$verbose); # flag
145.Ve
146.SH "DESCRIPTION"
147.IX Header "DESCRIPTION"
148The Getopt::Long module implements an extended getopt function called
149\&\fIGetOptions()\fR. This function adheres to the \s-1POSIX\s0 syntax for command
150line options, with \s-1GNU\s0 extensions. In general, this means that options
151have long names instead of single letters, and are introduced with a
152double dash \*(L"\-\-\*(R". Support for bundling of command line options, as was
153the case with the more traditional single-letter approach, is provided
154but not enabled by default.
155.SH "Command Line Options, an Introduction"
156.IX Header "Command Line Options, an Introduction"
157Command line operated programs traditionally take their arguments from
158the command line, for example filenames or other information that the
159program needs to know. Besides arguments, these programs often take
160command line \fIoptions\fR as well. Options are not necessary for the
161program to work, hence the name 'option', but are used to modify its
162default behaviour. For example, a program could do its job quietly,
163but with a suitable option it could provide verbose information about
164what it did.
165.PP
166Command line options come in several flavours. Historically, they are
167preceded by a single dash \f(CW\*(C`\-\*(C'\fR, and consist of a single letter.
168.PP
169.Vb 1
170\& -l -a -c
171.Ve
172.PP
173Usually, these single-character options can be bundled:
174.PP
175.Vb 1
176\& -lac
177.Ve
178.PP
179Options can have values, the value is placed after the option
180character. Sometimes with whitespace in between, sometimes not:
181.PP
182.Vb 1
183\& -s 24 -s24
184.Ve
185.PP
186Due to the very cryptic nature of these options, another style was
187developed that used long names. So instead of a cryptic \f(CW\*(C`\-l\*(C'\fR one
188could use the more descriptive \f(CW\*(C`\-\-long\*(C'\fR. To distinguish between a
189bundle of single-character options and a long one, two dashes are used
190to precede the option name. Early implementations of long options used
191a plus \f(CW\*(C`+\*(C'\fR instead. Also, option values could be specified either
192like
193.PP
194.Vb 1
195\& --size=24
196.Ve
197.PP
198or
199.PP
200.Vb 1
201\& --size 24
202.Ve
203.PP
204The \f(CW\*(C`+\*(C'\fR form is now obsolete and strongly deprecated.
205.SH "Getting Started with Getopt::Long"
206.IX Header "Getting Started with Getopt::Long"
207Getopt::Long is the Perl5 successor of \f(CW\*(C`newgetopt.pl\*(C'\fR. This was the
208first Perl module that provided support for handling the new style of
209command line options, hence the name Getopt::Long. This module also
210supports single-character options and bundling. Single character
211options may be any alphabetic character, a question mark, and a dash.
212Long options may consist of a series of letters, digits, and dashes.
213Although this is currently not enforced by Getopt::Long, multiple
214consecutive dashes are not allowed, and the option name must not end
215with a dash.
216.PP
217To use Getopt::Long from a Perl program, you must include the
218following line in your Perl program:
219.PP
220.Vb 1
221\& use Getopt::Long;
222.Ve
223.PP
224This will load the core of the Getopt::Long module and prepare your
225program for using it. Most of the actual Getopt::Long code is not
226loaded until you really call one of its functions.
227.PP
228In the default configuration, options names may be abbreviated to
229uniqueness, case does not matter, and a single dash is sufficient,
230even for long option names. Also, options may be placed between
231non-option arguments. See \*(L"Configuring Getopt::Long\*(R" for more
232details on how to configure Getopt::Long.
233.Sh "Simple options"
234.IX Subsection "Simple options"
235The most simple options are the ones that take no values. Their mere
236presence on the command line enables the option. Popular examples are:
237.PP
238.Vb 1
239\& --all --verbose --quiet --debug
240.Ve
241.PP
242Handling simple options is straightforward:
243.PP
244.Vb 3
245\& my $verbose = ''; # option variable with default value (false)
246\& my $all = ''; # option variable with default value (false)
247\& GetOptions ('verbose' => \e$verbose, 'all' => \e$all);
248.Ve
249.PP
250The call to \fIGetOptions()\fR parses the command line arguments that are
251present in \f(CW@ARGV\fR and sets the option variable to the value \f(CW1\fR if
252the option did occur on the command line. Otherwise, the option
253variable is not touched. Setting the option value to true is often
254called \fIenabling\fR the option.
255.PP
256The option name as specified to the \fIGetOptions()\fR function is called
257the option \fIspecification\fR. Later we'll see that this specification
258can contain more than just the option name. The reference to the
259variable is called the option \fIdestination\fR.
260.PP
261\&\fIGetOptions()\fR will return a true value if the command line could be
262processed successfully. Otherwise, it will write error messages to
263\&\s-1STDERR\s0, and return a false result.
264.Sh "A little bit less simple options"
265.IX Subsection "A little bit less simple options"
266Getopt::Long supports two useful variants of simple options:
267\&\fInegatable\fR options and \fIincremental\fR options.
268.PP
269A negatable option is specified with an exclamation mark \f(CW\*(C`!\*(C'\fR after the
270option name:
271.PP
272.Vb 2
273\& my $verbose = ''; # option variable with default value (false)
274\& GetOptions ('verbose!' => \e$verbose);
275.Ve
276.PP
277Now, using \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will enable \f(CW$verbose\fR,
278as expected. But it is also allowed to use \f(CW\*(C`\-\-noverbose\*(C'\fR, which will
279disable \f(CW$verbose\fR by setting its value to \f(CW0\fR. Using a suitable
280default value, the program can find out whether \f(CW$verbose\fR is false
281by default, or disabled by using \f(CW\*(C`\-\-noverbose\*(C'\fR.
282.PP
283An incremental option is specified with a plus \f(CW\*(C`+\*(C'\fR after the
284option name:
285.PP
286.Vb 2
287\& my $verbose = ''; # option variable with default value (false)
288\& GetOptions ('verbose+' => \e$verbose);
289.Ve
290.PP
291Using \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will increment the value of
292\&\f(CW$verbose\fR. This way the program can keep track of how many times the
293option occurred on the command line. For example, each occurrence of
294\&\f(CW\*(C`\-\-verbose\*(C'\fR could increase the verbosity level of the program.
295.Sh "Mixing command line option with other arguments"
296.IX Subsection "Mixing command line option with other arguments"
297Usually programs take command line options as well as other arguments,
298for example, file names. It is good practice to always specify the
299options first, and the other arguments last. Getopt::Long will,
300however, allow the options and arguments to be mixed and 'filter out'
301all the options before passing the rest of the arguments to the
302program. To stop Getopt::Long from processing further arguments,
303insert a double dash \f(CW\*(C`\-\-\*(C'\fR on the command line:
304.PP
305.Vb 1
306\& --size 24 -- --all
307.Ve
308.PP
309In this example, \f(CW\*(C`\-\-all\*(C'\fR will \fInot\fR be treated as an option, but
310passed to the program unharmed, in \f(CW@ARGV\fR.
311.Sh "Options with values"
312.IX Subsection "Options with values"
313For options that take values it must be specified whether the option
314value is required or not, and what kind of value the option expects.
315.PP
316Three kinds of values are supported: integer numbers, floating point
317numbers, and strings.
318.PP
319If the option value is required, Getopt::Long will take the
320command line argument that follows the option and assign this to the
321option variable. If, however, the option value is specified as
322optional, this will only be done if that value does not look like a
323valid command line option itself.
324.PP
325.Vb 2
326\& my $tag = ''; # option variable with default value
327\& GetOptions ('tag=s' => \e$tag);
328.Ve
329.PP
330In the option specification, the option name is followed by an equals
331sign \f(CW\*(C`=\*(C'\fR and the letter \f(CW\*(C`s\*(C'\fR. The equals sign indicates that this
332option requires a value. The letter \f(CW\*(C`s\*(C'\fR indicates that this value is
333an arbitrary string. Other possible value types are \f(CW\*(C`i\*(C'\fR for integer
334values, and \f(CW\*(C`f\*(C'\fR for floating point values. Using a colon \f(CW\*(C`:\*(C'\fR instead
335of the equals sign indicates that the option value is optional. In
336this case, if no suitable value is supplied, string valued options get
337an empty string \f(CW''\fR assigned, while numeric options are set to \f(CW0\fR.
338.Sh "Options with multiple values"
339.IX Subsection "Options with multiple values"
340Options sometimes take several values. For example, a program could
341use multiple directories to search for library files:
342.PP
343.Vb 1
344\& --library lib/stdlib --library lib/extlib
345.Ve
346.PP
347To accomplish this behaviour, simply specify an array reference as the
348destination for the option:
349.PP
350.Vb 1
351\& GetOptions ("library=s" => \e@libfiles);
352.Ve
353.PP
354Alternatively, you can specify that the option can have multiple
355values by adding a \*(L"@\*(R", and pass a scalar reference as the
356destination:
357.PP
358.Vb 1
359\& GetOptions ("library=s@" => \e$libfiles);
360.Ve
361.PP
362Used with the example above, \f(CW@libfiles\fR (or \f(CW@$libfiles\fR) would
363contain two strings upon completion: \f(CW"lib/srdlib"\fR and
364\&\f(CW"lib/extlib"\fR, in that order. It is also possible to specify that
365only integer or floating point numbers are acceptable values.
366.PP
367Often it is useful to allow comma-separated lists of values as well as
368multiple occurrences of the options. This is easy using Perl's \fIsplit()\fR
369and \fIjoin()\fR operators:
370.PP
371.Vb 2
372\& GetOptions ("library=s" => \e@libfiles);
373\& @libfiles = split(/,/,join(',',@libfiles));
374.Ve
375.PP
376Of course, it is important to choose the right separator string for
377each purpose.
378.PP
379Warning: What follows is an experimental feature.
380.PP
381Options can take multiple values at once, for example
382.PP
383.Vb 1
384\& --coordinates 52.2 16.4 --rgbcolor 255 255 149
385.Ve
386.PP
387This can be accomplished by adding a repeat specifier to the option
388specification. Repeat specifiers are very similar to the \f(CW\*(C`{...}\*(C'\fR
389repeat specifiers that can be used with regular expression patterns.
390For example, the above command line would be handled as follows:
391.PP
392.Vb 1
393\& GetOptions('coordinates=f{2}' => \e@coor, 'rgbcolor=i{3}' => \e@color);
394.Ve
395.PP
396The destination for the option must be an array or array reference.
397.PP
398It is also possible to specify the minimal and maximal number of
399arguments an option takes. \f(CW\*(C`foo=s{2,4}\*(C'\fR indicates an option that
400takes at least two and at most 4 arguments. \f(CW\*(C`foo=s{,}\*(C'\fR indicates one
401or more values; \f(CW\*(C`foo:s{,}\*(C'\fR indicates zero or more option values.
402.Sh "Options with hash values"
403.IX Subsection "Options with hash values"
404If the option destination is a reference to a hash, the option will
405take, as value, strings of the form \fIkey\fR\f(CW\*(C`=\*(C'\fR\fIvalue\fR. The value will
406be stored with the specified key in the hash.
407.PP
408.Vb 1
409\& GetOptions ("define=s" => \e%defines);
410.Ve
411.PP
412Alternatively you can use:
413.PP
414.Vb 1
415\& GetOptions ("define=s%" => \e$defines);
416.Ve
417.PP
418When used with command line options:
419.PP
420.Vb 1
421\& --define os=linux --define vendor=redhat
422.Ve
423.PP
424the hash \f(CW%defines\fR (or \f(CW%$defines\fR) will contain two keys, \f(CW"os"\fR
425with value \f(CW\*(C`"linux\*(C'\fR and \f(CW"vendor"\fR with value \f(CW"redhat"\fR. It is
426also possible to specify that only integer or floating point numbers
427are acceptable values. The keys are always taken to be strings.
428.Sh "User-defined subroutines to handle options"
429.IX Subsection "User-defined subroutines to handle options"
430Ultimate control over what should be done when (actually: each time)
431an option is encountered on the command line can be achieved by
432designating a reference to a subroutine (or an anonymous subroutine)
433as the option destination. When \fIGetOptions()\fR encounters the option, it
434will call the subroutine with two or three arguments. The first
435argument is the name of the option. For a scalar or array destination,
436the second argument is the value to be stored. For a hash destination,
437the second arguments is the key to the hash, and the third argument
438the value to be stored. It is up to the subroutine to store the value,
439or do whatever it thinks is appropriate.
440.PP
441A trivial application of this mechanism is to implement options that
442are related to each other. For example:
443.PP
444.Vb 3
445\& my $verbose = ''; # option variable with default value (false)
446\& GetOptions ('verbose' => \e$verbose,
447\& 'quiet' => sub { $verbose = 0 });
448.Ve
449.PP
450Here \f(CW\*(C`\-\-verbose\*(C'\fR and \f(CW\*(C`\-\-quiet\*(C'\fR control the same variable
451\&\f(CW$verbose\fR, but with opposite values.
452.PP
453If the subroutine needs to signal an error, it should call \fIdie()\fR with
454the desired error message as its argument. \fIGetOptions()\fR will catch the
455\&\fIdie()\fR, issue the error message, and record that an error result must
456be returned upon completion.
457.PP
458If the text of the error message starts with an exclamation mark \f(CW\*(C`!\*(C'\fR
459it is interpreted specially by \fIGetOptions()\fR. There is currently one
460special command implemented: \f(CW\*(C`die("!FINISH")\*(C'\fR will cause \fIGetOptions()\fR
461to stop processing options, as if it encountered a double dash \f(CW\*(C`\-\-\*(C'\fR.
462.Sh "Options with multiple names"
463.IX Subsection "Options with multiple names"
464Often it is user friendly to supply alternate mnemonic names for
465options. For example \f(CW\*(C`\-\-height\*(C'\fR could be an alternate name for
466\&\f(CW\*(C`\-\-length\*(C'\fR. Alternate names can be included in the option
467specification, separated by vertical bar \f(CW\*(C`|\*(C'\fR characters. To implement
468the above example:
469.PP
470.Vb 1
471\& GetOptions ('length|height=f' => \e$length);
472.Ve
473.PP
474The first name is called the \fIprimary\fR name, the other names are
475called \fIaliases\fR. When using a hash to store options, the key will
476always be the primary name.
477.PP
478Multiple alternate names are possible.
479.Sh "Case and abbreviations"
480.IX Subsection "Case and abbreviations"
481Without additional configuration, \fIGetOptions()\fR will ignore the case of
482option names, and allow the options to be abbreviated to uniqueness.
483.PP
484.Vb 1
485\& GetOptions ('length|height=f' => \e$length, "head" => \e$head);
486.Ve
487.PP
488This call will allow \f(CW\*(C`\-\-l\*(C'\fR and \f(CW\*(C`\-\-L\*(C'\fR for the length option, but
489requires a least \f(CW\*(C`\-\-hea\*(C'\fR and \f(CW\*(C`\-\-hei\*(C'\fR for the head and height options.
490.Sh "Summary of Option Specifications"
491.IX Subsection "Summary of Option Specifications"
492Each option specifier consists of two parts: the name specification
493and the argument specification.
494.PP
495The name specification contains the name of the option, optionally
496followed by a list of alternative names separated by vertical bar
497characters.
498.PP
499.Vb 2
500\& length option name is "length"
501\& length|size|l name is "length", aliases are "size" and "l"
502.Ve
503.PP
504The argument specification is optional. If omitted, the option is
505considered boolean, a value of 1 will be assigned when the option is
506used on the command line.
507.PP
508The argument specification can be
509.IP "!" 4
510The option does not take an argument and may be negated by prefixing
511it with \*(L"no\*(R" or \*(L"no\-\*(R". E.g. \f(CW"foo!"\fR will allow \f(CW\*(C`\-\-foo\*(C'\fR (a value of
5121 will be assigned) as well as \f(CW\*(C`\-\-nofoo\*(C'\fR and \f(CW\*(C`\-\-no\-foo\*(C'\fR (a value of
5130 will be assigned). If the option has aliases, this applies to the
514aliases as well.
515.Sp
516Using negation on a single letter option when bundling is in effect is
517pointless and will result in a warning.
518.IP "+" 4
519The option does not take an argument and will be incremented by 1
520every time it appears on the command line. E.g. \f(CW"more+"\fR, when used
521with \f(CW\*(C`\-\-more \-\-more \-\-more\*(C'\fR, will increment the value three times,
522resulting in a value of 3 (provided it was 0 or undefined at first).
523.Sp
524The \f(CW\*(C`+\*(C'\fR specifier is ignored if the option destination is not a scalar.
525.IP "= \fItype\fR [ \fIdesttype\fR ] [ \fIrepeat\fR ]" 4
526.IX Item "= type [ desttype ] [ repeat ]"
527The option requires an argument of the given type. Supported types
528are:
529.RS 4
530.IP "s" 4
531.IX Item "s"
532String. An arbitrary sequence of characters. It is valid for the
533argument to start with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR.
534.IP "i" 4
535.IX Item "i"
536Integer. An optional leading plus or minus sign, followed by a
537sequence of digits.
538.IP "o" 4
539Extended integer, Perl style. This can be either an optional leading
540plus or minus sign, followed by a sequence of digits, or an octal
541string (a zero, optionally followed by '0', '1', .. '7'), or a
542hexadecimal string (\f(CW\*(C`0x\*(C'\fR followed by '0' .. '9', 'a' .. 'f', case
543insensitive), or a binary string (\f(CW\*(C`0b\*(C'\fR followed by a series of '0'
544and '1').
545.IP "f" 4
546.IX Item "f"
547Real number. For example \f(CW3.14\fR, \f(CW\*(C`\-6.23E24\*(C'\fR and so on.
548.RE
549.RS 4
550.Sp
551The \fIdesttype\fR can be \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR to specify that the option is
552list or a hash valued. This is only needed when the destination for
553the option value is not otherwise specified. It should be omitted when
554not needed.
555.Sp
556The \fIrepeat\fR specifies the number of values this option takes per
557occurrence on the command line. It has the format \f(CW\*(C`{\*(C'\fR [ \fImin\fR ] [ \f(CW\*(C`,\*(C'\fR [ \fImax\fR ] ] \f(CW\*(C`}\*(C'\fR.
558.Sp
559\&\fImin\fR denotes the minimal number of arguments. It defaults to 1 for
560options with \f(CW\*(C`=\*(C'\fR and to 0 for options with \f(CW\*(C`:\*(C'\fR, see below. Note that
561\&\fImin\fR overrules the \f(CW\*(C`=\*(C'\fR / \f(CW\*(C`:\*(C'\fR semantics.
562.Sp
563\&\fImax\fR denotes the maximum number of arguments. It must be at least
564\&\fImin\fR. If \fImax\fR is omitted, \fIbut the comma is not\fR, there is no
565upper bound to the number of argument values taken.
566.RE
567.IP ": \fItype\fR [ \fIdesttype\fR ]" 4
568.IX Item ": type [ desttype ]"
569Like \f(CW\*(C`=\*(C'\fR, but designates the argument as optional.
570If omitted, an empty string will be assigned to string values options,
571and the value zero to numeric options.
572.Sp
573Note that if a string argument starts with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR, it will be
574considered an option on itself.
575.IP ": \fInumber\fR [ \fIdesttype\fR ]" 4
576.IX Item ": number [ desttype ]"
577Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the \fInumber\fR will be assigned.
578.IP ": + [ \fIdesttype\fR ]" 4
579.IX Item ": + [ desttype ]"
580Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the current value for the
581option will be incremented.
582.SH "Advanced Possibilities"
583.IX Header "Advanced Possibilities"
584.Sh "Object oriented interface"
585.IX Subsection "Object oriented interface"
586Getopt::Long can be used in an object oriented way as well:
587.PP
588.Vb 4
589\& use Getopt::Long;
590\& $p = new Getopt::Long::Parser;
591\& $p->configure(...configuration options...);
592\& if ($p->getoptions(...options descriptions...)) ...
593.Ve
594.PP
595Configuration options can be passed to the constructor:
596.PP
597.Vb 2
598\& $p = new Getopt::Long::Parser
599\& config => [...configuration options...];
600.Ve
601.Sh "Thread Safety"
602.IX Subsection "Thread Safety"
603Getopt::Long is thread safe when using ithreads as of Perl 5.8. It is
604\&\fInot\fR thread safe when using the older (experimental and now
605obsolete) threads implementation that was added to Perl 5.005.
606.Sh "Documentation and help texts"
607.IX Subsection "Documentation and help texts"
608Getopt::Long encourages the use of Pod::Usage to produce help
609messages. For example:
610.PP
611.Vb 2
612\& use Getopt::Long;
613\& use Pod::Usage;
614.Ve
615.PP
616.Vb 2
617\& my $man = 0;
618\& my $help = 0;
619.Ve
620.PP
621.Vb 3
622\& GetOptions('help|?' => \e$help, man => \e$man) or pod2usage(2);
623\& pod2usage(1) if $help;
624\& pod2usage(-exitstatus => 0, -verbose => 2) if $man;
625.Ve
626.PP
627.Vb 1
628\& __END__
629.Ve
630.PP
631.Vb 1
632\& =head1 NAME
633.Ve
634.PP
635.Vb 1
636\& sample - Using Getopt::Long and Pod::Usage
637.Ve
638.PP
639.Vb 1
640\& =head1 SYNOPSIS
641.Ve
642.PP
643.Vb 1
644\& sample [options] [file ...]
645.Ve
646.PP
647.Vb 3
648\& Options:
649\& -help brief help message
650\& -man full documentation
651.Ve
652.PP
653.Vb 1
654\& =head1 OPTIONS
655.Ve
656.PP
657.Vb 1
658\& =over 8
659.Ve
660.PP
661.Vb 1
662\& =item B<-help>
663.Ve
664.PP
665.Vb 1
666\& Print a brief help message and exits.
667.Ve
668.PP
669.Vb 1
670\& =item B<-man>
671.Ve
672.PP
673.Vb 1
674\& Prints the manual page and exits.
675.Ve
676.PP
677.Vb 1
678\& =back
679.Ve
680.PP
681.Vb 1
682\& =head1 DESCRIPTION
683.Ve
684.PP
685.Vb 2
686\& B<This program> will read the given input file(s) and do something
687\& useful with the contents thereof.
688.Ve
689.PP
690.Vb 1
691\& =cut
692.Ve
693.PP
694See Pod::Usage for details.
695.Sh "Storing option values in a hash"
696.IX Subsection "Storing option values in a hash"
697Sometimes, for example when there are a lot of options, having a
698separate variable for each of them can be cumbersome. \fIGetOptions()\fR
699supports, as an alternative mechanism, storing options in a hash.
700.PP
701To obtain this, a reference to a hash must be passed \fIas the first
702argument\fR to \fIGetOptions()\fR. For each option that is specified on the
703command line, the option value will be stored in the hash with the
704option name as key. Options that are not actually used on the command
705line will not be put in the hash, on other words,
706\&\f(CW\*(C`exists($h{option})\*(C'\fR (or \fIdefined()\fR) can be used to test if an option
707was used. The drawback is that warnings will be issued if the program
708runs under \f(CW\*(C`use strict\*(C'\fR and uses \f(CW$h{option}\fR without testing with
709\&\fIexists()\fR or \fIdefined()\fR first.
710.PP
711.Vb 2
712\& my %h = ();
713\& GetOptions (\e%h, 'length=i'); # will store in $h{length}
714.Ve
715.PP
716For options that take list or hash values, it is necessary to indicate
717this by appending an \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR sign after the type:
718.PP
719.Vb 1
720\& GetOptions (\e%h, 'colours=s@'); # will push to @{$h{colours}}
721.Ve
722.PP
723To make things more complicated, the hash may contain references to
724the actual destinations, for example:
725.PP
726.Vb 3
727\& my $len = 0;
728\& my %h = ('length' => \e$len);
729\& GetOptions (\e%h, 'length=i'); # will store in $len
730.Ve
731.PP
732This example is fully equivalent with:
733.PP
734.Vb 2
735\& my $len = 0;
736\& GetOptions ('length=i' => \e$len); # will store in $len
737.Ve
738.PP
739Any mixture is possible. For example, the most frequently used options
740could be stored in variables while all other options get stored in the
741hash:
742.PP
743.Vb 6
744\& my $verbose = 0; # frequently referred
745\& my $debug = 0; # frequently referred
746\& my %h = ('verbose' => \e$verbose, 'debug' => \e$debug);
747\& GetOptions (\e%h, 'verbose', 'debug', 'filter', 'size=i');
748\& if ( $verbose ) { ... }
749\& if ( exists $h{filter} ) { ... option 'filter' was specified ... }
750.Ve
751.Sh "Bundling"
752.IX Subsection "Bundling"
753With bundling it is possible to set several single-character options
754at once. For example if \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR are all valid options,
755.PP
756.Vb 1
757\& -vax
758.Ve
759.PP
760would set all three.
761.PP
762Getopt::Long supports two levels of bundling. To enable bundling, a
763call to Getopt::Long::Configure is required.
764.PP
765The first level of bundling can be enabled with:
766.PP
767.Vb 1
768\& Getopt::Long::Configure ("bundling");
769.Ve
770.PP
771Configured this way, single-character options can be bundled but long
772options \fBmust\fR always start with a double dash \f(CW\*(C`\-\-\*(C'\fR to avoid
773ambiguity. For example, when \f(CW\*(C`vax\*(C'\fR, \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR are all valid
774options,
775.PP
776.Vb 1
777\& -vax
778.Ve
779.PP
780would set \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR, but
781.PP
782.Vb 1
783\& --vax
784.Ve
785.PP
786would set \f(CW\*(C`vax\*(C'\fR.
787.PP
788The second level of bundling lifts this restriction. It can be enabled
789with:
790.PP
791.Vb 1
792\& Getopt::Long::Configure ("bundling_override");
793.Ve
794.PP
795Now, \f(CW\*(C`\-vax\*(C'\fR would set the option \f(CW\*(C`vax\*(C'\fR.
796.PP
797When any level of bundling is enabled, option values may be inserted
798in the bundle. For example:
799.PP
800.Vb 1
801\& -h24w80
802.Ve
803.PP
804is equivalent to
805.PP
806.Vb 1
807\& -h 24 -w 80
808.Ve
809.PP
810When configured for bundling, single-character options are matched
811case sensitive while long options are matched case insensitive. To
812have the single-character options matched case insensitive as well,
813use:
814.PP
815.Vb 1
816\& Getopt::Long::Configure ("bundling", "ignorecase_always");
817.Ve
818.PP
819It goes without saying that bundling can be quite confusing.
820.Sh "The lonesome dash"
821.IX Subsection "The lonesome dash"
822Normally, a lone dash \f(CW\*(C`\-\*(C'\fR on the command line will not be considered
823an option. Option processing will terminate (unless \*(L"permute\*(R" is
824configured) and the dash will be left in \f(CW@ARGV\fR.
825.PP
826It is possible to get special treatment for a lone dash. This can be
827achieved by adding an option specification with an empty name, for
828example:
829.PP
830.Vb 1
831\& GetOptions ('' => \e$stdio);
832.Ve
833.PP
834A lone dash on the command line will now be a legal option, and using
835it will set variable \f(CW$stdio\fR.
836.Sh "Argument callback"
837.IX Subsection "Argument callback"
838A special option 'name' \f(CW\*(C`<>\*(C'\fR can be used to designate a subroutine
839to handle non-option arguments. When \fIGetOptions()\fR encounters an
840argument that does not look like an option, it will immediately call this
841subroutine and passes it one parameter: the argument name.
842.PP
843For example:
844.PP
845.Vb 3
846\& my $width = 80;
847\& sub process { ... }
848\& GetOptions ('width=i' => \e$width, '<>' => \e&process);
849.Ve
850.PP
851When applied to the following command line:
852.PP
853.Vb 1
854\& arg1 --width=72 arg2 --width=60 arg3
855.Ve
856.PP
857This will call
858\&\f(CW\*(C`process("arg1")\*(C'\fR while \f(CW$width\fR is \f(CW80\fR,
859\&\f(CW\*(C`process("arg2")\*(C'\fR while \f(CW$width\fR is \f(CW72\fR, and
860\&\f(CW\*(C`process("arg3")\*(C'\fR while \f(CW$width\fR is \f(CW60\fR.
861.PP
862This feature requires configuration option \fBpermute\fR, see section
863\&\*(L"Configuring Getopt::Long\*(R".
864.SH "Configuring Getopt::Long"
865.IX Header "Configuring Getopt::Long"
866Getopt::Long can be configured by calling subroutine
867\&\fIGetopt::Long::Configure()\fR. This subroutine takes a list of quoted
868strings, each specifying a configuration option to be enabled, e.g.
869\&\f(CW\*(C`ignore_case\*(C'\fR, or disabled, e.g. \f(CW\*(C`no_ignore_case\*(C'\fR. Case does not
870matter. Multiple calls to \fIConfigure()\fR are possible.
871.PP
872Alternatively, as of version 2.24, the configuration options may be
873passed together with the \f(CW\*(C`use\*(C'\fR statement:
874.PP
875.Vb 1
876\& use Getopt::Long qw(:config no_ignore_case bundling);
877.Ve
878.PP
879The following options are available:
880.IP "default" 12
881.IX Item "default"
882This option causes all configuration options to be reset to their
883default values.
884.IP "posix_default" 12
885.IX Item "posix_default"
886This option causes all configuration options to be reset to their
887default values as if the environment variable \s-1POSIXLY_CORRECT\s0 had
888been set.
889.IP "auto_abbrev" 12
890.IX Item "auto_abbrev"
891Allow option names to be abbreviated to uniqueness.
892Default is enabled unless environment variable
893\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`auto_abbrev\*(C'\fR is disabled.
894.IP "getopt_compat" 12
895.IX Item "getopt_compat"
896Allow \f(CW\*(C`+\*(C'\fR to start options.
897Default is enabled unless environment variable
898\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`getopt_compat\*(C'\fR is disabled.
899.IP "gnu_compat" 12
900.IX Item "gnu_compat"
901\&\f(CW\*(C`gnu_compat\*(C'\fR controls whether \f(CW\*(C`\-\-opt=\*(C'\fR is allowed, and what it should
902do. Without \f(CW\*(C`gnu_compat\*(C'\fR, \f(CW\*(C`\-\-opt=\*(C'\fR gives an error. With \f(CW\*(C`gnu_compat\*(C'\fR,
903\&\f(CW\*(C`\-\-opt=\*(C'\fR will give option \f(CW\*(C`opt\*(C'\fR and empty value.
904This is the way \s-1GNU\s0 \fIgetopt_long()\fR does it.
905.IP "gnu_getopt" 12
906.IX Item "gnu_getopt"
907This is a short way of setting \f(CW\*(C`gnu_compat\*(C'\fR \f(CW\*(C`bundling\*(C'\fR \f(CW\*(C`permute\*(C'\fR
908\&\f(CW\*(C`no_getopt_compat\*(C'\fR. With \f(CW\*(C`gnu_getopt\*(C'\fR, command line handling should be
909fully compatible with \s-1GNU\s0 \fIgetopt_long()\fR.
910.IP "require_order" 12
911.IX Item "require_order"
912Whether command line arguments are allowed to be mixed with options.
913Default is disabled unless environment variable
914\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`require_order\*(C'\fR is enabled.
915.Sp
916See also \f(CW\*(C`permute\*(C'\fR, which is the opposite of \f(CW\*(C`require_order\*(C'\fR.
917.IP "permute" 12
918.IX Item "permute"
919Whether command line arguments are allowed to be mixed with options.
920Default is enabled unless environment variable
921\&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`permute\*(C'\fR is disabled.
922Note that \f(CW\*(C`permute\*(C'\fR is the opposite of \f(CW\*(C`require_order\*(C'\fR.
923.Sp
924If \f(CW\*(C`permute\*(C'\fR is enabled, this means that
925.Sp
926.Vb 1
927\& --foo arg1 --bar arg2 arg3
928.Ve
929.Sp
930is equivalent to
931.Sp
932.Vb 1
933\& --foo --bar arg1 arg2 arg3
934.Ve
935.Sp
936If an argument callback routine is specified, \f(CW@ARGV\fR will always be
937empty upon successful return of \fIGetOptions()\fR since all options have been
938processed. The only exception is when \f(CW\*(C`\-\-\*(C'\fR is used:
939.Sp
940.Vb 1
941\& --foo arg1 --bar arg2 -- arg3
942.Ve
943.Sp
944This will call the callback routine for arg1 and arg2, and then
945terminate \fIGetOptions()\fR leaving \f(CW"arg2"\fR in \f(CW@ARGV\fR.
946.Sp
947If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing
948terminates when the first non-option is encountered.
949.Sp
950.Vb 1
951\& --foo arg1 --bar arg2 arg3
952.Ve
953.Sp
954is equivalent to
955.Sp
956.Vb 1
957\& --foo -- arg1 --bar arg2 arg3
958.Ve
959.Sp
960If \f(CW\*(C`pass_through\*(C'\fR is also enabled, options processing will terminate
961at the first unrecognized option, or non\-option, whichever comes
962first.
963.IP "bundling (default: disabled)" 12
964.IX Item "bundling (default: disabled)"
965Enabling this option will allow single-character options to be
966bundled. To distinguish bundles from long option names, long options
967\&\fImust\fR be introduced with \f(CW\*(C`\-\-\*(C'\fR and bundles with \f(CW\*(C`\-\*(C'\fR.
968.Sp
969Note that, if you have options \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`l\*(C'\fR and \f(CW\*(C`all\*(C'\fR, and
970auto_abbrev enabled, possible arguments and option settings are:
971.Sp
972.Vb 6
973\& using argument sets option(s)
974\& ------------------------------------------
975\& -a, --a a
976\& -l, --l l
977\& -al, -la, -ala, -all,... a, l
978\& --al, --all all
979.Ve
980.Sp
981The surprising part is that \f(CW\*(C`\-\-a\*(C'\fR sets option \f(CW\*(C`a\*(C'\fR (due to auto
982completion), not \f(CW\*(C`all\*(C'\fR.
983.Sp
984Note: disabling \f(CW\*(C`bundling\*(C'\fR also disables \f(CW\*(C`bundling_override\*(C'\fR.
985.IP "bundling_override (default: disabled)" 12
986.IX Item "bundling_override (default: disabled)"
987If \f(CW\*(C`bundling_override\*(C'\fR is enabled, bundling is enabled as with
988\&\f(CW\*(C`bundling\*(C'\fR but now long option names override option bundles.
989.Sp
990Note: disabling \f(CW\*(C`bundling_override\*(C'\fR also disables \f(CW\*(C`bundling\*(C'\fR.
991.Sp
992\&\fBNote:\fR Using option bundling can easily lead to unexpected results,
993especially when mixing long options and bundles. Caveat emptor.
994.IP "ignore_case (default: enabled)" 12
995.IX Item "ignore_case (default: enabled)"
996If enabled, case is ignored when matching long option names. If,
997however, bundling is enabled as well, single character options will be
998treated case\-sensitive.
999.Sp
1000With \f(CW\*(C`ignore_case\*(C'\fR, option specifications for options that only
1001differ in case, e.g., \f(CW"foo"\fR and \f(CW"Foo"\fR, will be flagged as
1002duplicates.
1003.Sp
1004Note: disabling \f(CW\*(C`ignore_case\*(C'\fR also disables \f(CW\*(C`ignore_case_always\*(C'\fR.
1005.IP "ignore_case_always (default: disabled)" 12
1006.IX Item "ignore_case_always (default: disabled)"
1007When bundling is in effect, case is ignored on single-character
1008options also.
1009.Sp
1010Note: disabling \f(CW\*(C`ignore_case_always\*(C'\fR also disables \f(CW\*(C`ignore_case\*(C'\fR.
1011.IP "auto_version (default:disabled)" 12
1012.IX Item "auto_version (default:disabled)"
1013Automatically provide support for the \fB\-\-version\fR option if
1014the application did not specify a handler for this option itself.
1015.Sp
1016Getopt::Long will provide a standard version message that includes the
1017program name, its version (if \f(CW$main::VERSION\fR is defined), and the
1018versions of Getopt::Long and Perl. The message will be written to
1019standard output and processing will terminate.
1020.Sp
1021\&\f(CW\*(C`auto_version\*(C'\fR will be enabled if the calling program explicitly
1022specified a version number higher than 2.32 in the \f(CW\*(C`use\*(C'\fR or
1023\&\f(CW\*(C`require\*(C'\fR statement.
1024.IP "auto_help (default:disabled)" 12
1025.IX Item "auto_help (default:disabled)"
1026Automatically provide support for the \fB\-\-help\fR and \fB\-?\fR options if
1027the application did not specify a handler for this option itself.
1028.Sp
1029Getopt::Long will provide a help message using module Pod::Usage. The
1030message, derived from the \s-1SYNOPSIS\s0 \s-1POD\s0 section, will be written to
1031standard output and processing will terminate.
1032.Sp
1033\&\f(CW\*(C`auto_help\*(C'\fR will be enabled if the calling program explicitly
1034specified a version number higher than 2.32 in the \f(CW\*(C`use\*(C'\fR or
1035\&\f(CW\*(C`require\*(C'\fR statement.
1036.IP "pass_through (default: disabled)" 12
1037.IX Item "pass_through (default: disabled)"
1038Options that are unknown, ambiguous or supplied with an invalid option
1039value are passed through in \f(CW@ARGV\fR instead of being flagged as
1040errors. This makes it possible to write wrapper scripts that process
1041only part of the user supplied command line arguments, and pass the
1042remaining options to some other program.
1043.Sp
1044If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing will terminate at
1045the first unrecognized option, or non\-option, whichever comes first.
1046However, if \f(CW\*(C`permute\*(C'\fR is enabled instead, results can become confusing.
1047.Sp
1048Note that the options terminator (default \f(CW\*(C`\-\-\*(C'\fR), if present, will
1049also be passed through in \f(CW@ARGV\fR.
1050.IP "prefix" 12
1051.IX Item "prefix"
1052The string that starts options. If a constant string is not
1053sufficient, see \f(CW\*(C`prefix_pattern\*(C'\fR.
1054.IP "prefix_pattern" 12
1055.IX Item "prefix_pattern"
1056A Perl pattern that identifies the strings that introduce options.
1057Default is \f(CW\*(C`\-\-|\-|\e+\*(C'\fR unless environment variable
1058\&\s-1POSIXLY_CORRECT\s0 has been set, in which case it is \f(CW\*(C`\-\-|\-\*(C'\fR.
1059.IP "long_prefix_pattern" 12
1060.IX Item "long_prefix_pattern"
1061A Perl pattern that allows the disambiguation of long and short
1062prefixes. Default is \f(CW\*(C`\-\-\*(C'\fR.
1063.Sp
1064Typically you only need to set this if you are using nonstandard
1065prefixes and want some or all of them to have the same semantics as
1066\&'\-\-' does under normal circumstances.
1067.Sp
1068For example, setting prefix_pattern to \f(CW\*(C`\-\-|\-|\e+|\e/\*(C'\fR and
1069long_prefix_pattern to \f(CW\*(C`\-\-|\e/\*(C'\fR would add Win32 style argument
1070handling.
1071.IP "debug (default: disabled)" 12
1072.IX Item "debug (default: disabled)"
1073Enable debugging output.
1074.SH "Exportable Methods"
1075.IX Header "Exportable Methods"
1076.IP "VersionMessage" 4
1077.IX Item "VersionMessage"
1078This subroutine provides a standard version message. Its argument can be:
1079.RS 4
1080.IP "*" 4
1081A string containing the text of a message to print \fIbefore\fR printing
1082the standard message.
1083.IP "*" 4
1084A numeric value corresponding to the desired exit status.
1085.IP "*" 4
1086A reference to a hash.
1087.RE
1088.RS 4
1089.Sp
1090If more than one argument is given then the entire argument list is
1091assumed to be a hash. If a hash is supplied (either as a reference or
1092as a list) it should contain one or more elements with the following
1093keys:
1094.ie n .IP """\-message""" 4
1095.el .IP "\f(CW\-message\fR" 4
1096.IX Item "-message"
1097.PD 0
1098.ie n .IP """\-msg""" 4
1099.el .IP "\f(CW\-msg\fR" 4
1100.IX Item "-msg"
1101.PD
1102The text of a message to print immediately prior to printing the
1103program's usage message.
1104.ie n .IP """\-exitval""" 4
1105.el .IP "\f(CW\-exitval\fR" 4
1106.IX Item "-exitval"
1107The desired exit status to pass to the \fB\f(BIexit()\fB\fR function.
1108This should be an integer, or else the string \*(L"\s-1NOEXIT\s0\*(R" to
1109indicate that control should simply be returned without
1110terminating the invoking process.
1111.ie n .IP """\-output""" 4
1112.el .IP "\f(CW\-output\fR" 4
1113.IX Item "-output"
1114A reference to a filehandle, or the pathname of a file to which the
1115usage message should be written. The default is \f(CW\*(C`\e*STDERR\*(C'\fR unless the
1116exit value is less than 2 (in which case the default is \f(CW\*(C`\e*STDOUT\*(C'\fR).
1117.RE
1118.RS 4
1119.Sp
1120You cannot tie this routine directly to an option, e.g.:
1121.Sp
1122.Vb 1
1123\& GetOptions("version" => \e&VersionMessage);
1124.Ve
1125.Sp
1126Use this instead:
1127.Sp
1128.Vb 1
1129\& GetOptions("version" => sub { VersionMessage() });
1130.Ve
1131.RE
1132.IP "HelpMessage" 4
1133.IX Item "HelpMessage"
1134This subroutine produces a standard help message, derived from the
1135program's \s-1POD\s0 section \s-1SYNOPSIS\s0 using Pod::Usage. It takes the same
1136arguments as \fIVersionMessage()\fR. In particular, you cannot tie it
1137directly to an option, e.g.:
1138.Sp
1139.Vb 1
1140\& GetOptions("help" => \e&HelpMessage);
1141.Ve
1142.Sp
1143Use this instead:
1144.Sp
1145.Vb 1
1146\& GetOptions("help" => sub { HelpMessage() });
1147.Ve
1148.SH "Return values and Errors"
1149.IX Header "Return values and Errors"
1150Configuration errors and errors in the option definitions are
1151signalled using \fIdie()\fR and will terminate the calling program unless
1152the call to \fIGetopt::Long::GetOptions()\fR was embedded in \f(CW\*(C`eval { ...
1153}\*(C'\fR, or \fIdie()\fR was trapped using \f(CW$SIG{_\|_DIE_\|_}\fR.
1154.PP
1155GetOptions returns true to indicate success.
1156It returns false when the function detected one or more errors during
1157option parsing. These errors are signalled using \fIwarn()\fR and can be
1158trapped with \f(CW$SIG{_\|_WARN_\|_}\fR.
1159.SH "Legacy"
1160.IX Header "Legacy"
1161The earliest development of \f(CW\*(C`newgetopt.pl\*(C'\fR started in 1990, with Perl
1162version 4. As a result, its development, and the development of
1163Getopt::Long, has gone through several stages. Since backward
1164compatibility has always been extremely important, the current version
1165of Getopt::Long still supports a lot of constructs that nowadays are
1166no longer necessary or otherwise unwanted. This section describes
1167briefly some of these 'features'.
1168.Sh "Default destinations"
1169.IX Subsection "Default destinations"
1170When no destination is specified for an option, GetOptions will store
1171the resultant value in a global variable named \f(CW\*(C`opt_\*(C'\fR\fI\s-1XXX\s0\fR, where
1172\&\fI\s-1XXX\s0\fR is the primary name of this option. When a progam executes
1173under \f(CW\*(C`use strict\*(C'\fR (recommended), these variables must be
1174pre-declared with \fIour()\fR or \f(CW\*(C`use vars\*(C'\fR.
1175.PP
1176.Vb 2
1177\& our $opt_length = 0;
1178\& GetOptions ('length=i'); # will store in $opt_length
1179.Ve
1180.PP
1181To yield a usable Perl variable, characters that are not part of the
1182syntax for variables are translated to underscores. For example,
1183\&\f(CW\*(C`\-\-fpp\-struct\-return\*(C'\fR will set the variable
1184\&\f(CW$opt_fpp_struct_return\fR. Note that this variable resides in the
1185namespace of the calling program, not necessarily \f(CW\*(C`main\*(C'\fR. For
1186example:
1187.PP
1188.Vb 1
1189\& GetOptions ("size=i", "sizes=i@");
1190.Ve
1191.PP
1192with command line \*(L"\-size 10 \-sizes 24 \-sizes 48\*(R" will perform the
1193equivalent of the assignments
1194.PP
1195.Vb 2
1196\& $opt_size = 10;
1197\& @opt_sizes = (24, 48);
1198.Ve
1199.Sh "Alternative option starters"
1200.IX Subsection "Alternative option starters"
1201A string of alternative option starter characters may be passed as the
1202first argument (or the first argument after a leading hash reference
1203argument).
1204.PP
1205.Vb 2
1206\& my $len = 0;
1207\& GetOptions ('/', 'length=i' => $len);
1208.Ve
1209.PP
1210Now the command line may look like:
1211.PP
1212.Vb 1
1213\& /length 24 -- arg
1214.Ve
1215.PP
1216Note that to terminate options processing still requires a double dash
1217\&\f(CW\*(C`\-\-\*(C'\fR.
1218.PP
1219\&\fIGetOptions()\fR will not interpret a leading \f(CW"<>"\fR as option starters
1220if the next argument is a reference. To force \f(CW"<"\fR and \f(CW">"\fR as
1221option starters, use \f(CW"><"\fR. Confusing? Well, \fBusing a starter
1222argument is strongly deprecated\fR anyway.
1223.Sh "Configuration variables"
1224.IX Subsection "Configuration variables"
1225Previous versions of Getopt::Long used variables for the purpose of
1226configuring. Although manipulating these variables still work, it is
1227strongly encouraged to use the \f(CW\*(C`Configure\*(C'\fR routine that was introduced
1228in version 2.17. Besides, it is much easier.
1229.SH "Trouble Shooting"
1230.IX Header "Trouble Shooting"
1231.Sh "GetOptions does not return a false result when an option is not supplied"
1232.IX Subsection "GetOptions does not return a false result when an option is not supplied"
1233That's why they're called 'options'.
1234.Sh "GetOptions does not split the command line correctly"
1235.IX Subsection "GetOptions does not split the command line correctly"
1236The command line is not split by GetOptions, but by the command line
1237interpreter (\s-1CLI\s0). On Unix, this is the shell. On Windows, it is
1238\&\s-1COMMAND\s0.COM or \s-1CMD\s0.EXE. Other operating systems have other CLIs.
1239.PP
1240It is important to know that these CLIs may behave different when the
1241command line contains special characters, in particular quotes or
1242backslashes. For example, with Unix shells you can use single quotes
1243(\f(CW\*(C`'\*(C'\fR) and double quotes (\f(CW\*(C`"\*(C'\fR) to group words together. The following
1244alternatives are equivalent on Unix:
1245.PP
1246.Vb 3
1247\& "two words"
1248\& 'two words'
1249\& two\e words
1250.Ve
1251.PP
1252In case of doubt, insert the following statement in front of your Perl
1253program:
1254.PP
1255.Vb 1
1256\& print STDERR (join("|",@ARGV),"\en");
1257.Ve
1258.PP
1259to verify how your \s-1CLI\s0 passes the arguments to the program.
1260.Sh "Undefined subroutine &main::GetOptions called"
1261.IX Subsection "Undefined subroutine &main::GetOptions called"
1262Are you running Windows, and did you write
1263.PP
1264.Vb 1
1265\& use GetOpt::Long;
1266.Ve
1267.PP
1268(note the capital 'O')?
1269.ie n .Sh "How do I put a ""\-?"" option into a Getopt::Long?"
1270.el .Sh "How do I put a ``\-?'' option into a Getopt::Long?"
1271.IX Subsection "How do I put a -? option into a Getopt::Long?"
1272You can only obtain this using an alias, and Getopt::Long of at least
1273version 2.13.
1274.PP
1275.Vb 2
1276\& use Getopt::Long;
1277\& GetOptions ("help|?"); # -help and -? will both set $opt_help
1278.Ve
1279.SH "AUTHOR"
1280.IX Header "AUTHOR"
1281Johan Vromans <jvromans@squirrel.nl>
1282.SH "COPYRIGHT AND DISCLAIMER"
1283.IX Header "COPYRIGHT AND DISCLAIMER"
1284This program is Copyright 1990,2005 by Johan Vromans.
1285This program is free software; you can redistribute it and/or
1286modify it under the terms of the Perl Artistic License or the
1287\&\s-1GNU\s0 General Public License as published by the Free Software
1288Foundation; either version 2 of the License, or (at your option) any
1289later version.
1290.PP
1291This program is distributed in the hope that it will be useful,
1292but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of
1293\&\s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the
1294\&\s-1GNU\s0 General Public License for more details.
1295.PP
1296If you do not have a copy of the \s-1GNU\s0 General Public License write to
1297the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
1298\&\s-1MA\s0 02139, \s-1USA\s0.