]>
Commit | Line | Data |
---|---|---|
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 |
25 | getopt, getopt_long, getopt_long_only, |
26 | optarg, optind, opterr, optopt \- Parse command-line options | |
42009080 AC |
27 | .SH LIBRARY |
28 | Standard 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 |
51 | Feature 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 |
66 | The | |
f10e4102 | 67 | .BR getopt () |
c13182ef MK |
68 | function parses the command-line arguments. |
69 | Its arguments | |
fea681da MK |
70 | .I argc |
71 | and | |
72 | .I argv | |
73 | are the argument count and array as passed to the | |
63aa9df0 | 74 | .IR main () |
fea681da | 75 | function on program invocation. |
b957f81f | 76 | An element of \fIargv\fP that starts with \[aq]\-\[aq] |
fea681da | 77 | (and is not exactly "\-" or "\-\-") |
c13182ef MK |
78 | is an option element. |
79 | The characters of this element | |
b957f81f | 80 | (aside from the initial \[aq]\-\[aq]) are option characters. |
60a90ecd MK |
81 | If |
82 | .BR getopt () | |
fea681da MK |
83 | is called repeatedly, it returns successively each of the option characters |
84 | from each of the option elements. | |
c6d039a3 | 85 | .P |
66b08231 MK |
86 | The variable |
87 | .I optind | |
88 | is the index of the next element to be processed in | |
89 | .IR argv . | |
90 | The system initializes this value to 1. | |
91 | The caller can reset it to 1 to restart scanning of the same | |
92 | .IR argv , | |
93 | or when scanning a new argument vector. | |
c6d039a3 | 94 | .P |
60a90ecd MK |
95 | If |
96 | .BR getopt () | |
97 | finds another option character, it returns that | |
fea681da | 98 | character, updating the external variable \fIoptind\fP and a static |
60a90ecd MK |
99 | variable \fInextchar\fP so that the next call to |
100 | .BR getopt () | |
101 | can | |
fea681da MK |
102 | resume the scan with the following option character or |
103 | \fIargv\fP-element. | |
c6d039a3 | 104 | .P |
60a90ecd MK |
105 | If there are no more option characters, |
106 | .BR getopt () | |
107 | returns \-1. | |
fea681da MK |
108 | Then \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 | 112 | is a string containing the legitimate option characters. |
dfa1ee20 JH |
113 | A legitimate option character is any visible one byte |
114 | .BR ascii (7) | |
115 | character (for which | |
116 | .BR isgraph (3) | |
b957f81f | 117 | would return nonzero) that is not \[aq]\-\[aq], \[aq]:\[aq], or \[aq];\[aq]. |
c13182ef | 118 | If such a |
fea681da | 119 | character is followed by a colon, the option requires an argument, so |
60a90ecd MK |
120 | .BR getopt () |
121 | places 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 . | |
124 | Two colons mean an option takes | |
e6bcc128 | 125 | an 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 | 127 | then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero. |
c13182ef MK |
128 | This is a GNU extension. |
129 | If | |
fea681da MK |
130 | .I optstring |
131 | contains | |
132 | .B W | |
133 | followed by a semicolon, then | |
134 | .B \-W foo | |
135 | is treated as the long option | |
136 | .BR \-\-foo . | |
137 | (The | |
138 | .B \-W | |
139 | option is reserved by POSIX.2 for implementation extensions.) | |
d9bfdb9c | 140 | This behavior is a GNU extension, not available with libraries before |
5260fe08 | 141 | glibc 2. |
c6d039a3 | 142 | .P |
60a90ecd MK |
143 | By default, |
144 | .BR getopt () | |
145 | permutes the contents of \fIargv\fP as it | |
24b74457 | 146 | scans, so that eventually all the nonoptions are at the end. |
4f247e78 | 147 | Two other scanning modes are also implemented. |
c13182ef | 148 | If the first character of |
b957f81f | 149 | \fIoptstring\fP is \[aq]+\[aq] or the environment variable |
d9a10d9d | 150 | .B POSIXLY_CORRECT |
24b74457 | 151 | is set, then option processing stops as soon as a nonoption argument is |
c13182ef | 152 | encountered. |
b957f81f | 153 | If \[aq]+\[aq] is not the first character of |
87ce9492 JH |
154 | .IR optstring , |
155 | it is treated as a normal option. | |
156 | If | |
157 | .B POSIXLY_CORRECT | |
158 | behaviour is required in this case | |
159 | .I optstring | |
b957f81f AC |
160 | will contain two \[aq]+\[aq] symbols. |
161 | If the first character of \fIoptstring\fP is \[aq]\-\[aq], then | |
24b74457 | 162 | each nonoption \fIargv\fP-element is handled as if it were the argument of |
5e833e27 MK |
163 | an option with character code 1. |
164 | (This is used by programs that were | |
fea681da MK |
165 | written to expect options and other \fIargv\fP-elements in any order |
166 | and that care about the ordering of the two.) | |
167 | The special argument "\-\-" forces an end of option-scanning regardless | |
168 | of the scanning mode. | |
c6d039a3 | 169 | .P |
a4746c39 MK |
170 | While processing the option list, |
171 | .BR getopt () | |
172 | can detect two kinds of errors: | |
173 | (1) an option character that was not specified in | |
1ae6b2c7 | 174 | .I optstring |
a4746c39 MK |
175 | and (2) a missing option argument |
176 | (i.e., an option at the end of the command line without an expected argument). | |
177 | Such errors are handled and reported as follows: | |
cdede5cd | 178 | .IP \[bu] 3 |
a4746c39 | 179 | By default, |
60a90ecd | 180 | .BR getopt () |
a4746c39 MK |
181 | prints an error message on standard error, |
182 | places the erroneous option character in | |
183 | .IR optopt , | |
b957f81f | 184 | and returns \[aq]?\[aq] as the function result. |
cdede5cd | 185 | .IP \[bu] |
a4746c39 | 186 | If the caller has set the global variable |
1ae6b2c7 | 187 | .I opterr |
a4746c39 MK |
188 | to zero, then |
189 | .BR getopt () | |
190 | does not print an error message. | |
191 | The caller can determine that there was an error by testing whether | |
b957f81f | 192 | the function return value is \[aq]?\[aq]. |
a4746c39 | 193 | (By default, |
1ae6b2c7 | 194 | .I opterr |
a4746c39 | 195 | has a nonzero value.) |
cdede5cd | 196 | .IP \[bu] |
c13182ef | 197 | If the first character |
b957f81f | 198 | (following any optional \[aq]+\[aq] or \[aq]\-\[aq] described above) |
fea681da | 199 | of \fIoptstring\fP |
b957f81f | 200 | is a colon (\[aq]:\[aq]), then |
60a90ecd | 201 | .BR getopt () |
a4746c39 | 202 | likewise does not print an error message. |
b957f81f | 203 | In addition, it returns \[aq]:\[aq] instead of \[aq]?\[aq] to |
c13182ef | 204 | indicate a missing option argument. |
a4746c39 MK |
205 | This allows the caller to distinguish the two different types of errors. |
206 | .\" | |
2aeec1f9 | 207 | .SS getopt_long() and getopt_long_only() |
fea681da | 208 | The |
f10e4102 | 209 | .BR getopt_long () |
fea681da | 210 | function works like |
f10e4102 | 211 | .BR getopt () |
c1ed35a7 | 212 | except that it also accepts long options, started with two dashes. |
d0ac4fb3 MK |
213 | (If the program accepts only long options, then |
214 | .I optstring | |
215 | should be specified as an empty string (""), not NULL.) | |
fea681da | 216 | Long option names may be abbreviated if the abbreviation is |
c13182ef MK |
217 | unique or is an exact match for some defined option. |
218 | A long option | |
fea681da MK |
219 | may take a parameter, of the form |
220 | .B \-\-arg=param | |
221 | or | |
222 | .BR "\-\-arg param" . | |
c6d039a3 | 223 | .P |
fea681da MK |
224 | .I longopts |
225 | is a pointer to the first element of an array of | |
8478ee02 | 226 | .I struct option |
fea681da | 227 | declared in |
8478ee02 | 228 | .I <getopt.h> |
fea681da | 229 | as |
c6d039a3 | 230 | .P |
088a639b | 231 | .in +4n |
b8302363 | 232 | .EX |
fea681da | 233 | struct 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 |
242 | The meanings of the different fields are: |
243 | .TP | |
244 | .I name | |
245 | is the name of the long option. | |
246 | .TP | |
247 | .I has_arg | |
248 | is: | |
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 |
254 | specifies how results are returned for a long option. |
255 | If \fIflag\fP | |
60a90ecd MK |
256 | is NULL, then |
257 | .BR getopt_long () | |
7066ef44 MK |
258 | returns \fIval\fP. |
259 | (For example, the calling program may set \fIval\fP to the equivalent short | |
c13182ef | 260 | option character.) |
60a90ecd MK |
261 | Otherwise, |
262 | .BR getopt_long () | |
263 | returns 0, and | |
fea681da MK |
264 | \fIflag\fP points to a variable which is set to \fIval\fP if the |
265 | option is found, but left unchanged if the option is not found. | |
266 | .TP | |
c13182ef | 267 | \fIval\fP |
fea681da MK |
268 | is the value to return, or to load into the variable pointed |
269 | to by \fIflag\fP. | |
c6d039a3 | 270 | .P |
7b01461a | 271 | The last element of the array has to be filled with zeros. |
c6d039a3 | 272 | .P |
35e21ba7 | 273 | If \fIlongindex\fP is not NULL, it |
fea681da MK |
274 | points 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 () |
278 | is like | |
279 | .BR getopt_long (), | |
b957f81f | 280 | but \[aq]\-\[aq] as well |
f8a07a21 | 281 | as "\-\-" can indicate a long option. |
b957f81f | 282 | If an option that starts with \[aq]\-\[aq] |
f8a07a21 | 283 | (not "\-\-") doesn't match a long option, but does match a short option, |
c13182ef | 284 | it is parsed as a short option instead. |
47297adb | 285 | .SH RETURN VALUE |
f10e4102 MK |
286 | If an option was successfully found, then |
287 | .BR getopt () | |
288 | returns the option character. | |
289 | If all command-line options have been parsed, then | |
290 | .BR getopt () | |
291 | returns \-1. | |
292 | If | |
293 | .BR getopt () | |
294 | encounters an option character that was not in | |
295 | .IR optstring , | |
b957f81f | 296 | then \[aq]?\[aq] is returned. |
f10e4102 MK |
297 | If |
298 | .BR getopt () | |
299 | encounters an option with a missing argument, | |
300 | then the return value depends on the first character in | |
301 | .IR optstring : | |
d6c1998e AC |
302 | if it is \[aq]:\[aq], then \[aq]:\[aq] is returned; |
303 | otherwise \[aq]?\[aq] is returned. | |
c6d039a3 | 304 | .P |
60a90ecd MK |
305 | .BR getopt_long () |
306 | and | |
307 | .BR getopt_long_only () | |
308 | also return the option | |
c13182ef MK |
309 | character when a short option is recognized. |
310 | For a long option, they | |
311 | return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise. | |
60a90ecd MK |
312 | Error and \-1 returns are the same as for |
313 | .BR getopt (), | |
b957f81f | 314 | plus \[aq]?\[aq] for an |
fea681da | 315 | ambiguous match or an extraneous parameter. |
d219a1a3 | 316 | .SH ENVIRONMENT |
fea681da | 317 | .TP |
fea681da | 318 | .B POSIXLY_CORRECT |
24b74457 | 319 | If this is set, then option processing stops as soon as a nonoption |
fea681da MK |
320 | argument is encountered. |
321 | .TP | |
fea681da MK |
322 | .B _<PID>_GNU_nonoption_argv_flags_ |
323 | This variable was used by | |
f19a0f03 | 324 | .BR bash (1) |
5260fe08 | 325 | 2.0 to communicate to glibc which arguments are the results of |
c13182ef | 326 | wildcard expansion and so should not be considered as options. |
d9bfdb9c | 327 | This behavior was removed in |
f19a0f03 | 328 | .BR bash (1) |
b324e17d | 329 | 2.01, but the support remains in glibc. |
c7401a75 PH |
330 | .SH ATTRIBUTES |
331 | For an explanation of the terms used in this section, see | |
332 | .BR attributes (7). | |
333 | .TS | |
334 | allbox; | |
b32feea5 | 335 | lb lb lbx |
c7401a75 PH |
336 | l l l. |
337 | Interface Attribute Value | |
338 | T{ | |
9e54434e BR |
339 | .na |
340 | .nh | |
c7401a75 PH |
341 | .BR getopt (), |
342 | .BR getopt_long (), | |
343 | .BR getopt_long_only () | |
b32feea5 | 344 | T} Thread safety T{ |
9e54434e BR |
345 | .na |
346 | .nh | |
b32feea5 MK |
347 | MT-Unsafe race:getopt env |
348 | T} | |
c7401a75 | 349 | .TE |
4131356c | 350 | .SH VERSIONS |
185ed498 AC |
351 | POSIX specifies that the |
352 | .I argv | |
353 | array argument should be | |
354 | .IR const , | |
355 | but these functions permute its elements | |
356 | unless the environment variable | |
d9a10d9d MK |
357 | .B POSIXLY_CORRECT |
358 | is set. | |
1e3ae1b4 | 359 | .I const |
185ed498 AC |
360 | is used in the actual prototype to be compatible with other systems; |
361 | however, this page doesn't show the qualifier, | |
362 | to avoid confusing readers. | |
4131356c AC |
363 | .SH STANDARDS |
364 | .TP | |
365 | .BR getopt () | |
366 | POSIX.1-2008. | |
367 | .TP | |
368 | .BR getopt_long () | |
369 | .TQ | |
370 | .BR getopt_long_only () | |
371 | GNU. | |
847e0d88 | 372 | .IP |
b957f81f | 373 | The use of \[aq]+\[aq] and \[aq]\-\[aq] in |
e94c735e MK |
374 | .I optstring |
375 | is a GNU extension. | |
4131356c AC |
376 | .SH HISTORY |
377 | .TP | |
378 | .BR getopt () | |
379 | POSIX.1-2001, and POSIX.2. | |
c6d039a3 | 380 | .P |
2b2581ee MK |
381 | On some older implementations, |
382 | .BR getopt () | |
383 | was declared in | |
384 | .IR <stdio.h> . | |
385 | SUSv1 permitted the declaration to appear in either | |
386 | .I <unistd.h> | |
387 | or | |
388 | .IR <stdio.h> . | |
accc82d2 | 389 | POSIX.1-1996 marked the use of |
2b2581ee MK |
390 | .I <stdio.h> |
391 | for this purpose as LEGACY. | |
accc82d2 | 392 | POSIX.1-2001 does not require the declaration to appear in |
2b2581ee | 393 | .IR <stdio.h> . |
e94c735e MK |
394 | .SH NOTES |
395 | A program that scans multiple argument vectors, | |
396 | or rescans the same vector more than once, | |
b957f81f AC |
397 | and wants to make use of GNU extensions such as \[aq]+\[aq] |
398 | and \[aq]\-\[aq] at the start of | |
e94c735e MK |
399 | .IR optstring , |
400 | or changes the value of | |
401 | .B POSIXLY_CORRECT | |
402 | between scans, | |
403 | must reinitialize | |
404 | .BR getopt () | |
405 | by resetting | |
406 | .I optind | |
407 | to 0, rather than the traditional value of 1. | |
408 | (Resetting to 0 forces the invocation of an internal initialization | |
409 | routine that rechecks | |
410 | .B POSIXLY_CORRECT | |
411 | and checks for GNU extensions in | |
412 | .IR optstring .) | |
c6d039a3 | 413 | .P |
dfa1ee20 JH |
414 | Command-line arguments are parsed in strict order |
415 | meaning that an option requiring an argument will consume the next argument, | |
416 | regardless of whether that argument is the correctly specified option argument | |
417 | or simply the next option | |
418 | (in the scenario the user mis-specifies the command line). | |
419 | For example, if | |
420 | .I optstring | |
421 | is specified as "1n:" | |
422 | and the user specifies the command line arguments incorrectly as | |
423 | .IR "prog\ \-n\ \-1" , | |
424 | the | |
425 | .I \-n | |
426 | option will be given the | |
427 | .B optarg | |
428 | value "\-1", and the | |
429 | .I \-1 | |
430 | option will be considered to have not been specified. | |
a14af333 | 431 | .SH EXAMPLES |
81e10454 | 432 | .SS getopt() |
92e014f6 MK |
433 | The following trivial example program uses |
434 | .BR getopt () | |
c13182ef | 435 | to handle two program options: |
f262c004 | 436 | .IR \-n , |
92e014f6 | 437 | with no associated value; and |
f262c004 | 438 | .IR "\-t val" , |
92e014f6 | 439 | which 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 |
447 | int |
448 | main(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 | 489 | The following example program illustrates the use of |
f10e4102 | 490 | .BR getopt_long () |
fea681da | 491 | with 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 | 499 | int |
aa1f53cc | 500 | main(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) |