Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
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 "2002-06-01" "perl v5.8.0" "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 | |
208 | the first Perl module that provided support for handling the new style | |
209 | of command line options, hence the name Getopt::Long. This module | |
210 | also supports single-character options and bundling. In this case, the | |
211 | options are restricted to alphabetic characters only, and the | |
212 | characters \f(CW\*(C`?\*(C'\fR and \f(CW\*(C`\-\*(C'\fR. | |
213 | .PP | |
214 | To use Getopt::Long from a Perl program, you must include the | |
215 | following line in your Perl program: | |
216 | .PP | |
217 | .Vb 1 | |
218 | \& use Getopt::Long; | |
219 | .Ve | |
220 | .PP | |
221 | This will load the core of the Getopt::Long module and prepare your | |
222 | program for using it. Most of the actual Getopt::Long code is not | |
223 | loaded until you really call one of its functions. | |
224 | .PP | |
225 | In the default configuration, options names may be abbreviated to | |
226 | uniqueness, case does not matter, and a single dash is sufficient, | |
227 | even for long option names. Also, options may be placed between | |
228 | non-option arguments. See \*(L"Configuring Getopt::Long\*(R" for more | |
229 | details on how to configure Getopt::Long. | |
230 | .Sh "Simple options" | |
231 | .IX Subsection "Simple options" | |
232 | The most simple options are the ones that take no values. Their mere | |
233 | presence on the command line enables the option. Popular examples are: | |
234 | .PP | |
235 | .Vb 1 | |
236 | \& --all --verbose --quiet --debug | |
237 | .Ve | |
238 | .PP | |
239 | Handling simple options is straightforward: | |
240 | .PP | |
241 | .Vb 3 | |
242 | \& my $verbose = ''; # option variable with default value (false) | |
243 | \& my $all = ''; # option variable with default value (false) | |
244 | \& GetOptions ('verbose' => \e$verbose, 'all' => \e$all); | |
245 | .Ve | |
246 | .PP | |
247 | The call to \fIGetOptions()\fR parses the command line arguments that are | |
248 | present in \f(CW@ARGV\fR and sets the option variable to the value \f(CW1\fR if | |
249 | the option did occur on the command line. Otherwise, the option | |
250 | variable is not touched. Setting the option value to true is often | |
251 | called \fIenabling\fR the option. | |
252 | .PP | |
253 | The option name as specified to the \fIGetOptions()\fR function is called | |
254 | the option \fIspecification\fR. Later we'll see that this specification | |
255 | can contain more than just the option name. The reference to the | |
256 | variable is called the option \fIdestination\fR. | |
257 | .PP | |
258 | \&\fIGetOptions()\fR will return a true value if the command line could be | |
259 | processed successfully. Otherwise, it will write error messages to | |
260 | \&\s-1STDERR\s0, and return a false result. | |
261 | .Sh "A little bit less simple options" | |
262 | .IX Subsection "A little bit less simple options" | |
263 | Getopt::Long supports two useful variants of simple options: | |
264 | \&\fInegatable\fR options and \fIincremental\fR options. | |
265 | .PP | |
266 | A negatable option is specified with an exclamation mark \f(CW\*(C`!\*(C'\fR after the | |
267 | option name: | |
268 | .PP | |
269 | .Vb 2 | |
270 | \& my $verbose = ''; # option variable with default value (false) | |
271 | \& GetOptions ('verbose!' => \e$verbose); | |
272 | .Ve | |
273 | .PP | |
274 | Now, using \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will enable \f(CW$verbose\fR, | |
275 | as expected. But it is also allowed to use \f(CW\*(C`\-\-noverbose\*(C'\fR, which will | |
276 | disable \f(CW$verbose\fR by setting its value to \f(CW0\fR. Using a suitable | |
277 | default value, the program can find out whether \f(CW$verbose\fR is false | |
278 | by default, or disabled by using \f(CW\*(C`\-\-noverbose\*(C'\fR. | |
279 | .PP | |
280 | An incremental option is specified with a plus \f(CW\*(C`+\*(C'\fR after the | |
281 | option name: | |
282 | .PP | |
283 | .Vb 2 | |
284 | \& my $verbose = ''; # option variable with default value (false) | |
285 | \& GetOptions ('verbose+' => \e$verbose); | |
286 | .Ve | |
287 | .PP | |
288 | Using \f(CW\*(C`\-\-verbose\*(C'\fR on the command line will increment the value of | |
289 | \&\f(CW$verbose\fR. This way the program can keep track of how many times the | |
290 | option occurred on the command line. For example, each occurrence of | |
291 | \&\f(CW\*(C`\-\-verbose\*(C'\fR could increase the verbosity level of the program. | |
292 | .Sh "Mixing command line option with other arguments" | |
293 | .IX Subsection "Mixing command line option with other arguments" | |
294 | Usually programs take command line options as well as other arguments, | |
295 | for example, file names. It is good practice to always specify the | |
296 | options first, and the other arguments last. Getopt::Long will, | |
297 | however, allow the options and arguments to be mixed and 'filter out' | |
298 | all the options before passing the rest of the arguments to the | |
299 | program. To stop Getopt::Long from processing further arguments, | |
300 | insert a double dash \f(CW\*(C`\-\-\*(C'\fR on the command line: | |
301 | .PP | |
302 | .Vb 1 | |
303 | \& --size 24 -- --all | |
304 | .Ve | |
305 | .PP | |
306 | In this example, \f(CW\*(C`\-\-all\*(C'\fR will \fInot\fR be treated as an option, but | |
307 | passed to the program unharmed, in \f(CW@ARGV\fR. | |
308 | .Sh "Options with values" | |
309 | .IX Subsection "Options with values" | |
310 | For options that take values it must be specified whether the option | |
311 | value is required or not, and what kind of value the option expects. | |
312 | .PP | |
313 | Three kinds of values are supported: integer numbers, floating point | |
314 | numbers, and strings. | |
315 | .PP | |
316 | If the option value is required, Getopt::Long will take the | |
317 | command line argument that follows the option and assign this to the | |
318 | option variable. If, however, the option value is specified as | |
319 | optional, this will only be done if that value does not look like a | |
320 | valid command line option itself. | |
321 | .PP | |
322 | .Vb 2 | |
323 | \& my $tag = ''; # option variable with default value | |
324 | \& GetOptions ('tag=s' => \e$tag); | |
325 | .Ve | |
326 | .PP | |
327 | In the option specification, the option name is followed by an equals | |
328 | sign \f(CW\*(C`=\*(C'\fR and the letter \f(CW\*(C`s\*(C'\fR. The equals sign indicates that this | |
329 | option requires a value. The letter \f(CW\*(C`s\*(C'\fR indicates that this value is | |
330 | an arbitrary string. Other possible value types are \f(CW\*(C`i\*(C'\fR for integer | |
331 | values, and \f(CW\*(C`f\*(C'\fR for floating point values. Using a colon \f(CW\*(C`:\*(C'\fR instead | |
332 | of the equals sign indicates that the option value is optional. In | |
333 | this case, if no suitable value is supplied, string valued options get | |
334 | an empty string \f(CW''\fR assigned, while numeric options are set to \f(CW0\fR. | |
335 | .Sh "Options with multiple values" | |
336 | .IX Subsection "Options with multiple values" | |
337 | Options sometimes take several values. For example, a program could | |
338 | use multiple directories to search for library files: | |
339 | .PP | |
340 | .Vb 1 | |
341 | \& --library lib/stdlib --library lib/extlib | |
342 | .Ve | |
343 | .PP | |
344 | To accomplish this behaviour, simply specify an array reference as the | |
345 | destination for the option: | |
346 | .PP | |
347 | .Vb 2 | |
348 | \& my @libfiles = (); | |
349 | \& GetOptions ("library=s" => \e@libfiles); | |
350 | .Ve | |
351 | .PP | |
352 | Used with the example above, \f(CW@libfiles\fR would contain two strings | |
353 | upon completion: \f(CW"lib/srdlib"\fR and \f(CW"lib/extlib"\fR, in that order. | |
354 | It is also possible to specify that only integer or floating point | |
355 | numbers are acceptible values. | |
356 | .PP | |
357 | Often it is useful to allow comma-separated lists of values as well as | |
358 | multiple occurrences of the options. This is easy using Perl's \fIsplit()\fR | |
359 | and \fIjoin()\fR operators: | |
360 | .PP | |
361 | .Vb 3 | |
362 | \& my @libfiles = (); | |
363 | \& GetOptions ("library=s" => \e@libfiles); | |
364 | \& @libfiles = split(/,/,join(',',@libfiles)); | |
365 | .Ve | |
366 | .PP | |
367 | Of course, it is important to choose the right separator string for | |
368 | each purpose. | |
369 | .Sh "Options with hash values" | |
370 | .IX Subsection "Options with hash values" | |
371 | If the option destination is a reference to a hash, the option will | |
372 | take, as value, strings of the form \fIkey\fR\f(CW\*(C`=\*(C'\fR\fIvalue\fR. The value will | |
373 | be stored with the specified key in the hash. | |
374 | .PP | |
375 | .Vb 2 | |
376 | \& my %defines = (); | |
377 | \& GetOptions ("define=s" => \e%defines); | |
378 | .Ve | |
379 | .PP | |
380 | When used with command line options: | |
381 | .PP | |
382 | .Vb 1 | |
383 | \& --define os=linux --define vendor=redhat | |
384 | .Ve | |
385 | .PP | |
386 | the hash \f(CW%defines\fR will contain two keys, \f(CW"os"\fR with value | |
387 | \&\f(CW\*(C`"linux\*(C'\fR and \f(CW"vendor"\fR with value \f(CW"redhat"\fR. | |
388 | It is also possible to specify that only integer or floating point | |
389 | numbers are acceptible values. The keys are always taken to be strings. | |
390 | .Sh "User-defined subroutines to handle options" | |
391 | .IX Subsection "User-defined subroutines to handle options" | |
392 | Ultimate control over what should be done when (actually: each time) | |
393 | an option is encountered on the command line can be achieved by | |
394 | designating a reference to a subroutine (or an anonymous subroutine) | |
395 | as the option destination. When \fIGetOptions()\fR encounters the option, it | |
396 | will call the subroutine with two or three arguments. The first | |
397 | argument is the name of the option. For a scalar or array destination, | |
398 | the second argument is the value to be stored. For a hash destination, | |
399 | the second arguments is the key to the hash, and the third argument | |
400 | the value to be stored. It is up to the subroutine to store the value, | |
401 | or do whatever it thinks is appropriate. | |
402 | .PP | |
403 | A trivial application of this mechanism is to implement options that | |
404 | are related to each other. For example: | |
405 | .PP | |
406 | .Vb 3 | |
407 | \& my $verbose = ''; # option variable with default value (false) | |
408 | \& GetOptions ('verbose' => \e$verbose, | |
409 | \& 'quiet' => sub { $verbose = 0 }); | |
410 | .Ve | |
411 | .PP | |
412 | Here \f(CW\*(C`\-\-verbose\*(C'\fR and \f(CW\*(C`\-\-quiet\*(C'\fR control the same variable | |
413 | \&\f(CW$verbose\fR, but with opposite values. | |
414 | .PP | |
415 | If the subroutine needs to signal an error, it should call \fIdie()\fR with | |
416 | the desired error message as its argument. \fIGetOptions()\fR will catch the | |
417 | \&\fIdie()\fR, issue the error message, and record that an error result must | |
418 | be returned upon completion. | |
419 | .PP | |
420 | If the text of the error message starts with an exclamantion mark \f(CW\*(C`!\*(C'\fR | |
421 | it is interpreted specially by \fIGetOptions()\fR. There is currently one | |
422 | special command implemented: \f(CW\*(C`die("!FINISH")\*(C'\fR will cause \fIGetOptions()\fR | |
423 | to stop processing options, as if it encountered a double dash \f(CW\*(C`\-\-\*(C'\fR. | |
424 | .Sh "Options with multiple names" | |
425 | .IX Subsection "Options with multiple names" | |
426 | Often it is user friendly to supply alternate mnemonic names for | |
427 | options. For example \f(CW\*(C`\-\-height\*(C'\fR could be an alternate name for | |
428 | \&\f(CW\*(C`\-\-length\*(C'\fR. Alternate names can be included in the option | |
429 | specification, separated by vertical bar \f(CW\*(C`|\*(C'\fR characters. To implement | |
430 | the above example: | |
431 | .PP | |
432 | .Vb 1 | |
433 | \& GetOptions ('length|height=f' => \e$length); | |
434 | .Ve | |
435 | .PP | |
436 | The first name is called the \fIprimary\fR name, the other names are | |
437 | called \fIaliases\fR. | |
438 | .PP | |
439 | Multiple alternate names are possible. | |
440 | .Sh "Case and abbreviations" | |
441 | .IX Subsection "Case and abbreviations" | |
442 | Without additional configuration, \fIGetOptions()\fR will ignore the case of | |
443 | option names, and allow the options to be abbreviated to uniqueness. | |
444 | .PP | |
445 | .Vb 1 | |
446 | \& GetOptions ('length|height=f' => \e$length, "head" => \e$head); | |
447 | .Ve | |
448 | .PP | |
449 | This call will allow \f(CW\*(C`\-\-l\*(C'\fR and \f(CW\*(C`\-\-L\*(C'\fR for the length option, but | |
450 | requires a least \f(CW\*(C`\-\-hea\*(C'\fR and \f(CW\*(C`\-\-hei\*(C'\fR for the head and height options. | |
451 | .Sh "Summary of Option Specifications" | |
452 | .IX Subsection "Summary of Option Specifications" | |
453 | Each option specifier consists of two parts: the name specification | |
454 | and the argument specification. | |
455 | .PP | |
456 | The name specification contains the name of the option, optionally | |
457 | followed by a list of alternative names separated by vertical bar | |
458 | characters. | |
459 | .PP | |
460 | .Vb 2 | |
461 | \& length option name is "length" | |
462 | \& length|size|l name is "length", aliases are "size" and "l" | |
463 | .Ve | |
464 | .PP | |
465 | The argument specification is optional. If omitted, the option is | |
466 | considered boolean, a value of 1 will be assigned when the option is | |
467 | used on the command line. | |
468 | .PP | |
469 | The argument specification can be | |
470 | .IP "!" 4 | |
471 | The option does not take an argument and may be negated, i.e. prefixed | |
472 | by \*(L"no\*(R". E.g. \f(CW"foo!"\fR will allow \f(CW\*(C`\-\-foo\*(C'\fR (a value of 1 will be | |
473 | assigned) and \f(CW\*(C`\-\-nofoo\*(C'\fR (a value of 0 will be assigned). If the | |
474 | option has aliases, this applies to the aliases as well. | |
475 | .Sp | |
476 | Using negation on a single letter option when bundling is in effect is | |
477 | pointless and will result in a warning. | |
478 | .IP "+" 4 | |
479 | The option does not take an argument and will be incremented by 1 | |
480 | every time it appears on the command line. E.g. \f(CW"more+"\fR, when used | |
481 | with \f(CW\*(C`\-\-more \-\-more \-\-more\*(C'\fR, will increment the value three times, | |
482 | resulting in a value of 3 (provided it was 0 or undefined at first). | |
483 | .Sp | |
484 | The \f(CW\*(C`+\*(C'\fR specifier is ignored if the option destination is not a scalar. | |
485 | .IP "= \fItype\fR [ \fIdesttype\fR ]" 4 | |
486 | .IX Item "= type [ desttype ]" | |
487 | The option requires an argument of the given type. Supported types | |
488 | are: | |
489 | .RS 4 | |
490 | .IP "s" 4 | |
491 | .IX Item "s" | |
492 | String. An arbitrary sequence of characters. It is valid for the | |
493 | argument to start with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR. | |
494 | .IP "i" 4 | |
495 | .IX Item "i" | |
496 | Integer. An optional leading plus or minus sign, followed by a | |
497 | sequence of digits. | |
498 | .IP "o" 4 | |
499 | Extended integer, Perl style. This can be either an optional leading | |
500 | plus or minus sign, followed by a sequence of digits, or an octal | |
501 | string (a zero, optionally followed by '0', '1', .. '7'), or a | |
502 | hexadecimal string (\f(CW\*(C`0x\*(C'\fR followed by '0' .. '9', 'a' .. 'f', case | |
503 | insensitive), or a binary string (\f(CW\*(C`0b\*(C'\fR followed by a series of '0' | |
504 | and '1'). | |
505 | .IP "f" 4 | |
506 | .IX Item "f" | |
507 | Real number. For example \f(CW3.14\fR, \f(CW\*(C`\-6.23E24\*(C'\fR and so on. | |
508 | .RE | |
509 | .RS 4 | |
510 | .Sp | |
511 | The \fIdesttype\fR can be \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR to specify that the option is | |
512 | list or a hash valued. This is only needed when the destination for | |
513 | the option value is not otherwise specified. It should be omitted when | |
514 | not needed. | |
515 | .RE | |
516 | .IP ": \fItype\fR [ \fIdesttype\fR ]" 4 | |
517 | .IX Item ": type [ desttype ]" | |
518 | Like \f(CW\*(C`=\*(C'\fR, but designates the argument as optional. | |
519 | If omitted, an empty string will be assigned to string values options, | |
520 | and the value zero to numeric options. | |
521 | .Sp | |
522 | Note that if a string argument starts with \f(CW\*(C`\-\*(C'\fR or \f(CW\*(C`\-\-\*(C'\fR, it will be | |
523 | considered an option on itself. | |
524 | .IP ": \fInumber\fR [ \fIdesttype\fR ]" 4 | |
525 | .IX Item ": number [ desttype ]" | |
526 | Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the \fInumber\fR will be assigned. | |
527 | .IP ": + [ \fIdesttype\fR ]" 4 | |
528 | .IX Item ": + [ desttype ]" | |
529 | Like \f(CW\*(C`:i\*(C'\fR, but if the value is omitted, the current value for the | |
530 | option will be incremented. | |
531 | .SH "Advanced Possibilities" | |
532 | .IX Header "Advanced Possibilities" | |
533 | .Sh "Object oriented interface" | |
534 | .IX Subsection "Object oriented interface" | |
535 | Getopt::Long can be used in an object oriented way as well: | |
536 | .PP | |
537 | .Vb 4 | |
538 | \& use Getopt::Long; | |
539 | \& $p = new Getopt::Long::Parser; | |
540 | \& $p->configure(...configuration options...); | |
541 | \& if ($p->getoptions(...options descriptions...)) ... | |
542 | .Ve | |
543 | .PP | |
544 | Configuration options can be passed to the constructor: | |
545 | .PP | |
546 | .Vb 2 | |
547 | \& $p = new Getopt::Long::Parser | |
548 | \& config => [...configuration options...]; | |
549 | .Ve | |
550 | .Sh "Thread Safety" | |
551 | .IX Subsection "Thread Safety" | |
552 | Getopt::Long is thread safe when using ithreads as of Perl 5.8. It is | |
553 | \&\fInot\fR thread safe when using the older (experimental and now | |
554 | obsolete) threads implementation that was added to Perl 5.005. | |
555 | .Sh "Documentation and help texts" | |
556 | .IX Subsection "Documentation and help texts" | |
557 | Getopt::Long encourages the use of Pod::Usage to produce help | |
558 | messages. For example: | |
559 | .PP | |
560 | .Vb 2 | |
561 | \& use Getopt::Long; | |
562 | \& use Pod::Usage; | |
563 | .Ve | |
564 | .PP | |
565 | .Vb 2 | |
566 | \& my $man = 0; | |
567 | \& my $help = 0; | |
568 | .Ve | |
569 | .PP | |
570 | .Vb 3 | |
571 | \& GetOptions('help|?' => \e$help, man => \e$man) or pod2usage(2); | |
572 | \& pod2usage(1) if $help; | |
573 | \& pod2usage(-exitstatus => 0, -verbose => 2) if $man; | |
574 | .Ve | |
575 | .PP | |
576 | .Vb 1 | |
577 | \& __END__ | |
578 | .Ve | |
579 | .PP | |
580 | .Vb 1 | |
581 | \& =head1 NAME | |
582 | .Ve | |
583 | .PP | |
584 | .Vb 1 | |
585 | \& sample - Using GetOpt::Long and Pod::Usage | |
586 | .Ve | |
587 | .PP | |
588 | .Vb 1 | |
589 | \& =head1 SYNOPSIS | |
590 | .Ve | |
591 | .PP | |
592 | .Vb 1 | |
593 | \& sample [options] [file ...] | |
594 | .Ve | |
595 | .PP | |
596 | .Vb 3 | |
597 | \& Options: | |
598 | \& -help brief help message | |
599 | \& -man full documentation | |
600 | .Ve | |
601 | .PP | |
602 | .Vb 1 | |
603 | \& =head1 OPTIONS | |
604 | .Ve | |
605 | .PP | |
606 | .Vb 1 | |
607 | \& =over 8 | |
608 | .Ve | |
609 | .PP | |
610 | .Vb 1 | |
611 | \& =item B<-help> | |
612 | .Ve | |
613 | .PP | |
614 | .Vb 1 | |
615 | \& Print a brief help message and exits. | |
616 | .Ve | |
617 | .PP | |
618 | .Vb 1 | |
619 | \& =item B<-man> | |
620 | .Ve | |
621 | .PP | |
622 | .Vb 1 | |
623 | \& Prints the manual page and exits. | |
624 | .Ve | |
625 | .PP | |
626 | .Vb 1 | |
627 | \& =back | |
628 | .Ve | |
629 | .PP | |
630 | .Vb 1 | |
631 | \& =head1 DESCRIPTION | |
632 | .Ve | |
633 | .PP | |
634 | .Vb 2 | |
635 | \& B<This program> will read the given input file(s) and do someting | |
636 | \& useful with the contents thereof. | |
637 | .Ve | |
638 | .PP | |
639 | .Vb 1 | |
640 | \& =cut | |
641 | .Ve | |
642 | .PP | |
643 | See Pod::Usage for details. | |
644 | .Sh "Storing options in a hash" | |
645 | .IX Subsection "Storing options in a hash" | |
646 | Sometimes, for example when there are a lot of options, having a | |
647 | separate variable for each of them can be cumbersome. \fIGetOptions()\fR | |
648 | supports, as an alternative mechanism, storing options in a hash. | |
649 | .PP | |
650 | To obtain this, a reference to a hash must be passed \fIas the first | |
651 | argument\fR to \fIGetOptions()\fR. For each option that is specified on the | |
652 | command line, the option value will be stored in the hash with the | |
653 | option name as key. Options that are not actually used on the command | |
654 | line will not be put in the hash, on other words, | |
655 | \&\f(CW\*(C`exists($h{option})\*(C'\fR (or \fIdefined()\fR) can be used to test if an option | |
656 | was used. The drawback is that warnings will be issued if the program | |
657 | runs under \f(CW\*(C`use strict\*(C'\fR and uses \f(CW$h{option}\fR without testing with | |
658 | \&\fIexists()\fR or \fIdefined()\fR first. | |
659 | .PP | |
660 | .Vb 2 | |
661 | \& my %h = (); | |
662 | \& GetOptions (\e%h, 'length=i'); # will store in $h{length} | |
663 | .Ve | |
664 | .PP | |
665 | For options that take list or hash values, it is necessary to indicate | |
666 | this by appending an \f(CW\*(C`@\*(C'\fR or \f(CW\*(C`%\*(C'\fR sign after the type: | |
667 | .PP | |
668 | .Vb 1 | |
669 | \& GetOptions (\e%h, 'colours=s@'); # will push to @{$h{colours}} | |
670 | .Ve | |
671 | .PP | |
672 | To make things more complicated, the hash may contain references to | |
673 | the actual destinations, for example: | |
674 | .PP | |
675 | .Vb 3 | |
676 | \& my $len = 0; | |
677 | \& my %h = ('length' => \e$len); | |
678 | \& GetOptions (\e%h, 'length=i'); # will store in $len | |
679 | .Ve | |
680 | .PP | |
681 | This example is fully equivalent with: | |
682 | .PP | |
683 | .Vb 2 | |
684 | \& my $len = 0; | |
685 | \& GetOptions ('length=i' => \e$len); # will store in $len | |
686 | .Ve | |
687 | .PP | |
688 | Any mixture is possible. For example, the most frequently used options | |
689 | could be stored in variables while all other options get stored in the | |
690 | hash: | |
691 | .PP | |
692 | .Vb 6 | |
693 | \& my $verbose = 0; # frequently referred | |
694 | \& my $debug = 0; # frequently referred | |
695 | \& my %h = ('verbose' => \e$verbose, 'debug' => \e$debug); | |
696 | \& GetOptions (\e%h, 'verbose', 'debug', 'filter', 'size=i'); | |
697 | \& if ( $verbose ) { ... } | |
698 | \& if ( exists $h{filter} ) { ... option 'filter' was specified ... } | |
699 | .Ve | |
700 | .Sh "Bundling" | |
701 | .IX Subsection "Bundling" | |
702 | With bundling it is possible to set several single-character options | |
703 | 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, | |
704 | .PP | |
705 | .Vb 1 | |
706 | \& -vax | |
707 | .Ve | |
708 | .PP | |
709 | would set all three. | |
710 | .PP | |
711 | Getopt::Long supports two levels of bundling. To enable bundling, a | |
712 | call to Getopt::Long::Configure is required. | |
713 | .PP | |
714 | The first level of bundling can be enabled with: | |
715 | .PP | |
716 | .Vb 1 | |
717 | \& Getopt::Long::Configure ("bundling"); | |
718 | .Ve | |
719 | .PP | |
720 | Configured this way, single-character options can be bundled but long | |
721 | options \fBmust\fR always start with a double dash \f(CW\*(C`\-\-\*(C'\fR to avoid | |
722 | abiguity. 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 | |
723 | options, | |
724 | .PP | |
725 | .Vb 1 | |
726 | \& -vax | |
727 | .Ve | |
728 | .PP | |
729 | would set \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`x\*(C'\fR, but | |
730 | .PP | |
731 | .Vb 1 | |
732 | \& --vax | |
733 | .Ve | |
734 | .PP | |
735 | would set \f(CW\*(C`vax\*(C'\fR. | |
736 | .PP | |
737 | The second level of bundling lifts this restriction. It can be enabled | |
738 | with: | |
739 | .PP | |
740 | .Vb 1 | |
741 | \& Getopt::Long::Configure ("bundling_override"); | |
742 | .Ve | |
743 | .PP | |
744 | Now, \f(CW\*(C`\-vax\*(C'\fR would set the option \f(CW\*(C`vax\*(C'\fR. | |
745 | .PP | |
746 | When any level of bundling is enabled, option values may be inserted | |
747 | in the bundle. For example: | |
748 | .PP | |
749 | .Vb 1 | |
750 | \& -h24w80 | |
751 | .Ve | |
752 | .PP | |
753 | is equivalent to | |
754 | .PP | |
755 | .Vb 1 | |
756 | \& -h 24 -w 80 | |
757 | .Ve | |
758 | .PP | |
759 | When configured for bundling, single-character options are matched | |
760 | case sensitive while long options are matched case insensitive. To | |
761 | have the single-character options matched case insensitive as well, | |
762 | use: | |
763 | .PP | |
764 | .Vb 1 | |
765 | \& Getopt::Long::Configure ("bundling", "ignorecase_always"); | |
766 | .Ve | |
767 | .PP | |
768 | It goes without saying that bundling can be quite confusing. | |
769 | .Sh "The lonesome dash" | |
770 | .IX Subsection "The lonesome dash" | |
771 | Normally, a lone dash \f(CW\*(C`\-\*(C'\fR on the command line will not be considered | |
772 | an option. Option processing will terminate (unless \*(L"permute\*(R" is | |
773 | configured) and the dash will be left in \f(CW@ARGV\fR. | |
774 | .PP | |
775 | It is possible to get special treatment for a lone dash. This can be | |
776 | achieved by adding an option specification with an empty name, for | |
777 | example: | |
778 | .PP | |
779 | .Vb 1 | |
780 | \& GetOptions ('' => \e$stdio); | |
781 | .Ve | |
782 | .PP | |
783 | A lone dash on the command line will now be a legal option, and using | |
784 | it will set variable \f(CW$stdio\fR. | |
785 | .Sh "Argument callback" | |
786 | .IX Subsection "Argument callback" | |
787 | A special option 'name' \f(CW\*(C`<\*(C'\fR> can be used to designate a subroutine | |
788 | to handle non-option arguments. When \fIGetOptions()\fR encounters an | |
789 | argument that does not look like an option, it will immediately call this | |
790 | subroutine and passes it one parameter: the argument name. | |
791 | .PP | |
792 | For example: | |
793 | .PP | |
794 | .Vb 3 | |
795 | \& my $width = 80; | |
796 | \& sub process { ... } | |
797 | \& GetOptions ('width=i' => \e$width, '<>' => \e&process); | |
798 | .Ve | |
799 | .PP | |
800 | When applied to the following command line: | |
801 | .PP | |
802 | .Vb 1 | |
803 | \& arg1 --width=72 arg2 --width=60 arg3 | |
804 | .Ve | |
805 | .PP | |
806 | This will call | |
807 | \&\f(CW\*(C`process("arg1")\*(C'\fR while \f(CW$width\fR is \f(CW80\fR, | |
808 | \&\f(CW\*(C`process("arg2")\*(C'\fR while \f(CW$width\fR is \f(CW72\fR, and | |
809 | \&\f(CW\*(C`process("arg3")\*(C'\fR while \f(CW$width\fR is \f(CW60\fR. | |
810 | .PP | |
811 | This feature requires configuration option \fBpermute\fR, see section | |
812 | \&\*(L"Configuring Getopt::Long\*(R". | |
813 | .SH "Configuring Getopt::Long" | |
814 | .IX Header "Configuring Getopt::Long" | |
815 | Getopt::Long can be configured by calling subroutine | |
816 | \&\fIGetopt::Long::Configure()\fR. This subroutine takes a list of quoted | |
817 | strings, each specifying a configuration option to be enabled, e.g. | |
818 | \&\f(CW\*(C`ignore_case\*(C'\fR, or disabled, e.g. \f(CW\*(C`no_ignore_case\*(C'\fR. Case does not | |
819 | matter. Multiple calls to \fIConfigure()\fR are possible. | |
820 | .PP | |
821 | Alternatively, as of version 2.24, the configuration options may be | |
822 | passed together with the \f(CW\*(C`use\*(C'\fR statement: | |
823 | .PP | |
824 | .Vb 1 | |
825 | \& use Getopt::Long qw(:config no_ignore_case bundling); | |
826 | .Ve | |
827 | .PP | |
828 | The following options are available: | |
829 | .IP "default" 12 | |
830 | .IX Item "default" | |
831 | This option causes all configuration options to be reset to their | |
832 | default values. | |
833 | .IP "posix_default" 12 | |
834 | .IX Item "posix_default" | |
835 | This option causes all configuration options to be reset to their | |
836 | default values as if the environment variable \s-1POSIXLY_CORRECT\s0 had | |
837 | been set. | |
838 | .IP "auto_abbrev" 12 | |
839 | .IX Item "auto_abbrev" | |
840 | Allow option names to be abbreviated to uniqueness. | |
841 | Default is enabled unless environment variable | |
842 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`auto_abbrev\*(C'\fR is disabled. | |
843 | .IP "getopt_compat" 12 | |
844 | .IX Item "getopt_compat" | |
845 | Allow \f(CW\*(C`+\*(C'\fR to start options. | |
846 | Default is enabled unless environment variable | |
847 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`getopt_compat\*(C'\fR is disabled. | |
848 | .IP "gnu_compat" 12 | |
849 | .IX Item "gnu_compat" | |
850 | \&\f(CW\*(C`gnu_compat\*(C'\fR controls whether \f(CW\*(C`\-\-opt=\*(C'\fR is allowed, and what it should | |
851 | 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, | |
852 | \&\f(CW\*(C`\-\-opt=\*(C'\fR will give option \f(CW\*(C`opt\*(C'\fR and empty value. | |
853 | This is the way \s-1GNU\s0 \fIgetopt_long()\fR does it. | |
854 | .IP "gnu_getopt" 12 | |
855 | .IX Item "gnu_getopt" | |
856 | 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 | |
857 | \&\f(CW\*(C`no_getopt_compat\*(C'\fR. With \f(CW\*(C`gnu_getopt\*(C'\fR, command line handling should be | |
858 | fully compatible with \s-1GNU\s0 \fIgetopt_long()\fR. | |
859 | .IP "require_order" 12 | |
860 | .IX Item "require_order" | |
861 | Whether command line arguments are allowed to be mixed with options. | |
862 | Default is disabled unless environment variable | |
863 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`require_order\*(C'\fR is enabled. | |
864 | .Sp | |
865 | See also \f(CW\*(C`permute\*(C'\fR, which is the opposite of \f(CW\*(C`require_order\*(C'\fR. | |
866 | .IP "permute" 12 | |
867 | .IX Item "permute" | |
868 | Whether command line arguments are allowed to be mixed with options. | |
869 | Default is enabled unless environment variable | |
870 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case \f(CW\*(C`permute\*(C'\fR is disabled. | |
871 | Note that \f(CW\*(C`permute\*(C'\fR is the opposite of \f(CW\*(C`require_order\*(C'\fR. | |
872 | .Sp | |
873 | If \f(CW\*(C`permute\*(C'\fR is enabled, this means that | |
874 | .Sp | |
875 | .Vb 1 | |
876 | \& --foo arg1 --bar arg2 arg3 | |
877 | .Ve | |
878 | .Sp | |
879 | is equivalent to | |
880 | .Sp | |
881 | .Vb 1 | |
882 | \& --foo --bar arg1 arg2 arg3 | |
883 | .Ve | |
884 | .Sp | |
885 | If an argument callback routine is specified, \f(CW@ARGV\fR will always be | |
886 | empty upon succesful return of \fIGetOptions()\fR since all options have been | |
887 | processed. The only exception is when \f(CW\*(C`\-\-\*(C'\fR is used: | |
888 | .Sp | |
889 | .Vb 1 | |
890 | \& --foo arg1 --bar arg2 -- arg3 | |
891 | .Ve | |
892 | .Sp | |
893 | This will call the callback routine for arg1 and arg2, and then | |
894 | terminate \fIGetOptions()\fR leaving \f(CW"arg2"\fR in \f(CW@ARGV\fR. | |
895 | .Sp | |
896 | If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing | |
897 | terminates when the first non-option is encountered. | |
898 | .Sp | |
899 | .Vb 1 | |
900 | \& --foo arg1 --bar arg2 arg3 | |
901 | .Ve | |
902 | .Sp | |
903 | is equivalent to | |
904 | .Sp | |
905 | .Vb 1 | |
906 | \& --foo -- arg1 --bar arg2 arg3 | |
907 | .Ve | |
908 | .Sp | |
909 | If \f(CW\*(C`pass_through\*(C'\fR is also enabled, options processing will terminate | |
910 | at the first unrecognized option, or non\-option, whichever comes | |
911 | first. | |
912 | .IP "bundling (default: disabled)" 12 | |
913 | .IX Item "bundling (default: disabled)" | |
914 | Enabling this option will allow single-character options to be | |
915 | bundled. To distinguish bundles from long option names, long options | |
916 | \&\fImust\fR be introduced with \f(CW\*(C`\-\-\*(C'\fR and bundles with \f(CW\*(C`\-\*(C'\fR. | |
917 | .Sp | |
918 | 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 | |
919 | auto_abbrev enabled, possible arguments and option settings are: | |
920 | .Sp | |
921 | .Vb 6 | |
922 | \& using argument sets option(s) | |
923 | \& ------------------------------------------ | |
924 | \& -a, --a a | |
925 | \& -l, --l l | |
926 | \& -al, -la, -ala, -all,... a, l | |
927 | \& --al, --all all | |
928 | .Ve | |
929 | .Sp | |
930 | The suprising part is that \f(CW\*(C`\-\-a\*(C'\fR sets option \f(CW\*(C`a\*(C'\fR (due to auto | |
931 | completion), not \f(CW\*(C`all\*(C'\fR. | |
932 | .Sp | |
933 | Note: disabling \f(CW\*(C`bundling\*(C'\fR also disables \f(CW\*(C`bundling_override\*(C'\fR. | |
934 | .IP "bundling_override (default: disabled)" 12 | |
935 | .IX Item "bundling_override (default: disabled)" | |
936 | If \f(CW\*(C`bundling_override\*(C'\fR is enabled, bundling is enabled as with | |
937 | \&\f(CW\*(C`bundling\*(C'\fR but now long option names override option bundles. | |
938 | .Sp | |
939 | Note: disabling \f(CW\*(C`bundling_override\*(C'\fR also disables \f(CW\*(C`bundling\*(C'\fR. | |
940 | .Sp | |
941 | \&\fBNote:\fR Using option bundling can easily lead to unexpected results, | |
942 | especially when mixing long options and bundles. Caveat emptor. | |
943 | .IP "ignore_case (default: enabled)" 12 | |
944 | .IX Item "ignore_case (default: enabled)" | |
945 | If enabled, case is ignored when matching long option names. If, | |
946 | however, bundling is enabled as well, single character options will be | |
947 | treated case\-sensitive. | |
948 | .Sp | |
949 | With \f(CW\*(C`ignore_case\*(C'\fR, option specifications for options that only | |
950 | differ in case, e.g., \f(CW"foo"\fR and \f(CW"Foo"\fR, will be flagged as | |
951 | duplicates. | |
952 | .Sp | |
953 | Note: disabling \f(CW\*(C`ignore_case\*(C'\fR also disables \f(CW\*(C`ignore_case_always\*(C'\fR. | |
954 | .IP "ignore_case_always (default: disabled)" 12 | |
955 | .IX Item "ignore_case_always (default: disabled)" | |
956 | When bundling is in effect, case is ignored on single-character | |
957 | options also. | |
958 | .Sp | |
959 | Note: disabling \f(CW\*(C`ignore_case_always\*(C'\fR also disables \f(CW\*(C`ignore_case\*(C'\fR. | |
960 | .IP "pass_through (default: disabled)" 12 | |
961 | .IX Item "pass_through (default: disabled)" | |
962 | Options that are unknown, ambiguous or supplied with an invalid option | |
963 | value are passed through in \f(CW@ARGV\fR instead of being flagged as | |
964 | errors. This makes it possible to write wrapper scripts that process | |
965 | only part of the user supplied command line arguments, and pass the | |
966 | remaining options to some other program. | |
967 | .Sp | |
968 | If \f(CW\*(C`require_order\*(C'\fR is enabled, options processing will terminate at | |
969 | the first unrecognized option, or non\-option, whichever comes first. | |
970 | However, if \f(CW\*(C`permute\*(C'\fR is enabled instead, results can become confusing. | |
971 | .IP "prefix" 12 | |
972 | .IX Item "prefix" | |
973 | The string that starts options. If a constant string is not | |
974 | sufficient, see \f(CW\*(C`prefix_pattern\*(C'\fR. | |
975 | .IP "prefix_pattern" 12 | |
976 | .IX Item "prefix_pattern" | |
977 | A Perl pattern that identifies the strings that introduce options. | |
978 | Default is \f(CW\*(C`(\-\-|\-|\e+)\*(C'\fR unless environment variable | |
979 | \&\s-1POSIXLY_CORRECT\s0 has been set, in which case it is \f(CW\*(C`(\-\-|\-)\*(C'\fR. | |
980 | .IP "debug (default: disabled)" 12 | |
981 | .IX Item "debug (default: disabled)" | |
982 | Enable debugging output. | |
983 | .SH "Return values and Errors" | |
984 | .IX Header "Return values and Errors" | |
985 | Configuration errors and errors in the option definitions are | |
986 | signalled using \fIdie()\fR and will terminate the calling program unless | |
987 | the call to \fIGetopt::Long::GetOptions()\fR was embedded in \f(CW\*(C`eval { ... | |
988 | }\*(C'\fR, or \fIdie()\fR was trapped using \f(CW$SIG{_\|_DIE_\|_}\fR. | |
989 | .PP | |
990 | GetOptions returns true to indicate success. | |
991 | It returns false when the function detected one or more errors during | |
992 | option parsing. These errors are signalled using \fIwarn()\fR and can be | |
993 | trapped with \f(CW$SIG{_\|_WARN_\|_}\fR. | |
994 | .PP | |
995 | Errors that can't happen are signalled using \fICarp::croak()\fR. | |
996 | .SH "Legacy" | |
997 | .IX Header "Legacy" | |
998 | The earliest development of \f(CW\*(C`newgetopt.pl\*(C'\fR started in 1990, with Perl | |
999 | version 4. As a result, its development, and the development of | |
1000 | Getopt::Long, has gone through several stages. Since backward | |
1001 | compatibility has always been extremely important, the current version | |
1002 | of Getopt::Long still supports a lot of constructs that nowadays are | |
1003 | no longer necessary or otherwise unwanted. This section describes | |
1004 | briefly some of these 'features'. | |
1005 | .Sh "Default destinations" | |
1006 | .IX Subsection "Default destinations" | |
1007 | When no destination is specified for an option, GetOptions will store | |
1008 | the resultant value in a global variable named \f(CW\*(C`opt_\*(C'\fR\fI\s-1XXX\s0\fR, where | |
1009 | \&\fI\s-1XXX\s0\fR is the primary name of this option. When a progam executes | |
1010 | under \f(CW\*(C`use strict\*(C'\fR (recommended), these variables must be | |
1011 | pre-declared with \fIour()\fR or \f(CW\*(C`use vars\*(C'\fR. | |
1012 | .PP | |
1013 | .Vb 2 | |
1014 | \& our $opt_length = 0; | |
1015 | \& GetOptions ('length=i'); # will store in $opt_length | |
1016 | .Ve | |
1017 | .PP | |
1018 | To yield a usable Perl variable, characters that are not part of the | |
1019 | syntax for variables are translated to underscores. For example, | |
1020 | \&\f(CW\*(C`\-\-fpp\-struct\-return\*(C'\fR will set the variable | |
1021 | \&\f(CW$opt_fpp_struct_return\fR. Note that this variable resides in the | |
1022 | namespace of the calling program, not necessarily \f(CW\*(C`main\*(C'\fR. For | |
1023 | example: | |
1024 | .PP | |
1025 | .Vb 1 | |
1026 | \& GetOptions ("size=i", "sizes=i@"); | |
1027 | .Ve | |
1028 | .PP | |
1029 | with command line \*(L"\-size 10 \-sizes 24 \-sizes 48\*(R" will perform the | |
1030 | equivalent of the assignments | |
1031 | .PP | |
1032 | .Vb 2 | |
1033 | \& $opt_size = 10; | |
1034 | \& @opt_sizes = (24, 48); | |
1035 | .Ve | |
1036 | .Sh "Alternative option starters" | |
1037 | .IX Subsection "Alternative option starters" | |
1038 | A string of alternative option starter characters may be passed as the | |
1039 | first argument (or the first argument after a leading hash reference | |
1040 | argument). | |
1041 | .PP | |
1042 | .Vb 2 | |
1043 | \& my $len = 0; | |
1044 | \& GetOptions ('/', 'length=i' => $len); | |
1045 | .Ve | |
1046 | .PP | |
1047 | Now the command line may look like: | |
1048 | .PP | |
1049 | .Vb 1 | |
1050 | \& /length 24 -- arg | |
1051 | .Ve | |
1052 | .PP | |
1053 | Note that to terminate options processing still requires a double dash | |
1054 | \&\f(CW\*(C`\-\-\*(C'\fR. | |
1055 | .PP | |
1056 | \&\fIGetOptions()\fR will not interpret a leading \f(CW"<>"\fR as option starters | |
1057 | if the next argument is a reference. To force \f(CW"<"\fR and \f(CW">"\fR as | |
1058 | option starters, use \f(CW"><"\fR. Confusing? Well, \fBusing a starter | |
1059 | argument is strongly deprecated\fR anyway. | |
1060 | .Sh "Configuration variables" | |
1061 | .IX Subsection "Configuration variables" | |
1062 | Previous versions of Getopt::Long used variables for the purpose of | |
1063 | configuring. Although manipulating these variables still work, it is | |
1064 | strongly encouraged to use the \f(CW\*(C`Configure\*(C'\fR routine that was introduced | |
1065 | in version 2.17. Besides, it is much easier. | |
1066 | .SH "Trouble Shooting" | |
1067 | .IX Header "Trouble Shooting" | |
1068 | .Sh "Warning: Ignoring '!' modifier for short option" | |
1069 | .IX Subsection "Warning: Ignoring '!' modifier for short option" | |
1070 | This warning is issued when the '!' modifier is applied to a short | |
1071 | (one\-character) option and bundling is in effect. E.g., | |
1072 | .PP | |
1073 | .Vb 2 | |
1074 | \& Getopt::Long::Configure("bundling"); | |
1075 | \& GetOptions("foo|f!" => \e$foo); | |
1076 | .Ve | |
1077 | .PP | |
1078 | Note that older Getopt::Long versions did not issue a warning, because | |
1079 | the '!' modifier was applied to the first name only. This bug was | |
1080 | fixed in 2.22. | |
1081 | .PP | |
1082 | Solution: separate the long and short names and apply the '!' to the | |
1083 | long names only, e.g., | |
1084 | .PP | |
1085 | .Vb 1 | |
1086 | \& GetOptions("foo!" => \e$foo, "f" => \e$foo); | |
1087 | .Ve | |
1088 | .Sh "GetOptions does not return a false result when an option is not supplied" | |
1089 | .IX Subsection "GetOptions does not return a false result when an option is not supplied" | |
1090 | That's why they're called 'options'. | |
1091 | .Sh "GetOptions does not split the command line correctly" | |
1092 | .IX Subsection "GetOptions does not split the command line correctly" | |
1093 | The command line is not split by GetOptions, but by the command line | |
1094 | interpreter (\s-1CLI\s0). On Unix, this is the shell. On Windows, it is | |
1095 | \&\s-1COMMAND\s0.COM or \s-1CMD\s0.EXE. Other operating systems have other CLIs. | |
1096 | .PP | |
1097 | It is important to know that these CLIs may behave different when the | |
1098 | command line contains special characters, in particular quotes or | |
1099 | backslashes. For example, with Unix shells you can use single quotes | |
1100 | (\f(CW\*(C`'\*(C'\fR) and double quotes (\f(CW\*(C`"\*(C'\fR) to group words together. The following | |
1101 | alternatives are equivalent on Unix: | |
1102 | .PP | |
1103 | .Vb 3 | |
1104 | \& "two words" | |
1105 | \& 'two words' | |
1106 | \& two\e words | |
1107 | .Ve | |
1108 | .PP | |
1109 | In case of doubt, insert the following statement in front of your Perl | |
1110 | program: | |
1111 | .PP | |
1112 | .Vb 1 | |
1113 | \& print STDERR (join("|",@ARGV),"\en"); | |
1114 | .Ve | |
1115 | .PP | |
1116 | to verify how your \s-1CLI\s0 passes the arguments to the program. | |
1117 | .ie n .Sh "How do I put a ""\-?"" option into a Getopt::Long?" | |
1118 | .el .Sh "How do I put a ``\-?'' option into a Getopt::Long?" | |
1119 | .IX Subsection "How do I put a -? option into a Getopt::Long?" | |
1120 | You can only obtain this using an alias, and Getopt::Long of at least | |
1121 | version 2.13. | |
1122 | .PP | |
1123 | .Vb 2 | |
1124 | \& use Getopt::Long; | |
1125 | \& GetOptions ("help|?"); # -help and -? will both set $opt_help | |
1126 | .Ve | |
1127 | .SH "AUTHOR" | |
1128 | .IX Header "AUTHOR" | |
1129 | Johan Vromans <jvromans@squirrel.nl> | |
1130 | .SH "COPYRIGHT AND DISCLAIMER" | |
1131 | .IX Header "COPYRIGHT AND DISCLAIMER" | |
1132 | This program is Copyright 2002,1990 by Johan Vromans. | |
1133 | This program is free software; you can redistribute it and/or | |
1134 | modify it under the terms of the Perl Artistic License or the | |
1135 | \&\s-1GNU\s0 General Public License as published by the Free Software | |
1136 | Foundation; either version 2 of the License, or (at your option) any | |
1137 | later version. | |
1138 | .PP | |
1139 | This program is distributed in the hope that it will be useful, | |
1140 | but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of | |
1141 | \&\s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the | |
1142 | \&\s-1GNU\s0 General Public License for more details. | |
1143 | .PP | |
1144 | If you do not have a copy of the \s-1GNU\s0 General Public License write to | |
1145 | the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, | |
1146 | \&\s-1MA\s0 02139, \s-1USA\s0. |