1GETOPT(P) POSIX Programmer's Manual GETOPT(P)
2
3
4
6 getopt, optarg, opterr, optind, optopt - command option parsing
7
9 #include <unistd.h>
10
11 int getopt(int argc, char * const argv[], const char *optstring);
12 extern char *optarg;
13 extern int optind, opterr, optopt;
14
15
17 The getopt() function is a command-line parser that shall follow Util‐
18 ity Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in the Base Definitions
19 volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guide‐
20 lines.
21
22 The parameters argc and argv are the argument count and argument array
23 as passed to main() (see exec() ). The argument optstring is a string
24 of recognized option characters; if a character is followed by a colon,
25 the option takes an argument. All option characters allowed by Utility
26 Syntax Guideline 3 are allowed in optstring. The implementation may
27 accept other characters as an extension.
28
29 The variable optind is the index of the next element of the argv[] vec‐
30 tor to be processed. It shall be initialized to 1 by the system, and
31 getopt() shall update it when it finishes with each element of argv[].
32 When an element of argv[] contains multiple option characters, it is
33 unspecified how getopt() determines which options have already been
34 processed.
35
36 The getopt() function shall return the next option character (if one is
37 found) from argv that matches a character in optstring, if there is one
38 that matches. If the option takes an argument, getopt() shall set the
39 variable optarg to point to the option-argument as follows:
40
41 1. If the option was the last character in the string pointed to by an
42 element of argv, then optarg shall contain the next element of
43 argv, and optind shall be incremented by 2. If the resulting value
44 of optind is greater than argc, this indicates a missing option-
45 argument, and getopt() shall return an error indication.
46
47 2. Otherwise, optarg shall point to the string following the option
48 character in that element of argv, and optind shall be incremented
49 by 1.
50
51 If, when getopt() is called:
52
53
54 argv[optind] is a null pointer*
55 argv[optind] is not the character -
56 argv[optind] points to the string "-"
57
58 getopt() shall return -1 without changing optind. If:
59
60
61 argv[optind] points to the string "--"
62
63 getopt() shall return -1 after incrementing optind.
64
65 If getopt() encounters an option character that is not contained in
66 optstring, it shall return the question-mark ( '?' ) character. If it
67 detects a missing option-argument, it shall return the colon character
68 ( ':' ) if the first character of optstring was a colon, or a question-
69 mark character ( '?' ) otherwise. In either case, getopt() shall set
70 the variable optopt to the option character that caused the error. If
71 the application has not set the variable opterr to 0 and the first
72 character of optstring is not a colon, getopt() shall also print a
73 diagnostic message to stderr in the format specified for the getopts
74 utility.
75
76 The getopt() function need not be reentrant. A function that is not
77 required to be reentrant is not required to be thread-safe.
78
80 The getopt() function shall return the next option character specified
81 on the command line.
82
83 A colon ( ':' ) shall be returned if getopt() detects a missing argu‐
84 ment and the first character of optstring was a colon ( ':' ).
85
86 A question mark ( '?' ) shall be returned if getopt() encounters an
87 option character not in optstring or detects a missing argument and the
88 first character of optstring was not a colon ( ':' ).
89
90 Otherwise, getopt() shall return -1 when all command line options are
91 parsed.
92
94 No errors are defined.
95
96 The following sections are informative.
97
99 Parsing Command Line Options
100 The following code fragment shows how you might process the arguments
101 for a utility that can take the mutually-exclusive options a and b and
102 the options f and o, both of which require arguments:
103
104
105 #include <unistd.h>
106
107
108 int
109 main(int argc, char *argv[ ])
110 {
111 int c;
112 int bflg, aflg, errflg;
113 char *ifile;
114 char *ofile;
115 extern char *optarg;
116 extern int optind, optopt;
117 . . .
118 while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
119 switch(c) {
120 case 'a':
121 if (bflg)
122 errflg++;
123 else
124 aflg++;
125 break;
126 case 'b':
127 if (aflg)
128 errflg++;
129 else {
130 bflg++;
131 bproc();
132 }
133 break;
134 case 'f':
135 ifile = optarg;
136 break;
137 case 'o':
138 ofile = optarg;
139 break;
140 case ':': /* -f or -o without operand */
141 fprintf(stderr,
142 "Option -%c requires an operand\n", optopt);
143 errflg++;
144 break;
145 case '?':
146 fprintf(stderr,
147 "Unrecognized option: -%c\n", optopt);
148 errflg++;
149 }
150 }
151 if (errflg) {
152 fprintf(stderr, "usage: . . . ");
153 exit(2);
154 }
155 for ( ; optind < argc; optind++) {
156 if (access(argv[optind], R_OK)) {
157 . . .
158 }
159
160 This code accepts any of the following as equivalent:
161
162
163 cmd -ao arg path path
164 cmd -a -o arg path path
165 cmd -o arg -a path path
166 cmd -a -o arg -- path path
167 cmd -a -oarg path path
168 cmd -aoarg path path
169
170 Checking Options and Arguments
171 The following example parses a set of command line options and prints
172 messages to standard output for each option and argument that it
173 encounters.
174
175
176 #include <unistd.h>
177 #include <stdio.h>
178 ...
179 int c;
180 char *filename;
181 extern char *optarg;
182 extern int optind, optopt, opterr;
183 ...
184 while ((c = getopt(argc, argv, ":abf:")) != -1) {
185 switch(c) {
186 case 'a':
187 printf("a is set\n");
188 break;
189 case 'b':
190 printf("b is set\n");
191 break;
192 case 'f':
193 filename = optarg;
194 printf("filename is %s\n", filename);
195 break;
196 case ':':
197 printf("-%c without filename\n", optopt);
198 break;
199 case '?':
200 printf("unknown arg %c\n", optopt);
201 break;
202 }
203 }
204
205 Selecting Options from the Command Line
206 The following example selects the type of database routines the user
207 wants to use based on the Options argument.
208
209
210 #include <unistd.h>
211 #include <string.h>
212 ...
213 char *Options = "hdbtl";
214 ...
215 int dbtype, i;
216 char c;
217 char *st;
218 ...
219 dbtype = 0;
220 while ((c = getopt(argc, argv, Options)) != -1) {
221 if ((st = strchr(Options, c)) != NULL) {
222 dbtype = st - Options;
223 break;
224 }
225 }
226
228 The getopt() function is only required to support option characters
229 included in Utility Syntax Guideline 3. Many historical implementations
230 of getopt() support other characters as options. This is an allowed
231 extension, but applications that use extensions are not maximally por‐
232 table. Note that support for multi-byte option characters is only pos‐
233 sible when such characters can be represented as type int.
234
236 The optopt variable represents historical practice and allows the
237 application to obtain the identity of the invalid option.
238
239 The description has been written to make it clear that getopt(), like
240 the getopts utility, deals with option-arguments whether separated from
241 the option by <blank>s or not. Note that the requirements on getopt()
242 and getopts are more stringent than the Utility Syntax Guidelines.
243
244 The getopt() function shall return -1, rather than EOF, so that
245 <stdio.h> is not required.
246
247 The special significance of a colon as the first character of optstring
248 makes getopt() consistent with the getopts utility. It allows an appli‐
249 cation to make a distinction between a missing argument and an incor‐
250 rect option letter without having to examine the option letter. It is
251 true that a missing argument can only be detected in one case, but that
252 is a case that has to be considered.
253
255 None.
256
258 exec() , the Base Definitions volume of IEEE Std 1003.1-2001,
259 <unistd.h>, the Shell and Utilities volume of IEEE Std 1003.1-2001
260
262 Portions of this text are reprinted and reproduced in electronic form
263 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
264 -- Portable Operating System Interface (POSIX), The Open Group Base
265 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
266 Electrical and Electronics Engineers, Inc and The Open Group. In the
267 event of any discrepancy between this version and the original IEEE and
268 The Open Group Standard, the original IEEE and The Open Group Standard
269 is the referee document. The original Standard can be obtained online
270 at http://www.opengroup.org/unix/online.html .
271
272
273
274IEEE/The Open Group 2003 GETOPT(P)