]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/getopt.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / getopt.3
CommitLineData
a1eaacb1 1'\" t
bf5a7247 2.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
af88b3d3 3.\" and Copyright 2006-2008, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
c08df37a 6.\"
fea681da
MK
7.\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
8.\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
9.\" longindex is a pointer, has_arg can take 3 values, using consistent
10.\" names for optstring and longindex, "\n" in formats fixed. Documenting
11.\" opterr and getopt_long_only. Clarified explanations (borrowing heavily
12.\" from the source code).
13.\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
14.\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
15.\" says; moreover, EOF is not defined in <unistd.h>.
f74bac5d 16.\" Modified 2002-02-16, joey: added information about nonexistent
fea681da 17.\" option character and colon as first option character
c11b1abf 18.\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
19.\" Added text to explain how to order both '[-+]' and ':' at
20.\" the start of optstring
92e014f6 21.\" Modified 2006-12-15, mtk, Added getopt() example program.
fea681da 22.\"
4c1c5274 23.TH getopt 3 (date) "Linux man-pages (unreleased)"
fea681da 24.SH NAME
57db674d
MK
25getopt, getopt_long, getopt_long_only,
26optarg, optind, opterr, optopt \- Parse command-line options
42009080
AC
27.SH LIBRARY
28Standard C library
29.RI ( libc ", " \-lc )
fea681da
MK
30.SH SYNOPSIS
31.nf
32.B #include <unistd.h>
c6d039a3 33.P
185ed498 34.BI "int getopt(int " argc ", char *" argv [],
fea681da 35.BI " const char *" optstring );
c6d039a3 36.P
fea681da
MK
37.BI "extern char *" optarg ;
38.BI "extern int " optind ", " opterr ", " optopt ;
c6d039a3 39.P
fea681da 40.B #include <getopt.h>
c6d039a3 41.P
185ed498 42.BI "int getopt_long(int " argc ", char *" argv [],
fea681da
MK
43.BI " const char *" optstring ,
44.BI " const struct option *" longopts ", int *" longindex );
185ed498 45.BI "int getopt_long_only(int " argc ", char *" argv [],
fea681da
MK
46.BI " const char *" optstring ,
47.BI " const struct option *" longopts ", int *" longindex );
48.fi
c6d039a3 49.P
d39ad78f 50.RS -4
0f200f07
MK
51Feature Test Macro Requirements for glibc (see
52.BR feature_test_macros (7)):
d39ad78f 53.RE
c6d039a3 54.P
0f200f07 55.BR getopt ():
9d2adbae 56.nf
5c10d2c5 57 _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE
9d2adbae 58.fi
c6d039a3 59.P
0f200f07
MK
60.BR getopt_long (),
61.BR getopt_long_only ():
9d2adbae
MK
62.nf
63 _GNU_SOURCE
64.fi
fea681da
MK
65.SH DESCRIPTION
66The
f10e4102 67.BR getopt ()
c13182ef
MK
68function parses the command-line arguments.
69Its arguments
fea681da
MK
70.I argc
71and
72.I argv
73are the argument count and array as passed to the
63aa9df0 74.IR main ()
fea681da 75function on program invocation.
b957f81f 76An element of \fIargv\fP that starts with \[aq]\-\[aq]
fea681da 77(and is not exactly "\-" or "\-\-")
c13182ef
MK
78is an option element.
79The characters of this element
b957f81f 80(aside from the initial \[aq]\-\[aq]) are option characters.
60a90ecd
MK
81If
82.BR getopt ()
fea681da
MK
83is called repeatedly, it returns successively each of the option characters
84from each of the option elements.
c6d039a3 85.P
66b08231
MK
86The variable
87.I optind
88is the index of the next element to be processed in
89.IR argv .
90The system initializes this value to 1.
91The caller can reset it to 1 to restart scanning of the same
92.IR argv ,
93or when scanning a new argument vector.
c6d039a3 94.P
60a90ecd
MK
95If
96.BR getopt ()
97finds another option character, it returns that
fea681da 98character, updating the external variable \fIoptind\fP and a static
60a90ecd
MK
99variable \fInextchar\fP so that the next call to
100.BR getopt ()
101can
fea681da
MK
102resume the scan with the following option character or
103\fIargv\fP-element.
c6d039a3 104.P
60a90ecd
MK
105If there are no more option characters,
106.BR getopt ()
107returns \-1.
fea681da
MK
108Then \fIoptind\fP is the index in \fIargv\fP of the first
109\fIargv\fP-element that is not an option.
c6d039a3 110.P
fea681da 111.I optstring
c13182ef 112is a string containing the legitimate option characters.
dfa1ee20
JH
113A legitimate option character is any visible one byte
114.BR ascii (7)
115character (for which
116.BR isgraph (3)
b957f81f 117would return nonzero) that is not \[aq]\-\[aq], \[aq]:\[aq], or \[aq];\[aq].
c13182ef 118If such a
fea681da 119character is followed by a colon, the option requires an argument, so
60a90ecd
MK
120.BR getopt ()
121places a pointer to the following text in the same
fea681da
MK
122\fIargv\fP-element, or the text of the following \fIargv\fP-element, in
123.IR optarg .
124Two colons mean an option takes
e6bcc128 125an optional arg; if there is text in the current \fIargv\fP-element
f262c004 126(i.e., in the same word as the option name itself, for example, "\-oarg"),
e6bcc128 127then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero.
c13182ef
MK
128This is a GNU extension.
129If
fea681da
MK
130.I optstring
131contains
132.B W
133followed by a semicolon, then
134.B \-W foo
135is treated as the long option
136.BR \-\-foo .
137(The
138.B \-W
139option is reserved by POSIX.2 for implementation extensions.)
d9bfdb9c 140This behavior is a GNU extension, not available with libraries before
5260fe08 141glibc 2.
c6d039a3 142.P
60a90ecd
MK
143By default,
144.BR getopt ()
145permutes the contents of \fIargv\fP as it
24b74457 146scans, so that eventually all the nonoptions are at the end.
4f247e78 147Two other scanning modes are also implemented.
c13182ef 148If the first character of
b957f81f 149\fIoptstring\fP is \[aq]+\[aq] or the environment variable
d9a10d9d 150.B POSIXLY_CORRECT
24b74457 151is set, then option processing stops as soon as a nonoption argument is
c13182ef 152encountered.
b957f81f 153If \[aq]+\[aq] is not the first character of
87ce9492
JH
154.IR optstring ,
155it is treated as a normal option.
156If
157.B POSIXLY_CORRECT
158behaviour is required in this case
159.I optstring
b957f81f
AC
160will contain two \[aq]+\[aq] symbols.
161If the first character of \fIoptstring\fP is \[aq]\-\[aq], then
24b74457 162each nonoption \fIargv\fP-element is handled as if it were the argument of
5e833e27
MK
163an option with character code 1.
164(This is used by programs that were
fea681da
MK
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.
c6d039a3 169.P
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
1ae6b2c7 174.I optstring
a4746c39
MK
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:
cdede5cd 178.IP \[bu] 3
a4746c39 179By default,
60a90ecd 180.BR getopt ()
a4746c39
MK
181prints an error message on standard error,
182places the erroneous option character in
183.IR optopt ,
b957f81f 184and returns \[aq]?\[aq] as the function result.
cdede5cd 185.IP \[bu]
a4746c39 186If the caller has set the global variable
1ae6b2c7 187.I opterr
a4746c39
MK
188to zero, then
189.BR getopt ()
190does not print an error message.
191The caller can determine that there was an error by testing whether
b957f81f 192the function return value is \[aq]?\[aq].
a4746c39 193(By default,
1ae6b2c7 194.I opterr
a4746c39 195has a nonzero value.)
cdede5cd 196.IP \[bu]
c13182ef 197If the first character
b957f81f 198(following any optional \[aq]+\[aq] or \[aq]\-\[aq] described above)
fea681da 199of \fIoptstring\fP
b957f81f 200is a colon (\[aq]:\[aq]), then
60a90ecd 201.BR getopt ()
a4746c39 202likewise does not print an error message.
b957f81f 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" .
c6d039a3 223.P
fea681da
MK
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
c6d039a3 230.P
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
c6d039a3 241.P
fea681da
MK
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.
c6d039a3 270.P
7b01461a 271The last element of the array has to be filled with zeros.
c6d039a3 272.P
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 .
c6d039a3 276.P
60a90ecd
MK
277.BR getopt_long_only ()
278is like
279.BR getopt_long (),
b957f81f 280but \[aq]\-\[aq] as well
f8a07a21 281as "\-\-" can indicate a long option.
b957f81f 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 ,
b957f81f 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 :
d6c1998e
AC
302if it is \[aq]:\[aq], then \[aq]:\[aq] is returned;
303otherwise \[aq]?\[aq] is returned.
c6d039a3 304.P
60a90ecd
MK
305.BR getopt_long ()
306and
307.BR getopt_long_only ()
308also return the option
c13182ef
MK
309character when a short option is recognized.
310For a long option, they
311return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise.
60a90ecd
MK
312Error and \-1 returns are the same as for
313.BR getopt (),
b957f81f 314plus \[aq]?\[aq] for an
fea681da 315ambiguous match or an extraneous parameter.
d219a1a3 316.SH ENVIRONMENT
fea681da 317.TP
fea681da 318.B POSIXLY_CORRECT
24b74457 319If this is set, then option processing stops as soon as a nonoption
fea681da
MK
320argument is encountered.
321.TP
fea681da
MK
322.B _<PID>_GNU_nonoption_argv_flags_
323This variable was used by
f19a0f03 324.BR bash (1)
5260fe08 3252.0 to communicate to glibc which arguments are the results of
c13182ef 326wildcard expansion and so should not be considered as options.
d9bfdb9c 327This behavior was removed in
f19a0f03 328.BR bash (1)
b324e17d 3292.01, but the support remains in glibc.
c7401a75
PH
330.SH ATTRIBUTES
331For an explanation of the terms used in this section, see
332.BR attributes (7).
333.TS
334allbox;
b32feea5 335lb lb lbx
c7401a75
PH
336l l l.
337Interface Attribute Value
338T{
9e54434e
BR
339.na
340.nh
c7401a75
PH
341.BR getopt (),
342.BR getopt_long (),
343.BR getopt_long_only ()
b32feea5 344T} Thread safety T{
9e54434e
BR
345.na
346.nh
b32feea5
MK
347MT-Unsafe race:getopt env
348T}
c7401a75 349.TE
4131356c 350.SH VERSIONS
185ed498
AC
351POSIX specifies that the
352.I argv
353array argument should be
354.IR const ,
355but these functions permute its elements
356unless the environment variable
d9a10d9d
MK
357.B POSIXLY_CORRECT
358is set.
1e3ae1b4 359.I const
185ed498
AC
360is used in the actual prototype to be compatible with other systems;
361however, this page doesn't show the qualifier,
362to avoid confusing readers.
4131356c
AC
363.SH STANDARDS
364.TP
365.BR getopt ()
366POSIX.1-2008.
367.TP
368.BR getopt_long ()
369.TQ
370.BR getopt_long_only ()
371GNU.
847e0d88 372.IP
b957f81f 373The use of \[aq]+\[aq] and \[aq]\-\[aq] in
e94c735e
MK
374.I optstring
375is a GNU extension.
4131356c
AC
376.SH HISTORY
377.TP
378.BR getopt ()
379POSIX.1-2001, and POSIX.2.
c6d039a3 380.P
2b2581ee
MK
381On some older implementations,
382.BR getopt ()
383was declared in
384.IR <stdio.h> .
385SUSv1 permitted the declaration to appear in either
386.I <unistd.h>
387or
388.IR <stdio.h> .
accc82d2 389POSIX.1-1996 marked the use of
2b2581ee
MK
390.I <stdio.h>
391for this purpose as LEGACY.
accc82d2 392POSIX.1-2001 does not require the declaration to appear in
2b2581ee 393.IR <stdio.h> .
e94c735e
MK
394.SH NOTES
395A program that scans multiple argument vectors,
396or rescans the same vector more than once,
b957f81f
AC
397and wants to make use of GNU extensions such as \[aq]+\[aq]
398and \[aq]\-\[aq] at the start of
e94c735e
MK
399.IR optstring ,
400or changes the value of
401.B POSIXLY_CORRECT
402between scans,
403must reinitialize
404.BR getopt ()
405by resetting
406.I optind
407to 0, rather than the traditional value of 1.
408(Resetting to 0 forces the invocation of an internal initialization
409routine that rechecks
410.B POSIXLY_CORRECT
411and checks for GNU extensions in
412.IR optstring .)
c6d039a3 413.P
dfa1ee20
JH
414Command-line arguments are parsed in strict order
415meaning that an option requiring an argument will consume the next argument,
416regardless of whether that argument is the correctly specified option argument
417or simply the next option
418(in the scenario the user mis-specifies the command line).
419For example, if
420.I optstring
421is specified as "1n:"
422and the user specifies the command line arguments incorrectly as
423.IR "prog\ \-n\ \-1" ,
424the
425.I \-n
426option will be given the
427.B optarg
428value "\-1", and the
429.I \-1
430option will be considered to have not been specified.
a14af333 431.SH EXAMPLES
81e10454 432.SS getopt()
92e014f6
MK
433The following trivial example program uses
434.BR getopt ()
c13182ef 435to handle two program options:
f262c004 436.IR \-n ,
92e014f6 437with no associated value; and
f262c004 438.IR "\-t val" ,
92e014f6 439which expects an associated value.
c6d039a3 440.P
b0b6ab4e 441.\" SRC BEGIN (getopt.c)
bdd915e2 442.EX
92e014f6 443#include <stdio.h>
ad3868f0
AC
444#include <stdlib.h>
445#include <unistd.h>
fe5dba13 446\&
92e014f6
MK
447int
448main(int argc, char *argv[])
449{
450 int flags, opt;
451 int nsecs, tfnd;
fe5dba13 452\&
92e014f6
MK
453 nsecs = 0;
454 tfnd = 0;
455 flags = 0;
29059a65 456 while ((opt = getopt(argc, argv, "nt:")) != \-1) {
92e014f6 457 switch (opt) {
b957f81f 458 case \[aq]n\[aq]:
92e014f6
MK
459 flags = 1;
460 break;
b957f81f 461 case \[aq]t\[aq]:
92e014f6
MK
462 nsecs = atoi(optarg);
463 tfnd = 1;
464 break;
b957f81f 465 default: /* \[aq]?\[aq] */
d1a71985 466 fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\en",
cec29a73 467 argv[0]);
92e014f6
MK
468 exit(EXIT_FAILURE);
469 }
470 }
fe5dba13 471\&
d1a71985 472 printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\en",
d917c31d 473 flags, tfnd, nsecs, optind);
fe5dba13 474\&
92e014f6 475 if (optind >= argc) {
d1a71985 476 fprintf(stderr, "Expected argument after options\en");
92e014f6
MK
477 exit(EXIT_FAILURE);
478 }
fe5dba13 479\&
d1a71985 480 printf("name argument = %s\en", argv[optind]);
fe5dba13 481\&
92e014f6 482 /* Other code omitted */
fe5dba13 483\&
92e014f6
MK
484 exit(EXIT_SUCCESS);
485}
66af361c 486.EE
b0b6ab4e 487.\" SRC END
81e10454 488.SS getopt_long()
fea681da 489The following example program illustrates the use of
f10e4102 490.BR getopt_long ()
fea681da 491with most of its features.
c6d039a3 492.P
b0b6ab4e 493.\" SRC BEGIN (getopt_long.c)
66af361c 494.EX
ad3868f0 495#include <getopt.h>
fea681da
MK
496#include <stdio.h> /* for printf */
497#include <stdlib.h> /* for exit */
fe5dba13 498\&
fea681da 499int
aa1f53cc 500main(int argc, char *argv[])
41798314 501{
fea681da
MK
502 int c;
503 int digit_optind = 0;
fe5dba13 504\&
fea681da
MK
505 while (1) {
506 int this_option_optind = optind ? optind : 1;
507 int option_index = 0;
508 static struct option long_options[] = {
e5266ff9
BW
509 {"add", required_argument, 0, 0 },
510 {"append", no_argument, 0, 0 },
511 {"delete", required_argument, 0, 0 },
512 {"verbose", no_argument, 0, 0 },
b957f81f 513 {"create", required_argument, 0, \[aq]c\[aq]},
e5266ff9
BW
514 {"file", required_argument, 0, 0 },
515 {0, 0, 0, 0 }
fea681da 516 };
fe5dba13 517\&
cf0a9ace 518 c = getopt_long(argc, argv, "abc:d:012",
d917c31d 519 long_options, &option_index);
fea681da
MK
520 if (c == \-1)
521 break;
fe5dba13 522\&
fea681da
MK
523 switch (c) {
524 case 0:
cf0a9ace 525 printf("option %s", long_options[option_index].name);
fea681da 526 if (optarg)
cf0a9ace 527 printf(" with arg %s", optarg);
d1a71985 528 printf("\en");
fea681da 529 break;
fe5dba13 530\&
b957f81f
AC
531 case \[aq]0\[aq]:
532 case \[aq]1\[aq]:
533 case \[aq]2\[aq]:
fea681da 534 if (digit_optind != 0 && digit_optind != this_option_optind)
d1a71985 535 printf("digits occur in two different argv\-elements.\en");
fea681da 536 digit_optind = this_option_optind;
d1a71985 537 printf("option %c\en", c);
fea681da 538 break;
fe5dba13 539\&
b957f81f 540 case \[aq]a\[aq]:
d1a71985 541 printf("option a\en");
fea681da 542 break;
fe5dba13 543\&
b957f81f 544 case \[aq]b\[aq]:
d1a71985 545 printf("option b\en");
fea681da 546 break;
fe5dba13 547\&
b957f81f
AC
548 case \[aq]c\[aq]:
549 printf("option c with value \[aq]%s\[aq]\en", optarg);
fea681da 550 break;
fe5dba13 551\&
b957f81f
AC
552 case \[aq]d\[aq]:
553 printf("option d with value \[aq]%s\[aq]\en", optarg);
fea681da 554 break;
fe5dba13 555\&
b957f81f 556 case \[aq]?\[aq]:
fea681da 557 break;
fe5dba13 558\&
fea681da 559 default:
d1a71985 560 printf("?? getopt returned character code 0%o ??\en", c);
fea681da
MK
561 }
562 }
fe5dba13 563\&
fea681da 564 if (optind < argc) {
29059a65 565 printf("non\-option ARGV\-elements: ");
fea681da 566 while (optind < argc)
cf0a9ace 567 printf("%s ", argv[optind++]);
d1a71985 568 printf("\en");
fea681da 569 }
fe5dba13 570\&
4c44ffe5 571 exit(EXIT_SUCCESS);
fea681da 572}
bdd915e2 573.EE
b0b6ab4e 574.\" SRC END
47297adb 575.SH SEE ALSO
35d82b25 576.BR getopt (1),
0a4f8b7b 577.BR getsubopt (3)