lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | @node Getopt, Argp, , Parsing Program Arguments |
| 2 | @section Parsing program options using @code{getopt} |
| 3 | |
| 4 | The @code{getopt} and @code{getopt_long} functions automate some of the |
| 5 | chore 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 | |
| 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 |
| 20 | @file{unistd.h}. |
| 21 | @pindex unistd.h |
| 22 | |
| 23 | @comment unistd.h |
| 24 | @comment POSIX.2 |
| 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{?} |
| 31 | to indicate an error. |
| 32 | @end deftypevar |
| 33 | |
| 34 | @comment unistd.h |
| 35 | @comment POSIX.2 |
| 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 |
| 40 | messages. |
| 41 | @end deftypevar |
| 42 | |
| 43 | @comment unistd.h |
| 44 | @comment POSIX.2 |
| 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}. |
| 51 | @end deftypevar |
| 52 | |
| 53 | @comment unistd.h |
| 54 | @comment POSIX.2 |
| 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. |
| 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 |
| 85 | The @code{getopt} function gets the next option argument from the |
| 86 | argument list specified by the @var{argv} and @var{argc} arguments. |
| 87 | Normally these values come directly from the arguments received by |
| 88 | @code{main}. |
| 89 | |
| 90 | The @var{options} argument is a string that specifies the option |
| 91 | characters that are valid for this program. An option character in this |
| 92 | string can be followed by a colon (@samp{:}) to indicate that it takes a |
| 93 | required 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 |
| 97 | non-options @var{argv} elements. The special argument @samp{--} forces |
| 98 | in all cases the end of option scanning. |
| 99 | |
| 100 | @itemize @bullet |
| 101 | @item |
| 102 | The default is to permute the contents of @var{argv} while scanning it |
| 103 | so that eventually all the non-options are at the end. This allows |
| 104 | options to be given in any order, even with programs that were not |
| 105 | written to expect this. |
| 106 | |
| 107 | @item |
| 108 | If the @var{options} argument string begins with a hyphen (@samp{-}), this |
| 109 | is treated specially. It permits arguments that are not options to be |
| 110 | returned as if they were associated with option character @samp{\1}. |
| 111 | |
| 112 | @item |
| 113 | POSIX demands the following behavior: The first non-option stops option |
| 114 | processing. This mode is selected by either setting the environment |
| 115 | variable @code{POSIXLY_CORRECT} or beginning the @var{options} argument |
| 116 | string with a plus sign (@samp{+}). |
| 117 | @end itemize |
| 118 | |
| 119 | The @code{getopt} function returns the option character for the next |
| 120 | command line option. When no more option arguments are available, it |
| 121 | returns @code{-1}. There may still be more non-option arguments; you |
| 122 | must compare the external variable @code{optind} against the @var{argc} |
| 123 | parameter to check this. |
| 124 | |
| 125 | If the option has an argument, @code{getopt} returns the argument by |
| 126 | storing it in the variable @var{optarg}. You don't ordinarily need to |
| 127 | copy 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 | |
| 130 | If @code{getopt} finds an option character in @var{argv} that was not |
| 131 | included in @var{options}, or a missing option argument, it returns |
| 132 | @samp{?} and sets the external variable @code{optopt} to the actual |
| 133 | option character. If the first character of @var{options} is a colon |
| 134 | (@samp{:}), then @code{getopt} returns @samp{:} instead of @samp{?} to |
| 135 | indicate a missing option argument. In addition, if the external |
| 136 | variable @code{opterr} is nonzero (which is the default), @code{getopt} |
| 137 | prints an error message. |
| 138 | @end deftypefun |
| 139 | |
| 140 | @node Example of Getopt |
| 141 | @subsection Example of Parsing Arguments with @code{getopt} |
| 142 | |
| 143 | Here is an example showing how @code{getopt} is typically used. The |
| 144 | key points to notice are: |
| 145 | |
| 146 | @itemize @bullet |
| 147 | @item |
| 148 | Normally, @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 |
| 152 | A @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 |
| 154 | is used later in the program. |
| 155 | |
| 156 | @item |
| 157 | A 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 | |
| 164 | Here are some examples showing what this program prints with different |
| 165 | combinations of arguments: |
| 166 | |
| 167 | @smallexample |
| 168 | % testopt |
| 169 | aflag = 0, bflag = 0, cvalue = (null) |
| 170 | |
| 171 | % testopt -a -b |
| 172 | aflag = 1, bflag = 1, cvalue = (null) |
| 173 | |
| 174 | % testopt -ab |
| 175 | aflag = 1, bflag = 1, cvalue = (null) |
| 176 | |
| 177 | % testopt -c foo |
| 178 | aflag = 0, bflag = 0, cvalue = foo |
| 179 | |
| 180 | % testopt -cfoo |
| 181 | aflag = 0, bflag = 0, cvalue = foo |
| 182 | |
| 183 | % testopt arg1 |
| 184 | aflag = 0, bflag = 0, cvalue = (null) |
| 185 | Non-option argument arg1 |
| 186 | |
| 187 | % testopt -a arg1 |
| 188 | aflag = 1, bflag = 0, cvalue = (null) |
| 189 | Non-option argument arg1 |
| 190 | |
| 191 | % testopt -c foo arg1 |
| 192 | aflag = 0, bflag = 0, cvalue = foo |
| 193 | Non-option argument arg1 |
| 194 | |
| 195 | % testopt -a -- -b |
| 196 | aflag = 1, bflag = 0, cvalue = (null) |
| 197 | Non-option argument -b |
| 198 | |
| 199 | % testopt -a - |
| 200 | aflag = 1, bflag = 0, cvalue = (null) |
| 201 | Non-option argument - |
| 202 | @end smallexample |
| 203 | |
| 204 | @node Getopt Long Options |
| 205 | @subsection Parsing Long Options with @code{getopt_long} |
| 206 | |
| 207 | To accept GNU-style long options as well as single-character options, |
| 208 | use @code{getopt_long} instead of @code{getopt}. This function is |
| 209 | declared in @file{getopt.h}, not @file{unistd.h}. You should make every |
| 210 | program accept long options if it uses any options, for this takes |
| 211 | little 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} |
| 216 | This structure describes a single long option name for the sake of |
| 217 | @code{getopt_long}. The argument @var{longopts} must be an array of |
| 218 | these structures, one for each long option. Terminate the array with an |
| 219 | element containing all zeros. |
| 220 | |
| 221 | The @code{struct option} structure has these fields: |
| 222 | |
| 223 | @table @code |
| 224 | @item const char *name |
| 225 | This field is the name of the option. It is a string. |
| 226 | |
| 227 | @item int has_arg |
| 228 | This field says whether the option takes an argument. It is an integer, |
| 229 | and 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 |
| 234 | These fields control how to report or act on the option when it occurs. |
| 235 | |
| 236 | If @code{flag} is a null pointer, then the @code{val} is a value which |
| 237 | identifies this option. Often these values are chosen to uniquely |
| 238 | identify particular long options. |
| 239 | |
| 240 | If @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 |
| 243 | was 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. |
| 252 | Decode options from the vector @var{argv} (whose length is @var{argc}). |
| 253 | The argument @var{shortopts} describes the short options to accept, just as |
| 254 | it does in @code{getopt}. The argument @var{longopts} describes the long |
| 255 | options to accept (see above). |
| 256 | |
| 257 | When @code{getopt_long} encounters a short option, it does the same |
| 258 | thing that @code{getopt} would do: it returns the character code for the |
| 259 | option, and stores the options argument (if it has one) in @code{optarg}. |
| 260 | |
| 261 | When @code{getopt_long} encounters a long option, it takes actions based |
| 262 | on the @code{flag} and @code{val} fields of the definition of that |
| 263 | option. |
| 264 | |
| 265 | If @code{flag} is a null pointer, then @code{getopt_long} returns the |
| 266 | contents of @code{val} to indicate which option it found. You should |
| 267 | arrange distinct values in the @code{val} field for options with |
| 268 | different meanings, so you can decode these values after |
| 269 | @code{getopt_long} returns. If the long option is equivalent to a short |
| 270 | option, you can use the short option's character code in @code{val}. |
| 271 | |
| 272 | If @code{flag} is not a null pointer, that means this option should just |
| 273 | set a flag in the program. The flag is a variable of type @code{int} |
| 274 | that you define. Put the address of the flag in the @code{flag} field. |
| 275 | Put in the @code{val} field the value you would like this option to |
| 276 | store in the flag. In this case, @code{getopt_long} returns @code{0}. |
| 277 | |
| 278 | For 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 |
| 282 | long options either by the values in their @code{val} fields or by their |
| 283 | indices. You can also distinguish in this way among long options that |
| 284 | set flags. |
| 285 | |
| 286 | When a long option has an argument, @code{getopt_long} puts the argument |
| 287 | value in the variable @code{optarg} before returning. When the option |
| 288 | has no argument, the value in @code{optarg} is a null pointer. This is |
| 289 | how you can tell whether an optional argument was supplied. |
| 290 | |
| 291 | When @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 | |
| 296 | Since long option names were used before the @code{getopt_long} |
| 297 | options was invented there are program interfaces which require programs |
| 298 | to recognize options like @w{@samp{-option value}} instead of |
| 299 | @w{@samp{--option value}}. To enable these programs to use the GNU |
| 300 | getopt 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 | |
| 308 | The @code{getopt_long_only} function is equivalent to the |
| 309 | @code{getopt_long} function but it allows to specify the user of the |
| 310 | application to pass long options with only @samp{-} instead of |
| 311 | @samp{--}. The @samp{--} prefix is still recognized but instead of |
| 312 | looking through the short options if a @samp{-} is seen it is first |
| 313 | tried whether this parameter names a long option. If not, it is parsed |
| 314 | as a short option. |
| 315 | |
| 316 | Assuming @code{getopt_long_only} is used starting an application with |
| 317 | |
| 318 | @smallexample |
| 319 | app -foo |
| 320 | @end smallexample |
| 321 | |
| 322 | @noindent |
| 323 | the @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}, |
| 325 | and 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 |