]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getopt.3
crypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / getopt.3
CommitLineData
bf5a7247 1.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
fea681da 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
c08df37a 24.\"
fea681da
MK
25.\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
26.\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
27.\" longindex is a pointer, has_arg can take 3 values, using consistent
28.\" names for optstring and longindex, "\n" in formats fixed. Documenting
29.\" opterr and getopt_long_only. Clarified explanations (borrowing heavily
30.\" from the source code).
31.\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
32.\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
33.\" says; moreover, EOF is not defined in <unistd.h>.
f74bac5d 34.\" Modified 2002-02-16, joey: added information about nonexistent
fea681da 35.\" option character and colon as first option character
c11b1abf 36.\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
37.\" Added text to explain how to order both '[-+]' and ':' at
38.\" the start of optstring
92e014f6 39.\" Modified 2006-12-15, mtk, Added getopt() example program.
fea681da 40.\"
4b8c67d9 41.TH GETOPT 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da 42.SH NAME
57db674d
MK
43getopt, getopt_long, getopt_long_only,
44optarg, optind, opterr, optopt \- Parse command-line options
fea681da
MK
45.SH SYNOPSIS
46.nf
47.B #include <unistd.h>
68e4db0a 48.PP
fea681da
MK
49.BI "int getopt(int " argc ", char * const " argv[] ,
50.BI " const char *" optstring );
68e4db0a 51.PP
fea681da
MK
52.BI "extern char *" optarg ;
53.BI "extern int " optind ", " opterr ", " optopt ;
68e4db0a 54.PP
fea681da 55.B #include <getopt.h>
68e4db0a 56.PP
fea681da
MK
57.BI "int getopt_long(int " argc ", char * const " argv[] ,
58.BI " const char *" optstring ,
59.BI " const struct option *" longopts ", int *" longindex );
68e4db0a 60.PP
fea681da
MK
61.BI "int getopt_long_only(int " argc ", char * const " argv[] ,
62.BI " const char *" optstring ,
63.BI " const struct option *" longopts ", int *" longindex );
64.fi
68e4db0a 65.PP
0f200f07
MK
66.in -4n
67Feature Test Macro Requirements for glibc (see
68.BR feature_test_macros (7)):
69.ad l
70.in
68e4db0a 71.PP
0f200f07 72.BR getopt ():
0b8ff556 73_POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
0f200f07
MK
74.br
75.BR getopt_long (),
76.BR getopt_long_only ():
77_GNU_SOURCE
78.ad b
fea681da
MK
79.SH DESCRIPTION
80The
f10e4102 81.BR getopt ()
c13182ef
MK
82function parses the command-line arguments.
83Its arguments
fea681da
MK
84.I argc
85and
86.I argv
87are the argument count and array as passed to the
63aa9df0 88.IR main ()
fea681da 89function on program invocation.
f81fb444 90An element of \fIargv\fP that starts with \(aq\-\(aq
fea681da 91(and is not exactly "\-" or "\-\-")
c13182ef
MK
92is an option element.
93The characters of this element
f81fb444 94(aside from the initial \(aq\-\(aq) are option characters.
60a90ecd
MK
95If
96.BR getopt ()
fea681da
MK
97is called repeatedly, it returns successively each of the option characters
98from each of the option elements.
99.PP
66b08231
MK
100The variable
101.I optind
102is the index of the next element to be processed in
103.IR argv .
104The system initializes this value to 1.
105The caller can reset it to 1 to restart scanning of the same
106.IR argv ,
107or when scanning a new argument vector.
108.PP
60a90ecd
MK
109If
110.BR getopt ()
111finds another option character, it returns that
fea681da 112character, updating the external variable \fIoptind\fP and a static
60a90ecd
MK
113variable \fInextchar\fP so that the next call to
114.BR getopt ()
115can
fea681da
MK
116resume the scan with the following option character or
117\fIargv\fP-element.
118.PP
60a90ecd
MK
119If there are no more option characters,
120.BR getopt ()
121returns \-1.
fea681da
MK
122Then \fIoptind\fP is the index in \fIargv\fP of the first
123\fIargv\fP-element that is not an option.
124.PP
125.I optstring
c13182ef
MK
126is a string containing the legitimate option characters.
127If such a
fea681da 128character is followed by a colon, the option requires an argument, so
60a90ecd
MK
129.BR getopt ()
130places a pointer to the following text in the same
fea681da
MK
131\fIargv\fP-element, or the text of the following \fIargv\fP-element, in
132.IR optarg .
133Two colons mean an option takes
e6bcc128 134an optional arg; if there is text in the current \fIargv\fP-element
f262c004 135(i.e., in the same word as the option name itself, for example, "\-oarg"),
e6bcc128 136then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero.
c13182ef
MK
137This is a GNU extension.
138If
fea681da
MK
139.I optstring
140contains
141.B W
142followed by a semicolon, then
143.B \-W foo
144is treated as the long option
145.BR \-\-foo .
146(The
147.B \-W
148option is reserved by POSIX.2 for implementation extensions.)
d9bfdb9c 149This behavior is a GNU extension, not available with libraries before
5260fe08 150glibc 2.
fea681da 151.PP
60a90ecd
MK
152By default,
153.BR getopt ()
154permutes the contents of \fIargv\fP as it
24b74457 155scans, so that eventually all the nonoptions are at the end.
c13182ef
MK
156Two other modes are also implemented.
157If the first character of
f81fb444 158\fIoptstring\fP is \(aq+\(aq or the environment variable
d9a10d9d 159.B POSIXLY_CORRECT
24b74457 160is set, then option processing stops as soon as a nonoption argument is
c13182ef 161encountered.
f81fb444 162If the first character of \fIoptstring\fP is \(aq\-\(aq, then
24b74457 163each nonoption \fIargv\fP-element is handled as if it were the argument of
fea681da
MK
164an option with character code 1. (This is used by programs that were
165written to expect options and other \fIargv\fP-elements in any order
166and that care about the ordering of the two.)
167The special argument "\-\-" forces an end of option-scanning regardless
168of the scanning mode.
169.PP
a4746c39
MK
170While processing the option list,
171.BR getopt ()
172can detect two kinds of errors:
173(1) an option character that was not specified in
174.IR optstring
175and (2) a missing option argument
176(i.e., an option at the end of the command line without an expected argument).
177Such errors are handled and reported as follows:
178.IP * 3
179By default,
60a90ecd 180.BR getopt ()
a4746c39
MK
181prints an error message on standard error,
182places the erroneous option character in
183.IR optopt ,
184and returns \(aq?\(aq as the function result.
185.IP *
186If the caller has set the global variable
187.IR opterr
188to zero, then
189.BR getopt ()
190does not print an error message.
191The caller can determine that there was an error by testing whether
192the function return value is \(aq?\(aq.
193(By default,
194.IR opterr
195has a nonzero value.)
196.IP *
c13182ef 197If the first character
f81fb444 198(following any optional \(aq+\(aq or \(aq\-\(aq described above)
fea681da 199of \fIoptstring\fP
f81fb444 200is a colon (\(aq:\(aq), then
60a90ecd 201.BR getopt ()
a4746c39
MK
202likewise does not print an error message.
203In addition, it returns \(aq:\(aq instead of \(aq?\(aq to
c13182ef 204indicate a missing option argument.
a4746c39
MK
205This allows the caller to distinguish the two different types of errors.
206.\"
2aeec1f9 207.SS getopt_long() and getopt_long_only()
fea681da 208The
f10e4102 209.BR getopt_long ()
fea681da 210function works like
f10e4102 211.BR getopt ()
c1ed35a7 212except that it also accepts long options, started with two dashes.
d0ac4fb3
MK
213(If the program accepts only long options, then
214.I optstring
215should be specified as an empty string (""), not NULL.)
fea681da 216Long option names may be abbreviated if the abbreviation is
c13182ef
MK
217unique or is an exact match for some defined option.
218A long option
fea681da
MK
219may take a parameter, of the form
220.B \-\-arg=param
221or
222.BR "\-\-arg param" .
223.PP
224.I longopts
225is a pointer to the first element of an array of
8478ee02 226.I struct option
fea681da 227declared in
8478ee02 228.I <getopt.h>
fea681da 229as
bdd915e2 230.PP
088a639b 231.in +4n
b8302363 232.EX
fea681da 233struct option {
cf448976
MK
234 const char *name;
235 int has_arg;
236 int *flag;
237 int val;
fea681da 238};
b8302363 239.EE
cf448976 240.in
fea681da
MK
241.PP
242The meanings of the different fields are:
243.TP
244.I name
245is the name of the long option.
246.TP
247.I has_arg
248is:
f10e4102
MK
249\fBno_argument\fP (or 0) if the option does not take an argument;
250\fBrequired_argument\fP (or 1) if the option requires an argument; or
fea681da
MK
251\fBoptional_argument\fP (or 2) if the option takes an optional argument.
252.TP
253.I flag
c13182ef
MK
254specifies how results are returned for a long option.
255If \fIflag\fP
60a90ecd
MK
256is NULL, then
257.BR getopt_long ()
7066ef44
MK
258returns \fIval\fP.
259(For example, the calling program may set \fIval\fP to the equivalent short
c13182ef 260option character.)
60a90ecd
MK
261Otherwise,
262.BR getopt_long ()
263returns 0, and
fea681da
MK
264\fIflag\fP points to a variable which is set to \fIval\fP if the
265option is found, but left unchanged if the option is not found.
266.TP
c13182ef 267\fIval\fP
fea681da
MK
268is the value to return, or to load into the variable pointed
269to by \fIflag\fP.
270.PP
7b01461a 271The last element of the array has to be filled with zeros.
fea681da 272.PP
35e21ba7 273If \fIlongindex\fP is not NULL, it
fea681da
MK
274points to a variable which is set to the index of the long option relative to
275.IR longopts .
276.PP
60a90ecd
MK
277.BR getopt_long_only ()
278is like
279.BR getopt_long (),
f81fb444 280but \(aq\-\(aq as well
f8a07a21 281as "\-\-" can indicate a long option.
f81fb444 282If an option that starts with \(aq\-\(aq
f8a07a21 283(not "\-\-") doesn't match a long option, but does match a short option,
c13182ef 284it is parsed as a short option instead.
47297adb 285.SH RETURN VALUE
f10e4102
MK
286If an option was successfully found, then
287.BR getopt ()
288returns the option character.
289If all command-line options have been parsed, then
290.BR getopt ()
291returns \-1.
292If
293.BR getopt ()
294encounters an option character that was not in
295.IR optstring ,
f81fb444 296then \(aq?\(aq is returned.
f10e4102
MK
297If
298.BR getopt ()
299encounters an option with a missing argument,
300then the return value depends on the first character in
301.IR optstring :
f81fb444 302if it is \(aq:\(aq, then \(aq:\(aq is returned; otherwise \(aq?\(aq is returned.
fea681da 303.PP
60a90ecd
MK
304.BR getopt_long ()
305and
306.BR getopt_long_only ()
307also return the option
c13182ef
MK
308character when a short option is recognized.
309For a long option, they
310return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise.
60a90ecd
MK
311Error and \-1 returns are the same as for
312.BR getopt (),
f81fb444 313plus \(aq?\(aq for an
fea681da 314ambiguous match or an extraneous parameter.
d219a1a3 315.SH ENVIRONMENT
fea681da 316.TP
fea681da 317.B POSIXLY_CORRECT
24b74457 318If this is set, then option processing stops as soon as a nonoption
fea681da
MK
319argument is encountered.
320.TP
fea681da
MK
321.B _<PID>_GNU_nonoption_argv_flags_
322This variable was used by
f19a0f03 323.BR bash (1)
5260fe08 3242.0 to communicate to glibc which arguments are the results of
c13182ef 325wildcard expansion and so should not be considered as options.
d9bfdb9c 326This behavior was removed in
f19a0f03 327.BR bash (1)
5260fe08 328version 2.01, but the support remains in glibc.
c7401a75
PH
329.SH ATTRIBUTES
330For an explanation of the terms used in this section, see
331.BR attributes (7).
332.TS
333allbox;
334lbw24 lb lb
335l l l.
336Interface Attribute Value
337T{
338.BR getopt (),
339.BR getopt_long (),
340.BR getopt_long_only ()
5eef415b 341T} Thread safety MT-Unsafe race:getopt env
c7401a75 342.TE
47297adb 343.SH CONFORMING TO
2b2581ee
MK
344.TP
345.BR getopt ():
88490b86 346POSIX.1-2001, POSIX.1-2008, and POSIX.2,
d9a10d9d
MK
347provided the environment variable
348.B POSIXLY_CORRECT
349is set.
1e3ae1b4
MK
350Otherwise, the elements of \fIargv\fP aren't really
351.IR const ,
352because we permute them.
353We pretend they're
354.I const
355in the prototype to be compatible with other systems.
847e0d88 356.IP
e94c735e
MK
357The use of \(aq+\(aq and \(aq\-\(aq in
358.I optstring
359is a GNU extension.
847e0d88 360.IP
2b2581ee
MK
361On some older implementations,
362.BR getopt ()
363was declared in
364.IR <stdio.h> .
365SUSv1 permitted the declaration to appear in either
366.I <unistd.h>
367or
368.IR <stdio.h> .
accc82d2 369POSIX.1-1996 marked the use of
2b2581ee
MK
370.I <stdio.h>
371for this purpose as LEGACY.
accc82d2 372POSIX.1-2001 does not require the declaration to appear in
2b2581ee 373.IR <stdio.h> .
e94c735e
MK
374.TP
375.BR getopt_long "() and " getopt_long_only ():
376These functions are GNU extensions.
377.SH NOTES
378A program that scans multiple argument vectors,
379or rescans the same vector more than once,
380and wants to make use of GNU extensions such as \(aq+\(aq
c2a80b76 381and \(aq\-\(aq at the start of
e94c735e
MK
382.IR optstring ,
383or changes the value of
384.B POSIXLY_CORRECT
385between scans,
386must reinitialize
387.BR getopt ()
388by resetting
389.I optind
390to 0, rather than the traditional value of 1.
391(Resetting to 0 forces the invocation of an internal initialization
392routine that rechecks
393.B POSIXLY_CORRECT
394and checks for GNU extensions in
395.IR optstring .)
fea681da 396.SH EXAMPLE
81e10454 397.SS getopt()
92e014f6
MK
398The following trivial example program uses
399.BR getopt ()
c13182ef 400to handle two program options:
f262c004 401.IR \-n ,
92e014f6 402with no associated value; and
f262c004 403.IR "\-t val" ,
92e014f6 404which expects an associated value.
bdd915e2
MK
405.PP
406.EX
92e014f6
MK
407#include <unistd.h>
408#include <stdlib.h>
409#include <stdio.h>
410
411int
412main(int argc, char *argv[])
413{
414 int flags, opt;
415 int nsecs, tfnd;
416
417 nsecs = 0;
418 tfnd = 0;
419 flags = 0;
29059a65 420 while ((opt = getopt(argc, argv, "nt:")) != \-1) {
92e014f6 421 switch (opt) {
f81fb444 422 case \(aqn\(aq:
92e014f6
MK
423 flags = 1;
424 break;
f81fb444 425 case \(aqt\(aq:
92e014f6
MK
426 nsecs = atoi(optarg);
427 tfnd = 1;
428 break;
f81fb444 429 default: /* \(aq?\(aq */
29059a65 430 fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
cec29a73 431 argv[0]);
92e014f6
MK
432 exit(EXIT_FAILURE);
433 }
434 }
435
26f418da
MK
436 printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\\n",
437 flags, tfnd, nsecs, optind);
92e014f6
MK
438
439 if (optind >= argc) {
440 fprintf(stderr, "Expected argument after options\\n");
441 exit(EXIT_FAILURE);
442 }
443
444 printf("name argument = %s\\n", argv[optind]);
445
446 /* Other code omitted */
447
448 exit(EXIT_SUCCESS);
449}
bdd915e2 450.EX
81e10454 451.SS getopt_long()
fea681da 452The following example program illustrates the use of
f10e4102 453.BR getopt_long ()
fea681da 454with most of its features.
bdd915e2
MK
455.PP
456.EE
fea681da
MK
457#include <stdio.h> /* for printf */
458#include <stdlib.h> /* for exit */
459#include <getopt.h>
e646a1ba 460.PP
fea681da 461int
c13182ef 462main(int argc, char **argv)
41798314 463{
fea681da
MK
464 int c;
465 int digit_optind = 0;
e646a1ba 466.PP
fea681da
MK
467 while (1) {
468 int this_option_optind = optind ? optind : 1;
469 int option_index = 0;
470 static struct option long_options[] = {
e5266ff9
BW
471 {"add", required_argument, 0, 0 },
472 {"append", no_argument, 0, 0 },
473 {"delete", required_argument, 0, 0 },
474 {"verbose", no_argument, 0, 0 },
475 {"create", required_argument, 0, \(aqc\(aq},
476 {"file", required_argument, 0, 0 },
477 {0, 0, 0, 0 }
fea681da 478 };
e646a1ba 479.PP
cf0a9ace 480 c = getopt_long(argc, argv, "abc:d:012",
fea681da
MK
481 long_options, &option_index);
482 if (c == \-1)
483 break;
e646a1ba 484.PP
fea681da
MK
485 switch (c) {
486 case 0:
cf0a9ace 487 printf("option %s", long_options[option_index].name);
fea681da 488 if (optarg)
cf0a9ace
MK
489 printf(" with arg %s", optarg);
490 printf("\\n");
fea681da 491 break;
e646a1ba 492.PP
f81fb444
MK
493 case \(aq0\(aq:
494 case \(aq1\(aq:
495 case \(aq2\(aq:
fea681da 496 if (digit_optind != 0 && digit_optind != this_option_optind)
29059a65 497 printf("digits occur in two different argv\-elements.\\n");
fea681da 498 digit_optind = this_option_optind;
cf0a9ace 499 printf("option %c\\n", c);
fea681da 500 break;
e646a1ba 501.PP
f81fb444 502 case \(aqa\(aq:
cf0a9ace 503 printf("option a\\n");
fea681da 504 break;
e646a1ba 505.PP
f81fb444 506 case \(aqb\(aq:
cf0a9ace 507 printf("option b\\n");
fea681da 508 break;
e646a1ba 509.PP
f81fb444
MK
510 case \(aqc\(aq:
511 printf("option c with value \(aq%s\(aq\\n", optarg);
fea681da 512 break;
e646a1ba 513.PP
f81fb444
MK
514 case \(aqd\(aq:
515 printf("option d with value \(aq%s\(aq\\n", optarg);
fea681da 516 break;
e646a1ba 517.PP
f81fb444 518 case \(aq?\(aq:
fea681da 519 break;
e646a1ba 520.PP
fea681da 521 default:
cf0a9ace 522 printf("?? getopt returned character code 0%o ??\\n", c);
fea681da
MK
523 }
524 }
e646a1ba 525.PP
fea681da 526 if (optind < argc) {
29059a65 527 printf("non\-option ARGV\-elements: ");
fea681da 528 while (optind < argc)
cf0a9ace
MK
529 printf("%s ", argv[optind++]);
530 printf("\\n");
fea681da 531 }
e646a1ba 532.PP
4c44ffe5 533 exit(EXIT_SUCCESS);
fea681da 534}
bdd915e2 535.EE
47297adb 536.SH SEE ALSO
35d82b25 537.BR getopt (1),
0a4f8b7b 538.BR getsubopt (3)