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