Debian Codegraph @

wdoc/getopt.html

raw
1 <HTML>
2
3 <HEAD>
4 <TITLE>Manpage of GETOPT</TITLE>
5
6 <LINK rel=stylesheet type="text/css"
7 href="./wdoc.css"
8 title="Style sheet for wdoc developer documents">
9
10 </HEAD>
11
12 <BODY>
13
14 <H1>GETOPT</H1>
15 Section: Linux Programmer's Manual (3)<BR>Updated: 8 May 1998<BR><A HREF="#index">Index</A>
16
17 <A NAME="lbAB">&nbsp;</A>
18 <H2>NAME</H2>
19
20 getopt - Parse command line options
21 <A NAME="lbAC">&nbsp;</A>
22 <H2>SYNOPSIS</H2>
23
24 <PRE>
25 <B>#include &lt;<A HREF="file:/usr/include/unistd.h">unistd.h</A>&gt;</B>
26
27 <B>int getopt(int </B><I>argc</I><B>, char * const </B><I>argv[]</I><B>,</B>
28 <B> const char *</B><I>optstring</I><B>);</B>
29
30 <B>extern char *</B><I>optarg</I><B>;</B>
31 <B>extern int </B><I>optind</I><B>, </B><I>opterr</I><B>, </B><I>optopt</I><B>;</B>
32
33 <B>#define _GNU_SOURCE</B>
34 <BR>
35 <B>#include &lt;<A HREF="file:/usr/include/getopt.h">getopt.h</A>&gt;</B>
36
37 <B>int getopt_long(int </B><I>argc</I><B>, char * const </B><I>argv[]</I><B>,</B>
38 <B> const char *</B><I>optstring</I><B>,</B>
39 <B> const struct option *</B><I>longopts</I><B>, int *</B><I>longindex</I><B>);</B>
40
41 <B>int getopt_long_only(int </B><I>argc</I><B>, char * const </B><I>argv[]</I><B>,</B>
42 <B> const char *</B><I>optstring</I><B>,</B>
43 <B> const struct option *</B><I>longopts</I><B>, int *</B><I>longindex</I><B>);</B>
44 </PRE>
45
46 <A NAME="lbAD">&nbsp;</A>
47 <H2>DESCRIPTION</H2>
48
49 The
50 <B>getopt()</B>
51
52 function parses the command line arguments. Its arguments
53 <I>argc</I>
54
55 and
56 <I>argv</I>
57
58 are the argument count and array as passed to the
59 <B>main()</B>
60
61 function on program invocation.
62 An element of <I>argv</I> that starts with `-' (and is not exactly &quot;-&quot; or &quot;--&quot;)
63 is an option element. The characters of this element
64 (aside from the initial `-') are option characters. If <B>getopt()</B>
65 is called repeatedly, it returns successively each of the option characters
66 from each of the option elements.
67 <P>
68
69 If <B>getopt()</B> finds another option character, it returns that
70 character, updating the external variable <I>optind</I> and a static
71 variable <I>nextchar</I> so that the next call to <B>getopt()</B> can
72 resume the scan with the following option character or
73 <I>argv</I>-element.
74 <P>
75
76 If there are no more option characters, <B>getopt()</B> returns -1.
77 Then <I>optind</I> is the index in <I>argv</I> of the first
78 <I>argv</I>-element that is not an option.
79 <P>
80
81 <I>optstring</I>
82
83 is a string containing the legitimate option characters. If such a
84 character is followed by a colon, the option requires an argument, so
85 <B>getopt</B> places a pointer to the following text in the same
86 <I>argv</I>-element, or the text of the following <I>argv</I>-element, in
87 <I>optarg</I>.
88
89 Two colons mean an option takes
90 an optional arg; if there is text in the current <I>argv</I>-element,
91 it is returned in <I>optarg</I>, otherwise <I>optarg</I> is set to zero.
92 This is a GNU extension. If
93 <I>optstring</I>
94
95 contains
96 <B>W</B>
97
98 followed by a semicolon, then
99 <B>-W foo</B>
100
101 is treated as the long option
102 <B>--foo</B>.
103
104 (The
105 <B>-W</B>
106
107 option is reserved by POSIX.2 for implementation extensions.)
108 This behaviour is a GNU extension, not available with libraries before
109 GNU libc 2.
110 <P>
111
112 By default, <B>getopt()</B> permutes the contents of <I>argv</I> as it
113 scans, so that eventually all the non-options are at the end. Two
114 other modes are also implemented. If the first character of
115 <I>optstring</I> is `+' or the environment variable POSIXLY_CORRECT is
116 set, then option processing stops as soon as a non-option argument is
117 encountered. If the first character of <I>optstring</I> is `-', then
118 each non-option <I>argv</I>-element is handled as if it were the argument of
119 an option with character code 1. (This is used by programs that were
120 written to expect options and other <I>argv</I>-elements in any order
121 and that care about the ordering of the two.)
122 The special argument `--' forces an end of option-scanning regardless
123 of the scanning mode.
124 <P>
125
126 If <B>getopt()</B> does not recognize an option character, it prints an
127 error message to stderr, stores the character in <I>optopt</I>, and
128 returns `?'. The calling program may prevent the error message by
129 setting <I>opterr</I> to 0.
130 <P>
131
132 The
133 <B>getopt_long()</B>
134
135 function works like
136 <B>getopt()</B>
137
138 except that it also accepts long options, started out by two dashes.
139 Long option names may be abbreviated if the abbreviation is
140 unique or is an exact match for some defined option. A long option
141 may take a parameter, of the form
142 <B>--arg=param</B>
143
144 or
145 <B>--arg param</B>.
146
147 <P>
148
149 <I>longopts</I>
150
151 is a pointer to the first element of an array of
152 <B>struct option</B>
153
154 declared in
155 <B>&lt;<A HREF="file:/usr/include/getopt.h">getopt.h</A>&gt;</B>
156
157 as
158 <PRE>
159
160 struct option {
161 const char *name;
162 int has_arg;
163 int *flag;
164 int val;
165 };
166 </PRE>
167
168 <P>
169
170 The meanings of the different fields are:
171 <DL COMPACT>
172 <DT><I>name</I>
173
174 <DD>
175 is the name of the long option.
176 <DT><I>has_arg</I>
177
178 <DD>
179 is:
180 <B>no_argument</B> (or 0) if the option does not take an argument,
181 <B>required_argument</B> (or 1) if the option requires an argument, or
182 <B>optional_argument</B> (or 2) if the option takes an optional argument.
183 <DT><I>flag</I>
184
185 <DD>
186 specifies how results are returned for a long option. If <I>flag</I>
187 is <B>NULL</B>, then <B>getopt_long()</B> returns <I>val</I>. (For
188 example, the calling program may set <I>val</I> to the equivalent short
189 option character.) Otherwise, <B>getopt_long()</B> returns 0, and
190 <I>flag</I> points to a variable which is set to <I>val</I> if the
191 option is found, but left unchanged if the option is not found.
192 <DT><I>val</I> <DD>
193 is the value to return, or to load into the variable pointed
194 to by <I>flag</I>.
195 </DL>
196 <P>
197
198 The last element of the array has to be filled with zeroes.
199 <P>
200
201 If <I>longindex</I> is not <B>NULL</B>, it
202 points to a variable which is set to the index of the long option relative to
203 <I>longopts</I>.
204
205 <P>
206
207 <B>getopt_long_only()</B> is like <B>getopt_long()</B>, but `-' as well
208 as `--' can indicate a long option. If an option that starts with `-'
209 (not `--') doesn't match a long option, but does match a short option,
210 it is parsed as a short option instead.
211 <A NAME="lbAE">&nbsp;</A>
212 <H2>RETURN VALUE</H2>
213
214 The
215 <B>getopt()</B>
216
217 function returns the option character if the option was found
218 successfully, `:' if there was a missing parameter for one of the
219 options, `?' for an unknown option character, or -1
220 for the end of the option list.
221 <P>
222
223 <B>getopt_long()</B> and <B>getopt_long_only()</B> also return the option
224 character when a short option is recognized. For a long option, they
225 return <I>val</I> if <I>flag</I> is <B>NULL</B>, and 0 otherwise. Error
226 and -1 returns are the same as for <B>getopt()</B>, plus `?' for an
227 ambiguous match or an extraneous parameter.
228 <A NAME="lbAF">&nbsp;</A>
229 <H2>ENVIRONMENT VARIABLES</H2>
230
231 <DL COMPACT>
232 <DT><FONT SIZE="-1"><B>POSIXLY_CORRECT</B>
233
234 </FONT>
235 <DD>
236 If this is set, then option processing stops as soon as a non-option
237 argument is encountered.
238 <DT><FONT SIZE="-1"><B>_&lt;PID&gt;_GNU_nonoption_argv_flags_</B>
239
240 </FONT>
241 <DD>
242 This variable was used by
243 <B>bash</B>
244
245 2.0 to communicate to GNU libc which arguments are the results of
246 wildcard expansion and so should not be considered as options. This
247 behaviour was removed in
248 <B>bash</B>
249
250 version 2.01, but the support remains in GNU libc.
251 </DL>
252 <A NAME="lbAG">&nbsp;</A>
253 <H2>EXAMPLE</H2>
254
255 The following example program, from the source code, illustrates the
256 use of
257 <B>getopt_long()</B>
258
259 with most of its features.
260 <PRE>
261
262 #include &lt;<A HREF="file:/usr/include/stdio.h">stdio.h</A>&gt;
263
264 int
265 main (argc, argv)
266 int argc;
267 char **argv;
268 {
269 int c;
270 int digit_optind = 0;
271
272 while (1)
273 {
274 int this_option_optind = optind ? optind : 1;
275 int option_index = 0;
276 static struct option long_options[] =
277 {
278 {&quot;add&quot;, 1, 0, 0},
279 {&quot;append&quot;, 0, 0, 0},
280 {&quot;delete&quot;, 1, 0, 0},
281 {&quot;verbose&quot;, 0, 0, 0},
282 {&quot;create&quot;, 1, 0, 'c'},
283 {&quot;file&quot;, 1, 0, 0},
284 {0, 0, 0, 0}
285 };
286
287 c = getopt_long (argc, argv, &quot;abc:d:012&quot;,
288 long_options, &amp;option_index);
289 if (c == -1)
290 break;
291
292 switch (c)
293 {
294 case 0:
295 printf (&quot;option %s&quot;, long_options[option_index].name);
296 if (optarg)
297 printf (&quot; with arg %s&quot;, optarg);
298 printf (&quot;\n&quot;);
299 break;
300
301 case '0':
302 case '1':
303 case '2':
304 if (digit_optind != 0 &amp;&amp; digit_optind != this_option_optind)
305 printf (&quot;digits occur in two different argv-elements.\n&quot;);
306 digit_optind = this_option_optind;
307 printf (&quot;option %c\n&quot;, c);
308 break;
309
310 case 'a':
311 printf (&quot;option a\n&quot;);
312 break;
313
314 case 'b':
315 printf (&quot;option b\n&quot;);
316 break;
317
318 case 'c':
319 printf (&quot;option c with value `%s'\n&quot;, optarg);
320 break;
321
322 case 'd':
323 printf (&quot;option d with value `%s'\n&quot;, optarg);
324 break;
325
326 case '?':
327 break;
328
329 default:
330 printf (&quot;?? getopt returned character code 0%o ??\n&quot;, c);
331 }
332 }
333
334 if (optind &lt; argc)
335 {
336 printf (&quot;non-option ARGV-elements: &quot;);
337 while (optind &lt; argc)
338 printf (&quot;%s &quot;, argv[optind++]);
339 printf (&quot;\n&quot;);
340 }
341
342 exit (0);
343 }
344 </PRE>
345
346 <A NAME="lbAH">&nbsp;</A>
347 <H2>BUGS</H2>
348
349 This manpage is confusing.
350 <P>
351
352 The POSIX.2 specification of
353 <B>getopt()</B>
354
355 has a technical error described in POSIX.2 Interpretation 150. The GNU
356 implementation (and probably all other implementations) implements the
357 correct behaviour rather than that specified.
358 <A NAME="lbAI">&nbsp;</A>
359 <H2>CONFORMING TO</H2>
360
361 <DL COMPACT>
362 <DT><B>getopt()</B>:<DD>
363 POSIX.2, provided the environment variable POSIXLY_CORRECT is set.
364 Otherwise, the elements of <I>argv</I> aren't really const, because we
365 permute them. We pretend they're const in the prototype to be
366 compatible with other systems.
367 <P>
368 <P>
369 </DL>
370
371 <HR>
372 <A NAME="index">&nbsp;</A><H2>Index</H2>
373 <DL>
374 <DT><A HREF="#lbAB">NAME</A><DD>
375 <DT><A HREF="#lbAC">SYNOPSIS</A><DD>
376 <DT><A HREF="#lbAD">DESCRIPTION</A><DD>
377 <DT><A HREF="#lbAE">RETURN VALUE</A><DD>
378 <DT><A HREF="#lbAF">ENVIRONMENT VARIABLES</A><DD>
379 <DT><A HREF="#lbAG">EXAMPLE</A><DD>
380 <DT><A HREF="#lbAH">BUGS</A><DD>
381 <DT><A HREF="#lbAI">CONFORMING TO</A><DD>
382 </DL>
383 <HR>
384 This document was created by
385 <A HREF="http://localhost/cgi-bin/man/man2html">man2html</A>,
386 using the manual pages.<BR>
387 Time: 19:30:38 GMT, July 23, 2000
388 </BODY>
389 </HTML>