1 @node Getopt, Argp, , Parsing Program Arguments
2 @section Parsing program options using @code{getopt}
4 The @code{getopt} and @code{getopt_long} functions automate some of the
5 chore involved in parsing typical unix command line options.
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}.
15 @node Using Getopt, Example of Getopt, , Getopt
16 @subsection Using the @code{getopt} function
18 Here are the details about how to call the @code{getopt} function. To
19 use this facility, your program must include the header file
25 @deftypevar int opterr
26 If the value of this variable is nonzero, then @code{getopt} prints an
27 error message to the standard error stream if it encounters an unknown
28 option character or an option with a missing required argument. This is
29 the default behavior. If you set this variable to zero, @code{getopt}
30 does not print any messages, but it still returns the character @code{?}
36 @deftypevar int optopt
37 When @code{getopt} encounters an unknown option character or an option
38 with a missing required argument, it stores that option character in
39 this variable. You can use this for providing your own diagnostic
45 @deftypevar int optind
46 This variable is set by @code{getopt} to the index of the next element
47 of the @var{argv} array to be processed. Once @code{getopt} has found
48 all of the option arguments, you can use this variable to determine
49 where the remaining non-option arguments begin. The initial value of
50 this variable is @code{1}.
55 @deftypevar {char *} optarg
56 This variable is set by @code{getopt} to point at the value of the
57 option argument, for those options that accept arguments.
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.
80 @c lockfile, unlockfile, __fxprintf -> stderr
82 The @code{getopt} function gets the next option argument from the
83 argument list specified by the @var{argv} and @var{argc} arguments.
84 Normally these values come directly from the arguments received by
87 The @var{options} argument is a string that specifies the option
88 characters that are valid for this program. An option character in this
89 string can be followed by a colon (@samp{:}) to indicate that it takes a
90 required argument. If an option character is followed by two colons
91 (@samp{::}), its argument is optional; this is a GNU extension.
93 @code{getopt} has three ways to deal with options that follow
94 non-options @var{argv} elements. The special argument @samp{--} forces
95 in all cases the end of option scanning.
99 The default is to permute the contents of @var{argv} while scanning it
100 so that eventually all the non-options are at the end. This allows
101 options to be given in any order, even with programs that were not
102 written to expect this.
105 If the @var{options} argument string begins with a hyphen (@samp{-}), this
106 is treated specially. It permits arguments that are not options to be
107 returned as if they were associated with option character @samp{\1}.
110 POSIX demands the following behavior: the first non-option stops option
111 processing. This mode is selected by either setting the environment
112 variable @code{POSIXLY_CORRECT} or beginning the @var{options} argument
113 string with a plus sign (@samp{+}).
116 The @code{getopt} function returns the option character for the next
117 command line option. When no more option arguments are available, it
118 returns @code{-1}. There may still be more non-option arguments; you
119 must compare the external variable @code{optind} against the @var{argc}
120 parameter to check this.
122 If the option has an argument, @code{getopt} returns the argument by
123 storing it in the variable @var{optarg}. You don't ordinarily need to
124 copy the @code{optarg} string, since it is a pointer into the original
125 @var{argv} array, not into a static area that might be overwritten.
127 If @code{getopt} finds an option character in @var{argv} that was not
128 included in @var{options}, or a missing option argument, it returns
129 @samp{?} and sets the external variable @code{optopt} to the actual
130 option character. If the first character of @var{options} is a colon
131 (@samp{:}), then @code{getopt} returns @samp{:} instead of @samp{?} to
132 indicate a missing option argument. In addition, if the external
133 variable @code{opterr} is nonzero (which is the default), @code{getopt}
134 prints an error message.
137 @node Example of Getopt
138 @subsection Example of Parsing Arguments with @code{getopt}
140 Here is an example showing how @code{getopt} is typically used. The
141 key points to notice are:
145 Normally, @code{getopt} is called in a loop. When @code{getopt} returns
146 @code{-1}, indicating no more options are present, the loop terminates.
149 A @code{switch} statement is used to dispatch on the return value from
150 @code{getopt}. In typical use, each case just sets a variable that
151 is used later in the program.
154 A second loop is used to process the remaining non-option arguments.
158 @include testopt.c.texi
161 Here are some examples showing what this program prints with different
162 combinations of arguments:
166 aflag = 0, bflag = 0, cvalue = (null)
169 aflag = 1, bflag = 1, cvalue = (null)
172 aflag = 1, bflag = 1, cvalue = (null)
175 aflag = 0, bflag = 0, cvalue = foo
178 aflag = 0, bflag = 0, cvalue = foo
181 aflag = 0, bflag = 0, cvalue = (null)
182 Non-option argument arg1
185 aflag = 1, bflag = 0, cvalue = (null)
186 Non-option argument arg1
188 % testopt -c foo arg1
189 aflag = 0, bflag = 0, cvalue = foo
190 Non-option argument arg1
193 aflag = 1, bflag = 0, cvalue = (null)
194 Non-option argument -b
197 aflag = 1, bflag = 0, cvalue = (null)
198 Non-option argument -
201 @node Getopt Long Options
202 @subsection Parsing Long Options with @code{getopt_long}
204 To accept GNU-style long options as well as single-character options,
205 use @code{getopt_long} instead of @code{getopt}. This function is
206 declared in @file{getopt.h}, not @file{unistd.h}. You should make every
207 program accept long options if it uses any options, for this takes
208 little extra work and helps beginners remember how to use the program.
212 @deftp {Data Type} {struct option}
213 This structure describes a single long option name for the sake of
214 @code{getopt_long}. The argument @var{longopts} must be an array of
215 these structures, one for each long option. Terminate the array with an
216 element containing all zeros.
218 The @code{struct option} structure has these fields:
221 @item const char *name
222 This field is the name of the option. It is a string.
225 This field says whether the option takes an argument. It is an integer,
226 and there are three legitimate values: @w{@code{no_argument}},
227 @code{required_argument} and @code{optional_argument}.
231 These fields control how to report or act on the option when it occurs.
233 If @code{flag} is a null pointer, then the @code{val} is a value which
234 identifies this option. Often these values are chosen to uniquely
235 identify particular long options.
237 If @code{flag} is not a null pointer, it should be the address of an
238 @code{int} variable which is the flag for this option. The value in
239 @code{val} is the value to store in the flag to indicate that the option
246 @deftypefun int getopt_long (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
247 @safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
248 @c Same issues as getopt.
249 Decode options from the vector @var{argv} (whose length is @var{argc}).
250 The argument @var{shortopts} describes the short options to accept, just as
251 it does in @code{getopt}. The argument @var{longopts} describes the long
252 options to accept (see above).
254 When @code{getopt_long} encounters a short option, it does the same
255 thing that @code{getopt} would do: it returns the character code for the
256 option, and stores the option's argument (if it has one) in @code{optarg}.
258 When @code{getopt_long} encounters a long option, it takes actions based
259 on the @code{flag} and @code{val} fields of the definition of that
262 If @code{flag} is a null pointer, then @code{getopt_long} returns the
263 contents of @code{val} to indicate which option it found. You should
264 arrange distinct values in the @code{val} field for options with
265 different meanings, so you can decode these values after
266 @code{getopt_long} returns. If the long option is equivalent to a short
267 option, you can use the short option's character code in @code{val}.
269 If @code{flag} is not a null pointer, that means this option should just
270 set a flag in the program. The flag is a variable of type @code{int}
271 that you define. Put the address of the flag in the @code{flag} field.
272 Put in the @code{val} field the value you would like this option to
273 store in the flag. In this case, @code{getopt_long} returns @code{0}.
275 For any long option, @code{getopt_long} tells you the index in the array
276 @var{longopts} of the options definition, by storing it into
277 @code{*@var{indexptr}}. You can get the name of the option with
278 @code{@var{longopts}[*@var{indexptr}].name}. So you can distinguish among
279 long options either by the values in their @code{val} fields or by their
280 indices. You can also distinguish in this way among long options that
283 When a long option has an argument, @code{getopt_long} puts the argument
284 value in the variable @code{optarg} before returning. When the option
285 has no argument, the value in @code{optarg} is a null pointer. This is
286 how you can tell whether an optional argument was supplied.
288 When @code{getopt_long} has no more options to handle, it returns
289 @code{-1}, and leaves in the variable @code{optind} the index in
290 @var{argv} of the next remaining argument.
293 Since long option names were used before @code{getopt_long}
294 was invented there are program interfaces which require programs
295 to recognize options like @w{@samp{-option value}} instead of
296 @w{@samp{--option value}}. To enable these programs to use the GNU
297 getopt functionality there is one more function available.
301 @deftypefun int getopt_long_only (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
302 @safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
303 @c Same issues as getopt.
305 The @code{getopt_long_only} function is equivalent to the
306 @code{getopt_long} function but it allows the user of the
307 application to pass long options with only @samp{-} instead of
308 @samp{--}. The @samp{--} prefix is still recognized but instead of
309 looking through the short options if a @samp{-} is seen it is first
310 tried whether this parameter names a long option. If not, it is parsed
313 Assuming @code{getopt_long_only} is used starting an application with
320 the @code{getopt_long_only} will first look for a long option named
321 @samp{foo}. If this is not found, the short options @samp{f}, @samp{o},
322 and again @samp{o} are recognized.
325 @node Getopt Long Option Example
326 @subsection Example of Parsing Long Options with @code{getopt_long}
329 @include longopt.c.texi