Commit | Line | Data |
---|---|---|
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" | |
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\*(C`"linux\*(C'\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. For a scalar or array destination, | |
436 | the second argument is the value to be stored. For a hash destination, | |
437 | the second arguments is the key to the hash, and the third argument | |
438 | the value to be stored. It is up to the subroutine to store the value, | |
439 | or do whatever it thinks is appropriate. | |
440 | .PP | |
441 | A trivial application of this mechanism is to implement options that | |
442 | are 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 | |
450 | Here \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 | |
453 | If the subroutine needs to signal an error, it should call \fIdie()\fR with | |
454 | the 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 | |
456 | be returned upon completion. | |
457 | .PP | |
458 | If the text of the error message starts with an exclamation mark \f(CW\*(C`!\*(C'\fR | |
459 | it is interpreted specially by \fIGetOptions()\fR. There is currently one | |
460 | special command implemented: \f(CW\*(C`die("!FINISH")\*(C'\fR will cause \fIGetOptions()\fR | |
461 | to 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" | |
464 | Often it is user friendly to supply alternate mnemonic names for | |
465 | options. 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 | |
467 | specification, separated by vertical bar \f(CW\*(C`|\*(C'\fR characters. To implement | |
468 | the above example: | |
469 | .PP | |
470 | .Vb 1 | |
471 | \& GetOptions ('length|height=f' => \e$length); | |
472 | .Ve | |
473 | .PP | |
474 | The first name is called the \fIprimary\fR name, the other names are | |
475 | called \fIaliases\fR. When using a hash to store options, the key will | |
476 | always be the primary name. | |
477 | .PP | |
478 | Multiple alternate names are possible. | |
479 | .Sh "Case and abbreviations" | |
480 | .IX Subsection "Case and abbreviations" | |
481 | Without additional configuration, \fIGetOptions()\fR will ignore the case of | |
482 | option 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 | |
488 | This call will allow \f(CW\*(C`\-\-l\*(C'\fR and \f(CW\*(C`\-\-L\*(C'\fR for the length option, but | |
489 | requires 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" | |
492 | Each option specifier consists of two parts: the name specification | |
493 | and the argument specification. | |
494 | .PP | |
495 | The name specification contains the name of the option, optionally | |
496 | followed by a list of alternative names separated by vertical bar | |
497 | characters. | |
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 | |
504 | The argument specification is optional. If omitted, the option is | |
505 | considered boolean, a value of 1 will be assigned when the option is | |
506 | used on the command line. | |
507 | .PP | |
508 | The argument specification can be | |
509 | .IP "!" 4 | |
510 | The option does not take an argument and may be negated by prefixing | |
511 | 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 | |
512 | 1 will be assigned) as well as \f(CW\*(C`\-\-nofoo\*(C'\fR and \f(CW\*(C`\-\-no\-foo\*(C'\fR (a value of | |
513 | 0 will be assigned). If the option has aliases, this applies to the | |
514 | aliases as well. | |
515 | .Sp | |
516 | Using negation on a single letter option when bundling is in effect is | |
517 | pointless and will result in a warning. | |
518 | .IP "+" 4 | |
519 | The option does not take an argument and will be incremented by 1 | |
520 | every time it appears on the command line. E.g. \f(CW"more+"\fR, when used | |
521 | with \f(CW\*(C`\-\-more \-\-more \-\-more\*(C'\fR, will increment the value three times, | |
522 | resulting in a value of 3 (provided it was 0 or undefined at first). | |
523 | .Sp | |
524 | The \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 ]" | |
527 | The option requires an argument of the given type. Supported types | |
528 | are: | |
529 | .RS 4 | |
530 | .IP "s" 4 | |
531 | .IX Item "s" | |
532 | String. An arbitrary sequence of characters. It is valid for the | |
533 | argument to start with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR. | |
534 | .IP "i" 4 | |
535 | .IX Item "i" | |
536 | Integer. An optional leading plus or minus sign, followed by a | |
537 | sequence of digits. | |
538 | .IP "o" 4 | |
539 | Extended integer, Perl style. This can be either an optional leading | |
540 | plus or minus sign, followed by a sequence of digits, or an octal | |
541 | string (a zero, optionally followed by '0', '1', .. '7'), or a | |
542 | hexadecimal string (\f(CW\*(C`0x\*(C'\fR followed by '0' .. '9', 'a' .. 'f', case | |
543 | insensitive), or a binary string (\f(CW\*(C`0b\*(C'\fR followed by a series of '0' | |
544 | and '1'). | |
545 | .IP "f" 4 | |
546 | .IX Item "f" | |
547 | Real number. For example \f(CW3.14\fR, \f(CW\*(C`\-6.23E24\*(C'\fR and so on. | |
548 | .RE | |
549 | .RS 4 | |
550 | .Sp | |
551 | The \fIdesttype\fR can be \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR to specify that the option is | |
552 | list or a hash valued. This is only needed when the destination for | |
553 | the option value is not otherwise specified. It should be omitted when | |
554 | not needed. | |
555 | .Sp | |
556 | The \fIrepeat\fR specifies the number of values this option takes per | |
557 | 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. | |
558 | .Sp | |
559 | \&\fImin\fR denotes the minimal number of arguments. It defaults to 1 for | |
560 | options 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 | |
565 | upper bound to the number of argument values taken. | |
566 | .RE | |
567 | .IP ": \fItype\fR [ \fIdesttype\fR ]" 4 | |
568 | .IX Item ": type [ desttype ]" | |
569 | Like \f(CW\*(C`=\*(C'\fR, but designates the argument as optional. | |
570 | If omitted, an empty string will be assigned to string values options, | |
571 | and the value zero to numeric options. | |
572 | .Sp | |
573 | Note that if a string argument starts with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR, it will be | |
574 | considered an option on itself. | |
575 | .IP ": \fInumber\fR [ \fIdesttype\fR ]" 4 | |
576 | .IX Item ": number [ desttype ]" | |
577 | Like \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 ]" | |
580 | Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the current value for the | |
581 | option will be incremented. | |
582 | .SH "Advanced Possibilities" | |
583 | .IX Header "Advanced Possibilities" | |
584 | .Sh "Object oriented interface" | |
585 | .IX Subsection "Object oriented interface" | |
586 | Getopt::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 | |
595 | Configuration 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" | |
603 | Getopt::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 | |
605 | obsolete) threads implementation that was added to Perl 5.005. | |
606 | .Sh "Documentation and help texts" | |
607 | .IX Subsection "Documentation and help texts" | |
608 | Getopt::Long encourages the use of Pod::Usage to produce help | |
609 | messages. 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 | |
694 | See Pod::Usage for details. | |
695 | .Sh "Storing option values in a hash" | |
696 | .IX Subsection "Storing option values in a hash" | |
697 | Sometimes, for example when there are a lot of options, having a | |
698 | separate variable for each of them can be cumbersome. \fIGetOptions()\fR | |
699 | supports, as an alternative mechanism, storing options in a hash. | |
700 | .PP | |
701 | To obtain this, a reference to a hash must be passed \fIas the first | |
702 | argument\fR to \fIGetOptions()\fR. For each option that is specified on the | |
703 | command line, the option value will be stored in the hash with the | |
704 | option name as key. Options that are not actually used on the command | |
705 | line 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 | |
707 | was used. The drawback is that warnings will be issued if the program | |
708 | runs 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 | |
716 | For options that take list or hash values, it is necessary to indicate | |
717 | this 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 | |
723 | To make things more complicated, the hash may contain references to | |
724 | the 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 | |
732 | This 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 | |
739 | Any mixture is possible. For example, the most frequently used options | |
740 | could be stored in variables while all other options get stored in the | |
741 | hash: | |
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" | |
753 | With bundling it is possible to set several single-character options | |
754 | 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, | |
755 | .PP | |
756 | .Vb 1 | |
757 | \& -vax | |
758 | .Ve | |
759 | .PP | |
760 | would set all three. | |
761 | .PP | |
762 | Getopt::Long supports two levels of bundling. To enable bundling, a | |
763 | call to Getopt::Long::Configure is required. | |
764 | .PP | |
765 | The first level of bundling can be enabled with: | |
766 | .PP | |
767 | .Vb 1 | |
768 | \& Getopt::Long::Configure ("bundling"); | |
769 | .Ve | |
770 | .PP | |
771 | Configured this way, single-character options can be bundled but long | |
772 | options \fBmust\fR always start with a double dash \f(CW\*(C`\-\-\*(C'\fR to avoid | |
773 | 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 | |
774 | options, | |
775 | .PP | |
776 | .Vb 1 | |
777 | \& -vax | |
778 | .Ve | |
779 | .PP | |
780 | would 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 | |
786 | would set \f(CW\*(C`vax\*(C'\fR. | |
787 | .PP | |
788 | The second level of bundling lifts this restriction. It can be enabled | |
789 | with: | |
790 | .PP | |
791 | .Vb 1 | |
792 | \& Getopt::Long::Configure ("bundling_override"); | |
793 | .Ve | |
794 | .PP | |
795 | Now, \f(CW\*(C`\-vax\*(C'\fR would set the option \f(CW\*(C`vax\*(C'\fR. | |
796 | .PP | |
797 | When any level of bundling is enabled, option values may be inserted | |
798 | in the bundle. For example: | |
799 | .PP | |
800 | .Vb 1 | |
801 | \& -h24w80 | |
802 | .Ve | |
803 | .PP | |
804 | is equivalent to | |
805 | .PP | |
806 | .Vb 1 | |
807 | \& -h 24 -w 80 | |
808 | .Ve | |
809 | .PP | |
810 | When configured for bundling, single-character options are matched | |
811 | case sensitive while long options are matched case insensitive. To | |
812 | have the single-character options matched case insensitive as well, | |
813 | use: | |
814 | .PP | |
815 | .Vb 1 | |
816 | \& Getopt::Long::Configure ("bundling", "ignorecase_always"); | |
817 | .Ve | |
818 | .PP | |
819 | It goes without saying that bundling can be quite confusing. | |
820 | .Sh "The lonesome dash" | |
821 | .IX Subsection "The lonesome dash" | |
822 | Normally, a lone dash \f(CW\*(C`\-\*(C'\fR on the command line will not be considered | |
823 | an option. Option processing will terminate (unless \*(L"permute\*(R" is | |
824 | configured) and the dash will be left in \f(CW@ARGV\fR. | |
825 | .PP | |
826 | It is possible to get special treatment for a lone dash. This can be | |
827 | achieved by adding an option specification with an empty name, for | |
828 | example: | |
829 | .PP | |
830 | .Vb 1 | |
831 | \& GetOptions ('' => \e$stdio); | |
832 | .Ve | |
833 | .PP | |
834 | A lone dash on the command line will now be a legal option, and using | |
835 | it will set variable \f(CW$stdio\fR. | |
836 | .Sh "Argument callback" | |
837 | .IX Subsection "Argument callback" | |
838 | A special option 'name' \f(CW\*(C`<>\*(C'\fR can be used to designate a subroutine | |
839 | to handle non-option arguments. When \fIGetOptions()\fR encounters an | |
840 | argument that does not look like an option, it will immediately call this | |
841 | subroutine and passes it one parameter: the argument name. | |
842 | .PP | |
843 | For 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 | |
851 | When applied to the following command line: | |
852 | .PP | |
853 | .Vb 1 | |
854 | \& arg1 --width=72 arg2 --width=60 arg3 | |
855 | .Ve | |
856 | .PP | |
857 | This 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 | |
862 | This 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" | |
866 | Getopt::Long can be configured by calling subroutine | |
867 | \&\fIGetopt::Long::Configure()\fR. This subroutine takes a list of quoted | |
868 | strings, 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 | |
870 | matter. Multiple calls to \fIConfigure()\fR are possible. | |
871 | .PP | |
872 | Alternatively, as of version 2.24, the configuration options may be | |
873 | passed 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 | |
879 | The following options are available: | |
880 | .IP "default" 12 | |
881 | .IX Item "default" | |
882 | This option causes all configuration options to be reset to their | |
883 | default values. | |
884 | .IP "posix_default" 12 | |
885 | .IX Item "posix_default" | |
886 | This option causes all configuration options to be reset to their | |
887 | default values as if the environment variable \s-1POSIXLY_CORRECT\s0 had | |
888 | been set. | |
889 | .IP "auto_abbrev" 12 | |
890 | .IX Item "auto_abbrev" | |
891 | Allow option names to be abbreviated to uniqueness. | |
892 | Default 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" | |
896 | Allow \f(CW\*(C`+\*(C'\fR to start options. | |
897 | Default 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 | |
902 | 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, | |
903 | \&\f(CW\*(C`\-\-opt=\*(C'\fR will give option \f(CW\*(C`opt\*(C'\fR and empty value. | |
904 | This is the way \s-1GNU\s0 \fIgetopt_long()\fR does it. | |
905 | .IP "gnu_getopt" 12 | |
906 | .IX Item "gnu_getopt" | |
907 | 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 | |
908 | \&\f(CW\*(C`no_getopt_compat\*(C'\fR. With \f(CW\*(C`gnu_getopt\*(C'\fR, command line handling should be | |
909 | fully compatible with \s-1GNU\s0 \fIgetopt_long()\fR. | |
910 | .IP "require_order" 12 | |
911 | .IX Item "require_order" | |
912 | Whether command line arguments are allowed to be mixed with options. | |
913 | Default 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 | |
916 | See 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" | |
919 | Whether command line arguments are allowed to be mixed with options. | |
920 | Default is enabled unless environment variable | |
921 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`permute\*(C'\fR is disabled. | |
922 | Note that \f(CW\*(C`permute\*(C'\fR is the opposite of \f(CW\*(C`require_order\*(C'\fR. | |
923 | .Sp | |
924 | If \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 | |
930 | is equivalent to | |
931 | .Sp | |
932 | .Vb 1 | |
933 | \& --foo --bar arg1 arg2 arg3 | |
934 | .Ve | |
935 | .Sp | |
936 | If an argument callback routine is specified, \f(CW@ARGV\fR will always be | |
937 | empty upon successful return of \fIGetOptions()\fR since all options have been | |
938 | processed. 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 | |
944 | This will call the callback routine for arg1 and arg2, and then | |
945 | terminate \fIGetOptions()\fR leaving \f(CW"arg2"\fR in \f(CW@ARGV\fR. | |
946 | .Sp | |
947 | If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing | |
948 | terminates when the first non-option is encountered. | |
949 | .Sp | |
950 | .Vb 1 | |
951 | \& --foo arg1 --bar arg2 arg3 | |
952 | .Ve | |
953 | .Sp | |
954 | is equivalent to | |
955 | .Sp | |
956 | .Vb 1 | |
957 | \& --foo -- arg1 --bar arg2 arg3 | |
958 | .Ve | |
959 | .Sp | |
960 | If \f(CW\*(C`pass_through\*(C'\fR is also enabled, options processing will terminate | |
961 | at the first unrecognized option, or non\-option, whichever comes | |
962 | first. | |
963 | .IP "bundling (default: disabled)" 12 | |
964 | .IX Item "bundling (default: disabled)" | |
965 | Enabling this option will allow single-character options to be | |
966 | bundled. 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 | |
969 | 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 | |
970 | auto_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 | |
981 | The surprising part is that \f(CW\*(C`\-\-a\*(C'\fR sets option \f(CW\*(C`a\*(C'\fR (due to auto | |
982 | completion), not \f(CW\*(C`all\*(C'\fR. | |
983 | .Sp | |
984 | Note: 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)" | |
987 | If \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 | |
990 | Note: 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, | |
993 | especially when mixing long options and bundles. Caveat emptor. | |
994 | .IP "ignore_case (default: enabled)" 12 | |
995 | .IX Item "ignore_case (default: enabled)" | |
996 | If enabled, case is ignored when matching long option names. If, | |
997 | however, bundling is enabled as well, single character options will be | |
998 | treated case\-sensitive. | |
999 | .Sp | |
1000 | With \f(CW\*(C`ignore_case\*(C'\fR, option specifications for options that only | |
1001 | differ in case, e.g., \f(CW"foo"\fR and \f(CW"Foo"\fR, will be flagged as | |
1002 | duplicates. | |
1003 | .Sp | |
1004 | Note: 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)" | |
1007 | When bundling is in effect, case is ignored on single-character | |
1008 | options also. | |
1009 | .Sp | |
1010 | Note: 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)" | |
1013 | Automatically provide support for the \fB\-\-version\fR option if | |
1014 | the application did not specify a handler for this option itself. | |
1015 | .Sp | |
1016 | Getopt::Long will provide a standard version message that includes the | |
1017 | program name, its version (if \f(CW$main::VERSION\fR is defined), and the | |
1018 | versions of Getopt::Long and Perl. The message will be written to | |
1019 | standard output and processing will terminate. | |
1020 | .Sp | |
1021 | \&\f(CW\*(C`auto_version\*(C'\fR will be enabled if the calling program explicitly | |
1022 | specified 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)" | |
1026 | Automatically provide support for the \fB\-\-help\fR and \fB\-?\fR options if | |
1027 | the application did not specify a handler for this option itself. | |
1028 | .Sp | |
1029 | Getopt::Long will provide a help message using module Pod::Usage. The | |
1030 | message, derived from the \s-1SYNOPSIS\s0 \s-1POD\s0 section, will be written to | |
1031 | standard output and processing will terminate. | |
1032 | .Sp | |
1033 | \&\f(CW\*(C`auto_help\*(C'\fR will be enabled if the calling program explicitly | |
1034 | specified 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)" | |
1038 | Options that are unknown, ambiguous or supplied with an invalid option | |
1039 | value are passed through in \f(CW@ARGV\fR instead of being flagged as | |
1040 | errors. This makes it possible to write wrapper scripts that process | |
1041 | only part of the user supplied command line arguments, and pass the | |
1042 | remaining options to some other program. | |
1043 | .Sp | |
1044 | If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing will terminate at | |
1045 | the first unrecognized option, or non\-option, whichever comes first. | |
1046 | However, if \f(CW\*(C`permute\*(C'\fR is enabled instead, results can become confusing. | |
1047 | .Sp | |
1048 | Note that the options terminator (default \f(CW\*(C`\-\-\*(C'\fR), if present, will | |
1049 | also be passed through in \f(CW@ARGV\fR. | |
1050 | .IP "prefix" 12 | |
1051 | .IX Item "prefix" | |
1052 | The string that starts options. If a constant string is not | |
1053 | sufficient, see \f(CW\*(C`prefix_pattern\*(C'\fR. | |
1054 | .IP "prefix_pattern" 12 | |
1055 | .IX Item "prefix_pattern" | |
1056 | A Perl pattern that identifies the strings that introduce options. | |
1057 | Default 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" | |
1061 | A Perl pattern that allows the disambiguation of long and short | |
1062 | prefixes. Default is \f(CW\*(C`\-\-\*(C'\fR. | |
1063 | .Sp | |
1064 | Typically you only need to set this if you are using nonstandard | |
1065 | prefixes and want some or all of them to have the same semantics as | |
1066 | \&'\-\-' does under normal circumstances. | |
1067 | .Sp | |
1068 | For example, setting prefix_pattern to \f(CW\*(C`\-\-|\-|\e+|\e/\*(C'\fR and | |
1069 | long_prefix_pattern to \f(CW\*(C`\-\-|\e/\*(C'\fR would add Win32 style argument | |
1070 | handling. | |
1071 | .IP "debug (default: disabled)" 12 | |
1072 | .IX Item "debug (default: disabled)" | |
1073 | Enable debugging output. | |
1074 | .SH "Exportable Methods" | |
1075 | .IX Header "Exportable Methods" | |
1076 | .IP "VersionMessage" 4 | |
1077 | .IX Item "VersionMessage" | |
1078 | This subroutine provides a standard version message. Its argument can be: | |
1079 | .RS 4 | |
1080 | .IP "*" 4 | |
1081 | A string containing the text of a message to print \fIbefore\fR printing | |
1082 | the standard message. | |
1083 | .IP "*" 4 | |
1084 | A numeric value corresponding to the desired exit status. | |
1085 | .IP "*" 4 | |
1086 | A reference to a hash. | |
1087 | .RE | |
1088 | .RS 4 | |
1089 | .Sp | |
1090 | If more than one argument is given then the entire argument list is | |
1091 | assumed to be a hash. If a hash is supplied (either as a reference or | |
1092 | as a list) it should contain one or more elements with the following | |
1093 | keys: | |
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 | |
1102 | The text of a message to print immediately prior to printing the | |
1103 | program's usage message. | |
1104 | .ie n .IP """\-exitval""" 4 | |
1105 | .el .IP "\f(CW\-exitval\fR" 4 | |
1106 | .IX Item "-exitval" | |
1107 | The desired exit status to pass to the \fB\f(BIexit()\fB\fR function. | |
1108 | This should be an integer, or else the string \*(L"\s-1NOEXIT\s0\*(R" to | |
1109 | indicate that control should simply be returned without | |
1110 | terminating the invoking process. | |
1111 | .ie n .IP """\-output""" 4 | |
1112 | .el .IP "\f(CW\-output\fR" 4 | |
1113 | .IX Item "-output" | |
1114 | A reference to a filehandle, or the pathname of a file to which the | |
1115 | usage message should be written. The default is \f(CW\*(C`\e*STDERR\*(C'\fR unless the | |
1116 | exit 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 | |
1120 | You cannot tie this routine directly to an option, e.g.: | |
1121 | .Sp | |
1122 | .Vb 1 | |
1123 | \& GetOptions("version" => \e&VersionMessage); | |
1124 | .Ve | |
1125 | .Sp | |
1126 | Use this instead: | |
1127 | .Sp | |
1128 | .Vb 1 | |
1129 | \& GetOptions("version" => sub { VersionMessage() }); | |
1130 | .Ve | |
1131 | .RE | |
1132 | .IP "HelpMessage" 4 | |
1133 | .IX Item "HelpMessage" | |
1134 | This subroutine produces a standard help message, derived from the | |
1135 | program's \s-1POD\s0 section \s-1SYNOPSIS\s0 using Pod::Usage. It takes the same | |
1136 | arguments as \fIVersionMessage()\fR. In particular, you cannot tie it | |
1137 | directly to an option, e.g.: | |
1138 | .Sp | |
1139 | .Vb 1 | |
1140 | \& GetOptions("help" => \e&HelpMessage); | |
1141 | .Ve | |
1142 | .Sp | |
1143 | Use 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" | |
1150 | Configuration errors and errors in the option definitions are | |
1151 | signalled using \fIdie()\fR and will terminate the calling program unless | |
1152 | the 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 | |
1155 | GetOptions returns true to indicate success. | |
1156 | It returns false when the function detected one or more errors during | |
1157 | option parsing. These errors are signalled using \fIwarn()\fR and can be | |
1158 | trapped with \f(CW$SIG{_\|_WARN_\|_}\fR. | |
1159 | .SH "Legacy" | |
1160 | .IX Header "Legacy" | |
1161 | The earliest development of \f(CW\*(C`newgetopt.pl\*(C'\fR started in 1990, with Perl | |
1162 | version 4. As a result, its development, and the development of | |
1163 | Getopt::Long, has gone through several stages. Since backward | |
1164 | compatibility has always been extremely important, the current version | |
1165 | of Getopt::Long still supports a lot of constructs that nowadays are | |
1166 | no longer necessary or otherwise unwanted. This section describes | |
1167 | briefly some of these 'features'. | |
1168 | .Sh "Default destinations" | |
1169 | .IX Subsection "Default destinations" | |
1170 | When no destination is specified for an option, GetOptions will store | |
1171 | the 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 | |
1173 | under \f(CW\*(C`use strict\*(C'\fR (recommended), these variables must be | |
1174 | pre-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 | |
1181 | To yield a usable Perl variable, characters that are not part of the | |
1182 | syntax 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 | |
1185 | namespace of the calling program, not necessarily \f(CW\*(C`main\*(C'\fR. For | |
1186 | example: | |
1187 | .PP | |
1188 | .Vb 1 | |
1189 | \& GetOptions ("size=i", "sizes=i@"); | |
1190 | .Ve | |
1191 | .PP | |
1192 | with command line \*(L"\-size 10 \-sizes 24 \-sizes 48\*(R" will perform the | |
1193 | equivalent 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" | |
1201 | A string of alternative option starter characters may be passed as the | |
1202 | first argument (or the first argument after a leading hash reference | |
1203 | argument). | |
1204 | .PP | |
1205 | .Vb 2 | |
1206 | \& my $len = 0; | |
1207 | \& GetOptions ('/', 'length=i' => $len); | |
1208 | .Ve | |
1209 | .PP | |
1210 | Now the command line may look like: | |
1211 | .PP | |
1212 | .Vb 1 | |
1213 | \& /length 24 -- arg | |
1214 | .Ve | |
1215 | .PP | |
1216 | Note 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 | |
1220 | if the next argument is a reference. To force \f(CW"<"\fR and \f(CW">"\fR as | |
1221 | option starters, use \f(CW"><"\fR. Confusing? Well, \fBusing a starter | |
1222 | argument is strongly deprecated\fR anyway. | |
1223 | .Sh "Configuration variables" | |
1224 | .IX Subsection "Configuration variables" | |
1225 | Previous versions of Getopt::Long used variables for the purpose of | |
1226 | configuring. Although manipulating these variables still work, it is | |
1227 | strongly encouraged to use the \f(CW\*(C`Configure\*(C'\fR routine that was introduced | |
1228 | in 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" | |
1233 | That'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" | |
1236 | The command line is not split by GetOptions, but by the command line | |
1237 | interpreter (\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 | |
1240 | It is important to know that these CLIs may behave different when the | |
1241 | command line contains special characters, in particular quotes or | |
1242 | backslashes. 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 | |
1244 | alternatives are equivalent on Unix: | |
1245 | .PP | |
1246 | .Vb 3 | |
1247 | \& "two words" | |
1248 | \& 'two words' | |
1249 | \& two\e words | |
1250 | .Ve | |
1251 | .PP | |
1252 | In case of doubt, insert the following statement in front of your Perl | |
1253 | program: | |
1254 | .PP | |
1255 | .Vb 1 | |
1256 | \& print STDERR (join("|",@ARGV),"\en"); | |
1257 | .Ve | |
1258 | .PP | |
1259 | to 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" | |
1262 | Are 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?" | |
1272 | You can only obtain this using an alias, and Getopt::Long of at least | |
1273 | version 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" | |
1281 | Johan Vromans <jvromans@squirrel.nl> | |
1282 | .SH "COPYRIGHT AND DISCLAIMER" | |
1283 | .IX Header "COPYRIGHT AND DISCLAIMER" | |
1284 | This program is Copyright 1990,2005 by Johan Vromans. | |
1285 | This program is free software; you can redistribute it and/or | |
1286 | modify it under the terms of the Perl Artistic License or the | |
1287 | \&\s-1GNU\s0 General Public License as published by the Free Software | |
1288 | Foundation; either version 2 of the License, or (at your option) any | |
1289 | later version. | |
1290 | .PP | |
1291 | This program is distributed in the hope that it will be useful, | |
1292 | but \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 | |
1296 | If you do not have a copy of the \s-1GNU\s0 General Public License write to | |
1297 | the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, | |
1298 | \&\s-1MA\s0 02139, \s-1USA\s0. |