1 .\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
2 .TH "GETOPT" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
5 getopt, optarg, opterr, optind, optopt \- command option parsing
11 int getopt(int\fP \fIargc\fP\fB, char * const\fP \fIargv\fP\fB[],
12 const char *\fP\fIoptstring\fP\fB);
16 extern int optind, opterr, optopt;
21 The \fIgetopt\fP() function is a command-line parser that shall follow
22 Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in
23 the Base Definitions volume of IEEE\ Std\ 1003.1-2001, Section 12.2,
24 Utility Syntax Guidelines.
26 The parameters \fIargc\fP and \fIargv\fP are the argument count and
27 argument array as passed to \fImain\fP() (see \fIexec\fP() ). The
28 argument \fIoptstring\fP is a string of recognized
29 option characters; if a character is followed by a colon, the option
30 takes an argument. All option characters allowed by Utility
31 Syntax Guideline 3 are allowed in \fIoptstring\fP. The implementation
32 may accept other characters as an extension.
34 The variable \fIoptind\fP is the index of the next element of the
35 \fIargv\fP[] vector to be processed. It shall be initialized
36 to 1 by the system, and \fIgetopt\fP() shall update it when it finishes
37 with each element of \fIargv\fP[]. When an element of
38 \fIargv\fP[] contains multiple option characters, it is unspecified
39 how \fIgetopt\fP() determines which options have already been
42 The \fIgetopt\fP() function shall return the next option character
43 (if one is found) from \fIargv\fP that matches a character
44 in \fIoptstring\fP, if there is one that matches. If the option takes
45 an argument, \fIgetopt\fP() shall set the variable
46 \fIoptarg\fP to point to the option-argument as follows:
48 If the option was the last character in the string pointed to by an
49 element of \fIargv\fP, then \fIoptarg\fP shall contain the
50 next element of \fIargv\fP, and \fIoptind\fP shall be incremented
51 by 2. If the resulting value of \fIoptind\fP is greater than
52 \fIargc\fP, this indicates a missing option-argument, and \fIgetopt\fP()
53 shall return an error indication.
56 Otherwise, \fIoptarg\fP shall point to the string following the option
57 character in that element of \fIargv\fP, and
58 \fIoptind\fP shall be incremented by 1.
61 If, when \fIgetopt\fP() is called:
66 \fIargv\fP\fB[optind]\fP is a null pointer\fB*\fP
67 \fIargv\fP\fB[optind]\fP is not the character \fB- \fP
68 \fIargv\fP\fB[optind]\fP points to the string \fB"-"\fP
72 \fIgetopt\fP() shall return -1 without changing \fIoptind\fP. If:
77 \fIargv\fP\fB[optind] \fP points to the string \fB"--"
82 \fIgetopt\fP() shall return -1 after incrementing \fIoptind\fP.
84 If \fIgetopt\fP() encounters an option character that is not contained
85 in \fIoptstring\fP, it shall return the question-mark (
86 \fB'?'\fP ) character. If it detects a missing option-argument, it
87 shall return the colon character ( \fB':'\fP ) if the
88 first character of \fIoptstring\fP was a colon, or a question-mark
89 character ( \fB'?'\fP ) otherwise. In either case,
90 \fIgetopt\fP() shall set the variable \fIoptopt\fP to the option character
91 that caused the error. If the application has not set
92 the variable \fIopterr\fP to 0 and the first character of \fIoptstring\fP
93 is not a colon, \fIgetopt\fP() shall also print a
94 diagnostic message to \fIstderr\fP in the format specified for the
98 The \fIgetopt\fP() function need not be reentrant. A function that
99 is not required to be reentrant is not required to be
103 The \fIgetopt\fP() function shall return the next option character
104 specified on the command line.
106 A colon ( \fB':'\fP ) shall be returned if \fIgetopt\fP() detects
107 a missing argument and the first character of
108 \fIoptstring\fP was a colon ( \fB':'\fP ).
110 A question mark ( \fB'?'\fP ) shall be returned if \fIgetopt\fP()
111 encounters an option character not in \fIoptstring\fP or
112 detects a missing argument and the first character of \fIoptstring\fP
113 was not a colon ( \fB':'\fP ).
115 Otherwise, \fIgetopt\fP() shall return -1 when all command line options
119 No errors are defined.
121 \fIThe following sections are informative.\fP
123 .SS Parsing Command Line Options
125 The following code fragment shows how you might process the arguments
126 for a utility that can take the mutually-exclusive options
127 \fIa\fP and \fIb\fP and the options \fIf\fP and \fIo\fP, both of which
133 \fB#include <unistd.h>
137 main(int argc, char *argv[ ])
140 int bflg, aflg, errflg;
144 extern int optind, optopt;
146 while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
168 case ':': /* -f or -o without operand */
170 "Option -%c requires an operand\\n", optopt);
175 "Unrecognized option: -%c\\n", optopt);
180 fprintf(stderr, "usage: . . . ");
183 for ( ; optind < argc; optind++) {
184 if (access(argv[optind], R_OK)) {
191 This code accepts any of the following as equivalent:
196 \fBcmd -ao arg path path
197 cmd -a -o arg path path
198 cmd -o arg -a path path
199 cmd -a -o arg -- path path
200 cmd -a -oarg path path
205 .SS Checking Options and Arguments
207 The following example parses a set of command line options and prints
208 messages to standard output for each option and argument
214 \fB#include <unistd.h>
220 extern int optind, optopt, opterr;
222 while ((c = getopt(argc, argv, ":abf:")) != -1) {
225 printf("a is set\\n");
228 printf("b is set\\n");
232 printf("filename is %s\\n", filename);
235 printf("-%c without filename\\n", optopt);
238 printf("unknown arg %c\\n", optopt);
245 .SS Selecting Options from the Command Line
247 The following example selects the type of database routines the user
248 wants to use based on the \fIOptions\fP argument.
253 \fB#include <unistd.h>
256 char *Options = "hdbtl";
263 while ((c = getopt(argc, argv, Options)) != -1) {
264 if ((st = strchr(Options, c)) != NULL) {
265 dbtype = st - Options;
272 .SH APPLICATION USAGE
274 The \fIgetopt\fP() function is only required to support option characters
275 included in Utility Syntax Guideline 3. Many
276 historical implementations of \fIgetopt\fP() support other characters
277 as options. This is an allowed extension, but applications
278 that use extensions are not maximally portable. Note that support
279 for multi-byte option characters is only possible when such
280 characters can be represented as type \fBint\fP.
283 The \fIoptopt\fP variable represents historical practice and allows
284 the application to obtain the identity of the invalid
287 The description has been written to make it clear that \fIgetopt\fP(),
288 like the \fIgetopts\fP utility, deals with option-arguments whether
289 separated from the option by
290 <blank>s or not. Note that the requirements on \fIgetopt\fP() and
292 more stringent than the Utility Syntax Guidelines.
294 The \fIgetopt\fP() function shall return -1, rather than EOF, so that
295 \fI<stdio.h>\fP is not required.
297 The special significance of a colon as the first character of \fIoptstring\fP
298 makes \fIgetopt\fP() consistent with the \fIgetopts\fP utility. It
299 allows an application to make a distinction between a missing
300 argument and an incorrect option letter without having to examine
301 the option letter. It is true that a missing argument can only be
302 detected in one case, but that is a case that has to be considered.
303 .SH FUTURE DIRECTIONS
308 \fIexec\fP() , the Base Definitions volume of
309 IEEE\ Std\ 1003.1-2001, \fI<unistd.h>\fP, the Shell and Utilities
311 IEEE\ Std\ 1003.1-2001
313 Portions of this text are reprinted and reproduced in electronic form
314 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
315 -- Portable Operating System Interface (POSIX), The Open Group Base
316 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
317 Electrical and Electronics Engineers, Inc and The Open Group. In the
318 event of any discrepancy between this version and the original IEEE and
319 The Open Group Standard, the original IEEE and The Open Group Standard
320 is the referee document. The original Standard can be obtained online at
321 http://www.opengroup.org/unix/online.html .