blob: aa4134b0e59fedf89342c95b06141174d492de48 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001@node Getopt, Argp, , Parsing Program Arguments
2@section Parsing program options using @code{getopt}
3
4The @code{getopt} and @code{getopt_long} functions automate some of the
5chore involved in parsing typical unix command line options.
6
7@menu
8* Using Getopt:: Using the @code{getopt} function.
9* Example of Getopt:: An example of parsing options with @code{getopt}.
10* Getopt Long Options:: GNU suggests utilities accept long-named
11 options; here is one way to do.
12* Getopt Long Option Example:: An example of using @code{getopt_long}.
13@end menu
14
15@node Using Getopt, Example of Getopt, , Getopt
16@subsection Using the @code{getopt} function
17
18Here are the details about how to call the @code{getopt} function. To
19use this facility, your program must include the header file
20@file{unistd.h}.
21@pindex unistd.h
22
23@comment unistd.h
24@comment POSIX.2
25@deftypevar int opterr
26If the value of this variable is nonzero, then @code{getopt} prints an
27error message to the standard error stream if it encounters an unknown
28option character or an option with a missing required argument. This is
29the default behavior. If you set this variable to zero, @code{getopt}
30does not print any messages, but it still returns the character @code{?}
31to indicate an error.
32@end deftypevar
33
34@comment unistd.h
35@comment POSIX.2
36@deftypevar int optopt
37When @code{getopt} encounters an unknown option character or an option
38with a missing required argument, it stores that option character in
39this variable. You can use this for providing your own diagnostic
40messages.
41@end deftypevar
42
43@comment unistd.h
44@comment POSIX.2
45@deftypevar int optind
46This variable is set by @code{getopt} to the index of the next element
47of the @var{argv} array to be processed. Once @code{getopt} has found
48all of the option arguments, you can use this variable to determine
49where the remaining non-option arguments begin. The initial value of
50this variable is @code{1}.
51@end deftypevar
52
53@comment unistd.h
54@comment POSIX.2
55@deftypevar {char *} optarg
56This variable is set by @code{getopt} to point at the value of the
57option argument, for those options that accept arguments.
58@end deftypevar
59
60@comment unistd.h
61@comment POSIX.2
62@deftypefun int getopt (int @var{argc}, char *const *@var{argv}, const char *@var{options})
63@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
64@c Swapping elements of passed-in argv may be partial in case of
65@c cancellation. Gettext brings about a whole lot of AS and AC safety
66@c issues. The getopt API involves returning values in the
67@c non-thread-specific optarg variable, which adds another thread-safety
68@c issue. Given print_errors, it may output errors to stderr, which may
69@c self-deadlock, leak locks, or encounter (in a signal handler) or
70@c leave (in case of cancellation) stderr in an inconsistent state.
71@c Various implicit, indirect uses of malloc, in uses of memstream and
72@c asprintf for error-printing, bring about the usual malloc issues.
73@c (The explicit use of malloc in a conditional situation in
74@c _getopt_initialize is never exercised in glibc.)
75@c
76@c _getopt_internal
77@c _getopt_internal_r
78@c gettext
79@c _getopt_initialize
80@c getenv
81@c malloc if USE_NONOPTION_FLAGS, never defined in libc
82@c open_memstream
83@c lockfile, unlockfile, __fxprintf -> stderr
84@c asprintf
85The @code{getopt} function gets the next option argument from the
86argument list specified by the @var{argv} and @var{argc} arguments.
87Normally these values come directly from the arguments received by
88@code{main}.
89
90The @var{options} argument is a string that specifies the option
91characters that are valid for this program. An option character in this
92string can be followed by a colon (@samp{:}) to indicate that it takes a
93required argument. If an option character is followed by two colons
94(@samp{::}), its argument is optional; this is a GNU extension.
95
96@code{getopt} has three ways to deal with options that follow
97non-options @var{argv} elements. The special argument @samp{--} forces
98in all cases the end of option scanning.
99
100@itemize @bullet
101@item
102The default is to permute the contents of @var{argv} while scanning it
103so that eventually all the non-options are at the end. This allows
104options to be given in any order, even with programs that were not
105written to expect this.
106
107@item
108If the @var{options} argument string begins with a hyphen (@samp{-}), this
109is treated specially. It permits arguments that are not options to be
110returned as if they were associated with option character @samp{\1}.
111
112@item
113POSIX demands the following behavior: The first non-option stops option
114processing. This mode is selected by either setting the environment
115variable @code{POSIXLY_CORRECT} or beginning the @var{options} argument
116string with a plus sign (@samp{+}).
117@end itemize
118
119The @code{getopt} function returns the option character for the next
120command line option. When no more option arguments are available, it
121returns @code{-1}. There may still be more non-option arguments; you
122must compare the external variable @code{optind} against the @var{argc}
123parameter to check this.
124
125If the option has an argument, @code{getopt} returns the argument by
126storing it in the variable @var{optarg}. You don't ordinarily need to
127copy the @code{optarg} string, since it is a pointer into the original
128@var{argv} array, not into a static area that might be overwritten.
129
130If @code{getopt} finds an option character in @var{argv} that was not
131included in @var{options}, or a missing option argument, it returns
132@samp{?} and sets the external variable @code{optopt} to the actual
133option character. If the first character of @var{options} is a colon
134(@samp{:}), then @code{getopt} returns @samp{:} instead of @samp{?} to
135indicate a missing option argument. In addition, if the external
136variable @code{opterr} is nonzero (which is the default), @code{getopt}
137prints an error message.
138@end deftypefun
139
140@node Example of Getopt
141@subsection Example of Parsing Arguments with @code{getopt}
142
143Here is an example showing how @code{getopt} is typically used. The
144key points to notice are:
145
146@itemize @bullet
147@item
148Normally, @code{getopt} is called in a loop. When @code{getopt} returns
149@code{-1}, indicating no more options are present, the loop terminates.
150
151@item
152A @code{switch} statement is used to dispatch on the return value from
153@code{getopt}. In typical use, each case just sets a variable that
154is used later in the program.
155
156@item
157A second loop is used to process the remaining non-option arguments.
158@end itemize
159
160@smallexample
161@include testopt.c.texi
162@end smallexample
163
164Here are some examples showing what this program prints with different
165combinations of arguments:
166
167@smallexample
168% testopt
169aflag = 0, bflag = 0, cvalue = (null)
170
171% testopt -a -b
172aflag = 1, bflag = 1, cvalue = (null)
173
174% testopt -ab
175aflag = 1, bflag = 1, cvalue = (null)
176
177% testopt -c foo
178aflag = 0, bflag = 0, cvalue = foo
179
180% testopt -cfoo
181aflag = 0, bflag = 0, cvalue = foo
182
183% testopt arg1
184aflag = 0, bflag = 0, cvalue = (null)
185Non-option argument arg1
186
187% testopt -a arg1
188aflag = 1, bflag = 0, cvalue = (null)
189Non-option argument arg1
190
191% testopt -c foo arg1
192aflag = 0, bflag = 0, cvalue = foo
193Non-option argument arg1
194
195% testopt -a -- -b
196aflag = 1, bflag = 0, cvalue = (null)
197Non-option argument -b
198
199% testopt -a -
200aflag = 1, bflag = 0, cvalue = (null)
201Non-option argument -
202@end smallexample
203
204@node Getopt Long Options
205@subsection Parsing Long Options with @code{getopt_long}
206
207To accept GNU-style long options as well as single-character options,
208use @code{getopt_long} instead of @code{getopt}. This function is
209declared in @file{getopt.h}, not @file{unistd.h}. You should make every
210program accept long options if it uses any options, for this takes
211little extra work and helps beginners remember how to use the program.
212
213@comment getopt.h
214@comment GNU
215@deftp {Data Type} {struct option}
216This structure describes a single long option name for the sake of
217@code{getopt_long}. The argument @var{longopts} must be an array of
218these structures, one for each long option. Terminate the array with an
219element containing all zeros.
220
221The @code{struct option} structure has these fields:
222
223@table @code
224@item const char *name
225This field is the name of the option. It is a string.
226
227@item int has_arg
228This field says whether the option takes an argument. It is an integer,
229and there are three legitimate values: @w{@code{no_argument}},
230@code{required_argument} and @code{optional_argument}.
231
232@item int *flag
233@itemx int val
234These fields control how to report or act on the option when it occurs.
235
236If @code{flag} is a null pointer, then the @code{val} is a value which
237identifies this option. Often these values are chosen to uniquely
238identify particular long options.
239
240If @code{flag} is not a null pointer, it should be the address of an
241@code{int} variable which is the flag for this option. The value in
242@code{val} is the value to store in the flag to indicate that the option
243was seen.
244@end table
245@end deftp
246
247@comment getopt.h
248@comment GNU
249@deftypefun int getopt_long (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
250@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
251@c Same issues as getopt.
252Decode options from the vector @var{argv} (whose length is @var{argc}).
253The argument @var{shortopts} describes the short options to accept, just as
254it does in @code{getopt}. The argument @var{longopts} describes the long
255options to accept (see above).
256
257When @code{getopt_long} encounters a short option, it does the same
258thing that @code{getopt} would do: it returns the character code for the
259option, and stores the options argument (if it has one) in @code{optarg}.
260
261When @code{getopt_long} encounters a long option, it takes actions based
262on the @code{flag} and @code{val} fields of the definition of that
263option.
264
265If @code{flag} is a null pointer, then @code{getopt_long} returns the
266contents of @code{val} to indicate which option it found. You should
267arrange distinct values in the @code{val} field for options with
268different meanings, so you can decode these values after
269@code{getopt_long} returns. If the long option is equivalent to a short
270option, you can use the short option's character code in @code{val}.
271
272If @code{flag} is not a null pointer, that means this option should just
273set a flag in the program. The flag is a variable of type @code{int}
274that you define. Put the address of the flag in the @code{flag} field.
275Put in the @code{val} field the value you would like this option to
276store in the flag. In this case, @code{getopt_long} returns @code{0}.
277
278For any long option, @code{getopt_long} tells you the index in the array
279@var{longopts} of the options definition, by storing it into
280@code{*@var{indexptr}}. You can get the name of the option with
281@code{@var{longopts}[*@var{indexptr}].name}. So you can distinguish among
282long options either by the values in their @code{val} fields or by their
283indices. You can also distinguish in this way among long options that
284set flags.
285
286When a long option has an argument, @code{getopt_long} puts the argument
287value in the variable @code{optarg} before returning. When the option
288has no argument, the value in @code{optarg} is a null pointer. This is
289how you can tell whether an optional argument was supplied.
290
291When @code{getopt_long} has no more options to handle, it returns
292@code{-1}, and leaves in the variable @code{optind} the index in
293@var{argv} of the next remaining argument.
294@end deftypefun
295
296Since long option names were used before the @code{getopt_long}
297options was invented there are program interfaces which require programs
298to recognize options like @w{@samp{-option value}} instead of
299@w{@samp{--option value}}. To enable these programs to use the GNU
300getopt functionality there is one more function available.
301
302@comment getopt.h
303@comment GNU
304@deftypefun int getopt_long_only (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
305@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
306@c Same issues as getopt.
307
308The @code{getopt_long_only} function is equivalent to the
309@code{getopt_long} function but it allows to specify the user of the
310application to pass long options with only @samp{-} instead of
311@samp{--}. The @samp{--} prefix is still recognized but instead of
312looking through the short options if a @samp{-} is seen it is first
313tried whether this parameter names a long option. If not, it is parsed
314as a short option.
315
316Assuming @code{getopt_long_only} is used starting an application with
317
318@smallexample
319 app -foo
320@end smallexample
321
322@noindent
323the @code{getopt_long_only} will first look for a long option named
324@samp{foo}. If this is not found, the short options @samp{f}, @samp{o},
325and again @samp{o} are recognized.
326@end deftypefun
327
328@node Getopt Long Option Example
329@subsection Example of Parsing Long Options with @code{getopt_long}
330
331@smallexample
332@include longopt.c.texi
333@end smallexample