]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getopt.3
ldd.1, sprof.1, accept.2, alarm.2, bind.2, chdir.2, clock_nanosleep.2, close.2, conne...
[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.\"
460495ca 41.TH GETOPT 3 2015-08-08 "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>
48.sp
49.BI "int getopt(int " argc ", char * const " argv[] ,
50.BI " const char *" optstring );
51.sp
52.BI "extern char *" optarg ;
53.BI "extern int " optind ", " opterr ", " optopt ;
54.sp
fea681da
MK
55.B #include <getopt.h>
56.sp
57.BI "int getopt_long(int " argc ", char * const " argv[] ,
58.BI " const char *" optstring ,
59.BI " const struct option *" longopts ", int *" longindex );
60.sp
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
0f200f07
MK
65.sp
66.in -4n
67Feature Test Macro Requirements for glibc (see
68.BR feature_test_macros (7)):
69.ad l
70.in
71.sp
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
60a90ecd
MK
170If
171.BR getopt ()
172does not recognize an option character, it prints an
ce2801d0 173error message to \fIstderr\fP, stores the character in \fIoptopt\fP, and
f81fb444 174returns \(aq?\(aq.
c13182ef 175The calling program may prevent the error message by
fea681da
MK
176setting \fIopterr\fP to 0.
177.PP
60a90ecd
MK
178If
179.BR getopt ()
180finds an option character in \fIargv\fP that was not
fea681da 181included in \fIoptstring\fP, or if it detects a missing option argument,
0425de01 182it returns \(aq?\(aq and sets the external variable \fIoptopt\fP to the
c13182ef
MK
183actual option character.
184If the first character
f81fb444 185(following any optional \(aq+\(aq or \(aq\-\(aq described above)
fea681da 186of \fIoptstring\fP
f81fb444 187is a colon (\(aq:\(aq), then
60a90ecd 188.BR getopt ()
f81fb444 189returns \(aq:\(aq instead of \(aq?\(aq to
c13182ef
MK
190indicate a missing option argument.
191If an error was detected, and
fea681da 192the first character of \fIoptstring\fP is not a colon, and
c7094399 193the external variable \fIopterr\fP is nonzero (which is the default),
60a90ecd
MK
194.BR getopt ()
195prints an error message.
2aeec1f9 196.SS getopt_long() and getopt_long_only()
fea681da 197The
f10e4102 198.BR getopt_long ()
fea681da 199function works like
f10e4102 200.BR getopt ()
c1ed35a7 201except that it also accepts long options, started with two dashes.
d0ac4fb3
MK
202(If the program accepts only long options, then
203.I optstring
204should be specified as an empty string (""), not NULL.)
fea681da 205Long option names may be abbreviated if the abbreviation is
c13182ef
MK
206unique or is an exact match for some defined option.
207A long option
fea681da
MK
208may take a parameter, of the form
209.B \-\-arg=param
210or
211.BR "\-\-arg param" .
212.PP
213.I longopts
214is a pointer to the first element of an array of
8478ee02 215.I struct option
fea681da 216declared in
8478ee02 217.I <getopt.h>
fea681da 218as
088a639b 219.in +4n
fea681da
MK
220.nf
221.sp
fea681da 222struct option {
cf448976
MK
223 const char *name;
224 int has_arg;
225 int *flag;
226 int val;
fea681da
MK
227};
228.fi
cf448976 229.in
fea681da
MK
230.PP
231The meanings of the different fields are:
232.TP
233.I name
234is the name of the long option.
235.TP
236.I has_arg
237is:
f10e4102
MK
238\fBno_argument\fP (or 0) if the option does not take an argument;
239\fBrequired_argument\fP (or 1) if the option requires an argument; or
fea681da
MK
240\fBoptional_argument\fP (or 2) if the option takes an optional argument.
241.TP
242.I flag
c13182ef
MK
243specifies how results are returned for a long option.
244If \fIflag\fP
60a90ecd
MK
245is NULL, then
246.BR getopt_long ()
7066ef44
MK
247returns \fIval\fP.
248(For example, the calling program may set \fIval\fP to the equivalent short
c13182ef 249option character.)
60a90ecd
MK
250Otherwise,
251.BR getopt_long ()
252returns 0, and
fea681da
MK
253\fIflag\fP points to a variable which is set to \fIval\fP if the
254option is found, but left unchanged if the option is not found.
255.TP
c13182ef 256\fIval\fP
fea681da
MK
257is the value to return, or to load into the variable pointed
258to by \fIflag\fP.
259.PP
7b01461a 260The last element of the array has to be filled with zeros.
fea681da 261.PP
35e21ba7 262If \fIlongindex\fP is not NULL, it
fea681da
MK
263points to a variable which is set to the index of the long option relative to
264.IR longopts .
265.PP
60a90ecd
MK
266.BR getopt_long_only ()
267is like
268.BR getopt_long (),
f81fb444 269but \(aq\-\(aq as well
f8a07a21 270as "\-\-" can indicate a long option.
f81fb444 271If an option that starts with \(aq\-\(aq
f8a07a21 272(not "\-\-") doesn't match a long option, but does match a short option,
c13182ef 273it is parsed as a short option instead.
47297adb 274.SH RETURN VALUE
f10e4102
MK
275If an option was successfully found, then
276.BR getopt ()
277returns the option character.
278If all command-line options have been parsed, then
279.BR getopt ()
280returns \-1.
281If
282.BR getopt ()
283encounters an option character that was not in
284.IR optstring ,
f81fb444 285then \(aq?\(aq is returned.
f10e4102
MK
286If
287.BR getopt ()
288encounters an option with a missing argument,
289then the return value depends on the first character in
290.IR optstring :
f81fb444 291if it is \(aq:\(aq, then \(aq:\(aq is returned; otherwise \(aq?\(aq is returned.
fea681da 292.PP
60a90ecd
MK
293.BR getopt_long ()
294and
295.BR getopt_long_only ()
296also return the option
c13182ef
MK
297character when a short option is recognized.
298For a long option, they
299return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise.
60a90ecd
MK
300Error and \-1 returns are the same as for
301.BR getopt (),
f81fb444 302plus \(aq?\(aq for an
fea681da 303ambiguous match or an extraneous parameter.
d219a1a3 304.SH ENVIRONMENT
fea681da 305.TP
fea681da 306.B POSIXLY_CORRECT
24b74457 307If this is set, then option processing stops as soon as a nonoption
fea681da
MK
308argument is encountered.
309.TP
fea681da
MK
310.B _<PID>_GNU_nonoption_argv_flags_
311This variable was used by
f19a0f03 312.BR bash (1)
5260fe08 3132.0 to communicate to glibc which arguments are the results of
c13182ef 314wildcard expansion and so should not be considered as options.
d9bfdb9c 315This behavior was removed in
f19a0f03 316.BR bash (1)
5260fe08 317version 2.01, but the support remains in glibc.
c7401a75
PH
318.SH ATTRIBUTES
319For an explanation of the terms used in this section, see
320.BR attributes (7).
321.TS
322allbox;
323lbw24 lb lb
324l l l.
325Interface Attribute Value
326T{
327.BR getopt (),
328.BR getopt_long (),
329.BR getopt_long_only ()
5eef415b 330T} Thread safety MT-Unsafe race:getopt env
c7401a75 331.TE
47297adb 332.SH CONFORMING TO
2b2581ee
MK
333.TP
334.BR getopt ():
88490b86 335POSIX.1-2001, POSIX.1-2008, and POSIX.2,
d9a10d9d
MK
336provided the environment variable
337.B POSIXLY_CORRECT
338is set.
1e3ae1b4
MK
339Otherwise, the elements of \fIargv\fP aren't really
340.IR const ,
341because we permute them.
342We pretend they're
343.I const
344in the prototype to be compatible with other systems.
ca92ce95 345
e94c735e
MK
346The use of \(aq+\(aq and \(aq\-\(aq in
347.I optstring
348is a GNU extension.
349
2b2581ee
MK
350On some older implementations,
351.BR getopt ()
352was declared in
353.IR <stdio.h> .
354SUSv1 permitted the declaration to appear in either
355.I <unistd.h>
356or
357.IR <stdio.h> .
358POSIX.1-2001 marked the use of
359.I <stdio.h>
360for this purpose as LEGACY.
361POSIX.1-2001 does not allow the declaration to appear in
362.IR <stdio.h> .
e94c735e
MK
363.TP
364.BR getopt_long "() and " getopt_long_only ():
365These functions are GNU extensions.
366.SH NOTES
367A program that scans multiple argument vectors,
368or rescans the same vector more than once,
369and wants to make use of GNU extensions such as \(aq+\(aq
c2a80b76 370and \(aq\-\(aq at the start of
e94c735e
MK
371.IR optstring ,
372or changes the value of
373.B POSIXLY_CORRECT
374between scans,
375must reinitialize
376.BR getopt ()
377by resetting
378.I optind
379to 0, rather than the traditional value of 1.
380(Resetting to 0 forces the invocation of an internal initialization
381routine that rechecks
382.B POSIXLY_CORRECT
383and checks for GNU extensions in
384.IR optstring .)
fea681da 385.SH EXAMPLE
81e10454 386.SS getopt()
92e014f6
MK
387The following trivial example program uses
388.BR getopt ()
c13182ef 389to handle two program options:
f262c004 390.IR \-n ,
92e014f6 391with no associated value; and
f262c004 392.IR "\-t val" ,
92e014f6
MK
393which expects an associated value.
394.nf
395.sp
396#include <unistd.h>
397#include <stdlib.h>
398#include <stdio.h>
399
400int
401main(int argc, char *argv[])
402{
403 int flags, opt;
404 int nsecs, tfnd;
405
406 nsecs = 0;
407 tfnd = 0;
408 flags = 0;
29059a65 409 while ((opt = getopt(argc, argv, "nt:")) != \-1) {
92e014f6 410 switch (opt) {
f81fb444 411 case \(aqn\(aq:
92e014f6
MK
412 flags = 1;
413 break;
f81fb444 414 case \(aqt\(aq:
92e014f6
MK
415 nsecs = atoi(optarg);
416 tfnd = 1;
417 break;
f81fb444 418 default: /* \(aq?\(aq */
29059a65 419 fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
cec29a73 420 argv[0]);
92e014f6
MK
421 exit(EXIT_FAILURE);
422 }
423 }
424
26f418da
MK
425 printf("flags=%d; tfnd=%d; nsecs=%d; optind=%d\\n",
426 flags, tfnd, nsecs, optind);
92e014f6
MK
427
428 if (optind >= argc) {
429 fprintf(stderr, "Expected argument after options\\n");
430 exit(EXIT_FAILURE);
431 }
432
433 printf("name argument = %s\\n", argv[optind]);
434
435 /* Other code omitted */
436
437 exit(EXIT_SUCCESS);
438}
439.fi
81e10454 440.SS getopt_long()
fea681da 441The following example program illustrates the use of
f10e4102 442.BR getopt_long ()
fea681da
MK
443with most of its features.
444.nf
445.sp
446#include <stdio.h> /* for printf */
447#include <stdlib.h> /* for exit */
448#include <getopt.h>
449
450int
c13182ef 451main(int argc, char **argv)
41798314 452{
fea681da
MK
453 int c;
454 int digit_optind = 0;
455
456 while (1) {
457 int this_option_optind = optind ? optind : 1;
458 int option_index = 0;
459 static struct option long_options[] = {
e5266ff9
BW
460 {"add", required_argument, 0, 0 },
461 {"append", no_argument, 0, 0 },
462 {"delete", required_argument, 0, 0 },
463 {"verbose", no_argument, 0, 0 },
464 {"create", required_argument, 0, \(aqc\(aq},
465 {"file", required_argument, 0, 0 },
466 {0, 0, 0, 0 }
fea681da
MK
467 };
468
cf0a9ace 469 c = getopt_long(argc, argv, "abc:d:012",
fea681da
MK
470 long_options, &option_index);
471 if (c == \-1)
472 break;
473
474 switch (c) {
475 case 0:
cf0a9ace 476 printf("option %s", long_options[option_index].name);
fea681da 477 if (optarg)
cf0a9ace
MK
478 printf(" with arg %s", optarg);
479 printf("\\n");
fea681da
MK
480 break;
481
f81fb444
MK
482 case \(aq0\(aq:
483 case \(aq1\(aq:
484 case \(aq2\(aq:
fea681da 485 if (digit_optind != 0 && digit_optind != this_option_optind)
29059a65 486 printf("digits occur in two different argv\-elements.\\n");
fea681da 487 digit_optind = this_option_optind;
cf0a9ace 488 printf("option %c\\n", c);
fea681da
MK
489 break;
490
f81fb444 491 case \(aqa\(aq:
cf0a9ace 492 printf("option a\\n");
fea681da
MK
493 break;
494
f81fb444 495 case \(aqb\(aq:
cf0a9ace 496 printf("option b\\n");
fea681da
MK
497 break;
498
f81fb444
MK
499 case \(aqc\(aq:
500 printf("option c with value \(aq%s\(aq\\n", optarg);
fea681da
MK
501 break;
502
f81fb444
MK
503 case \(aqd\(aq:
504 printf("option d with value \(aq%s\(aq\\n", optarg);
fea681da
MK
505 break;
506
f81fb444 507 case \(aq?\(aq:
fea681da
MK
508 break;
509
510 default:
cf0a9ace 511 printf("?? getopt returned character code 0%o ??\\n", c);
fea681da
MK
512 }
513 }
514
515 if (optind < argc) {
29059a65 516 printf("non\-option ARGV\-elements: ");
fea681da 517 while (optind < argc)
cf0a9ace
MK
518 printf("%s ", argv[optind++]);
519 printf("\\n");
fea681da
MK
520 }
521
4c44ffe5 522 exit(EXIT_SUCCESS);
fea681da
MK
523}
524.fi
47297adb 525.SH SEE ALSO
35d82b25 526.BR getopt (1),
0a4f8b7b 527.BR getsubopt (3)