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 **@var{argv}, const char *@var{options})
63 The @code{getopt} function gets the next option argument from the
64 argument list specified by the @var{argv} and @var{argc} arguments.
65 Normally these values come directly from the arguments received by
68 The @var{options} argument is a string that specifies the option
69 characters that are valid for this program. An option character in this
70 string can be followed by a colon (@samp{:}) to indicate that it takes a
71 required argument. If an option character is followed by two colons
72 (@samp{::}), its argument is optional; this is a GNU extension.
74 If the @var{options} argument string begins with a hyphen (@samp{-}), this
75 is treated specially. It permits arguments that are not options to be
76 returned as if they were associated with option character @samp{\0}.
78 The @code{getopt} function returns the option character for the next
79 command line option. When no more option arguments are available, it
80 returns @code{-1}. There may still be more non-option arguments; you
81 must compare the external variable @code{optind} against the @var{argc}
82 parameter to check this.
84 If the option has an argument, @code{getopt} returns the argument by
85 storing it in the variable @var{optarg}. You don't ordinarily need to
86 copy the @code{optarg} string, since it is a pointer into the original
87 @var{argv} array, not into a static area that might be overwritten.
89 If @code{getopt} finds an option character in @var{argv} that was not
90 included in @var{options}, or a missing option argument, it returns
91 @samp{?} and sets the external variable @code{optopt} to the actual
92 option character. If the first character of @var{options} is a colon
93 (@samp{:}), then @code{getopt} returns @samp{:} instead of @samp{?} to
94 indicate a missing option argument. In addition, if the external
95 variable @code{opterr} is nonzero (which is the default), @code{getopt}
96 prints an error message.
99 @node Example of Getopt
100 @subsection Example of Parsing Arguments with @code{getopt}
102 Here is an example showing how @code{getopt} is typically used. The
103 key points to notice are:
107 Normally, @code{getopt} is called in a loop. When @code{getopt} returns
108 @code{-1}, indicating no more options are present, the loop terminates.
111 A @code{switch} statement is used to dispatch on the return value from
112 @code{getopt}. In typical use, each case just sets a variable that
113 is used later in the program.
116 A second loop is used to process the remaining non-option arguments.
120 @include testopt.c.texi
123 Here are some examples showing what this program prints with different
124 combinations of arguments:
128 aflag = 0, bflag = 0, cvalue = (null)
131 aflag = 1, bflag = 1, cvalue = (null)
134 aflag = 1, bflag = 1, cvalue = (null)
137 aflag = 0, bflag = 0, cvalue = foo
140 aflag = 0, bflag = 0, cvalue = foo
143 aflag = 0, bflag = 0, cvalue = (null)
144 Non-option argument arg1
147 aflag = 1, bflag = 0, cvalue = (null)
148 Non-option argument arg1
150 % testopt -c foo arg1
151 aflag = 0, bflag = 0, cvalue = foo
152 Non-option argument arg1
155 aflag = 1, bflag = 0, cvalue = (null)
156 Non-option argument -b
159 aflag = 1, bflag = 0, cvalue = (null)
160 Non-option argument -
163 @node Getopt Long Options
164 @subsection Parsing Long Options with @code{getopt_long}
166 To accept GNU-style long options as well as single-character options,
167 use @code{getopt_long} instead of @code{getopt}. This function is
168 declared in @file{getopt.h}, not @file{unistd.h}. You should make every
169 program accept long options if it uses any options, for this takes
170 little extra work and helps beginners remember how to use the program.
174 @deftp {Data Type} {struct option}
175 This structure describes a single long option name for the sake of
176 @code{getopt_long}. The argument @var{longopts} must be an array of
177 these structures, one for each long option. Terminate the array with an
178 element containing all zeros.
180 The @code{struct option} structure has these fields:
183 @item const char *name
184 This field is the name of the option. It is a string.
187 This field says whether the option takes an argument. It is an integer,
188 and there are three legitimate values: @w{@code{no_argument}},
189 @code{required_argument} and @code{optional_argument}.
193 These fields control how to report or act on the option when it occurs.
195 If @code{flag} is a null pointer, then the @code{val} is a value which
196 identifies this option. Often these values are chosen to uniquely
197 identify particular long options.
199 If @code{flag} is not a null pointer, it should be the address of an
200 @code{int} variable which is the flag for this option. The value in
201 @code{val} is the value to store in the flag to indicate that the option
208 @deftypefun int getopt_long (int @var{argc}, char **@var{argv}, const char *@var{shortopts}, struct option *@var{longopts}, int *@var{indexptr})
209 Decode options from the vector @var{argv} (whose length is @var{argc}).
210 The argument @var{shortopts} describes the short options to accept, just as
211 it does in @code{getopt}. The argument @var{longopts} describes the long
212 options to accept (see above).
214 When @code{getopt_long} encounters a short option, it does the same
215 thing that @code{getopt} would do: it returns the character code for the
216 option, and stores the options argument (if it has one) in @code{optarg}.
218 When @code{getopt_long} encounters a long option, it takes actions based
219 on the @code{flag} and @code{val} fields of the definition of that
222 If @code{flag} is a null pointer, then @code{getopt_long} returns the
223 contents of @code{val} to indicate which option it found. You should
224 arrange distinct values in the @code{val} field for options with
225 different meanings, so you can decode these values after
226 @code{getopt_long} returns. If the long option is equivalent to a short
227 option, you can use the short option's character code in @code{val}.
229 If @code{flag} is not a null pointer, that means this option should just
230 set a flag in the program. The flag is a variable of type @code{int}
231 that you define. Put the address of the flag in the @code{flag} field.
232 Put in the @code{val} field the value you would like this option to
233 store in the flag. In this case, @code{getopt_long} returns @code{0}.
235 For any long option, @code{getopt_long} tells you the index in the array
236 @var{longopts} of the options definition, by storing it into
237 @code{*@var{indexptr}}. You can get the name of the option with
238 @code{@var{longopts}[*@var{indexptr}].name}. So you can distinguish among
239 long options either by the values in their @code{val} fields or by their
240 indices. You can also distinguish in this way among long options that
243 When a long option has an argument, @code{getopt_long} puts the argument
244 value in the variable @code{optarg} before returning. When the option
245 has no argument, the value in @code{optarg} is a null pointer. This is
246 how you can tell whether an optional argument was supplied.
248 When @code{getopt_long} has no more options to handle, it returns
249 @code{-1}, and leaves in the variable @code{optind} the index in
250 @var{argv} of the next remaining argument.
253 @node Getopt Long Option Example
254 @subsection Example of Parsing Long Options with @code{getopt_long}
257 @include longopt.c.texi