1.\" Copyright (c) 1988, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" $OpenBSD: getopt.3,v 1.14 2000/12/15 14:15:27 aaron Exp $ 33.\" 34.Dd April 19, 1994 35.Dt GETOPT 3 36.Os 37.Sh NAME 38.Nm getopt 39.Nd get option character from command line argument list 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Vt extern char *optarg; 43.Vt extern int optind; 44.Vt extern int optopt; 45.Vt extern int opterr; 46.Vt extern int optreset; 47.Ft int 48.Fn getopt "int argc" "char * const *argv" "const char *optstring" 49.Sh DESCRIPTION 50The 51.Fn getopt 52function incrementally parses a command line argument list 53.Fa argv 54and returns the next known option character. 55An option character is 56.Dq known 57if it has been specified in the string of accepted option characters, 58.Fa optstring . 59.Pp 60The option string 61.Fa optstring 62may contain the following elements: individual characters and 63characters followed by a colon to indicate an option argument 64is to follow. 65For example, an option string 66.Qq x 67recognizes an option 68.Fl x , 69and an option string 70.Qq Li x: 71recognizes an option and argument 72.Fl x Ar argument . 73It does not matter to 74.Fn getopt 75if a following argument has leading whitespace. 76.Pp 77On return from 78.Fn getopt , 79.Va optarg 80points to an option argument, if it is anticipated, 81and the variable 82.Va optind 83contains the index to the next 84.Fa argv 85argument for a subsequent call 86to 87.Fn getopt . 88The variable 89.Va optopt 90saves the last known option character returned by 91.Fn getopt . 92.Pp 93The variables 94.Va opterr 95and 96.Va optind 97are both initialized to 1. 98The 99.Va optind 100variable may be set to another value before a set of calls to 101.Fn getopt 102in order to skip over more or less argv entries. 103.Pp 104In order to use 105.Fn getopt 106to evaluate multiple sets of arguments, or to evaluate a single set of 107arguments multiple times, 108the variable 109.Va optreset 110must be set to 1 before the second and each additional set of calls to 111.Fn getopt , 112and the variable 113.Va optind 114must be reinitialized. 115.Pp 116The 117.Fn getopt 118function returns \-1 when the argument list is exhausted. 119The interpretation of options in the argument list may be cancelled 120by the option 121.Ql -- 122(double dash) which causes 123.Fn getopt 124to signal the end of argument processing and returns \-1. 125When all options have been processed (i.e., up to the first non-option 126argument), 127.Fn getopt 128returns \-1. 129.Sh EXAMPLES 130.Bd -literal -compact 131extern char *optarg; 132extern int optind; 133int bflag, ch, fd; 134 135bflag = 0; 136while ((ch = getopt(argc, argv, "bf:")) != -1) { 137 switch (ch) { 138 case 'b': 139 bflag = 1; 140 break; 141 case 'f': 142 if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 143 (void)fprintf(stderr, 144 "myname: %s: %s\en", optarg, strerror(errno)); 145 exit(1); 146 } 147 break; 148 case '?': 149 default: 150 usage(); 151 } 152} 153argc -= optind; 154argv += optind; 155.Ed 156.Sh SEE ALSO 157.Xr getopt 1 , 158.Xr getsubopt 3 159.Sh DIAGNOSTICS 160If the 161.Fn getopt 162function encounters a character not found in the string 163.Va optstring 164or detects 165a missing option argument it writes an error message to 166.Em stderr 167and returns 168.Ql ? . 169Setting 170.Va opterr 171to a zero will disable these error messages. 172If 173.Va optstring 174has a leading 175.Ql \&: 176then a missing option argument causes a 177.Ql \&: 178to be returned in addition to suppressing any error messages. 179.Pp 180Option arguments are allowed to begin with 181.Ql - ; 182this is reasonable but reduces the amount of error checking possible. 183.Sh EXTENSIONS 184The 185.Va optreset 186variable was added to make it possible to call the 187.Fn getopt 188function multiple times. 189This is an extension to the 190.St -p1003.2 191specification. 192.Sh HISTORY 193The 194.Fn getopt 195function appeared in 196.Bx 4.3 . 197.Sh BUGS 198The 199.Fn getopt 200function was once specified to return 201.Dv EOF 202instead of \-1. 203This was changed by 204.St -p1003.2-92 205to decouple 206.Fn getopt 207from 208.Pa <stdio.h> . 209.Pp 210A single dash 211.Pq Ql - 212may be specified as a character in 213.Fa optstring , 214however it should 215.Em never 216have an argument associated with it. 217This allows 218.Fn getopt 219to be used with programs that expect 220.Ql - 221as an option flag. 222This practice is wrong, and should not be used in any current development. 223It is provided for backward compatibility 224.Em only . 225By default, a single dash causes 226.Fn getopt 227to return \-1. 228This is, we believe, compatible with System V. 229.Pp 230It is also possible to handle digits as option letters. 231This allows 232.Fn getopt 233to be used with programs that expect a number 234.Pq Dq Li \&-\&3 235as an option. 236This practice is wrong, and should not be used in any current development. 237It is provided for backward compatibility 238.Em only . 239The following code fragment works in most cases. 240.Bd -literal -offset indent 241int length; 242char *p; 243 244while ((c = getopt(argc, argv, "0123456789")) != -1) { 245 switch (c) { 246 case '0': case '1': case '2': case '3': case '4': 247 case '5': case '6': case '7': case '8': case '9': 248 p = argv[optind - 1]; 249 if (p[0] == '-' && p[1] == ch && !p[2]) 250 length = atoi(++p); 251 else 252 length = atoi(argv[optind] + 1); 253 break; 254 } 255} 256.Ed 257