1.\" Copyright (c) 1988, 1991 Regents of the University of California. 2.\" 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.\" from: @(#)getopt.3 6.16 (Berkeley) 4/19/91 33.\" $Id: getopt.3,v 1.3 1993/09/14 22:37:24 jtc Exp $ 34.\" 35.Dd April 19, 1991 36.Dt GETOPT 3 37.Os BSD 4.3 38.Sh NAME 39.Nm getopt 40.Nd get option letter from argv 41.Sh SYNOPSIS 42.Fd #include <stdlib.h> 43.Vt extern char *optarg 44.Vt extern int optind 45.Vt extern int opterr 46.Ft int 47.Fn getopt "int argc" "char * const *argv" "const char *optstring" 48.Sh DESCRIPTION 49The 50.Fn getopt 51function gets 52the next 53.Em known 54option character from 55.Fa argv . 56An option character is 57.Em known 58if it has been specified in the string of accepted option characters, 59.Fa optstring . 60.Pp 61The option string 62.Fa optstring 63may contain the following characters; letters and 64letters followed by a colon to indicate an option argument 65is to follow. It does not matter to 66.Fn getopt 67if a following argument has leading white space. 68.Pp 69On return from 70.Fn getopt , 71.Va optarg 72points to an option argument, if it is anticipated, 73and the variable 74.Va optind 75contains the index to the next 76.Fa argv 77argument for a subsequent call 78to 79.Fn getopt . 80.Pp 81The variable 82.Va opterr 83and 84.Va optind 85are both initialized to 1. 86In order to use 87.Fn getopt 88to evaluate multiple sets of arguments, or to evaluate a single set of 89arguments multiple times, 90.Va optind 91must be initialized to the number of argv entries to be skipped in each 92evaluation. 93.Pp 94The 95.Fn getopt 96function 97returns \-1 98when the argument list is exhausted, or a non-recognized 99option is encountered. 100The interpretation of options in the argument list may be cancelled 101by the option 102.Ql -- 103(double dash) which causes 104.Fn getopt 105to signal the end of argument processing and return \-1. 106When all options have been processed (i.e., up to the first non-option 107argument), 108.Fn getopt 109returns \-1. 110.Sh DIAGNOSTICS 111If the 112.Fn getopt 113function encounters a character not found in the string 114.Va optarg 115or detects 116a missing option argument 117it writes error message 118.Ql ? 119to the 120.Em stderr . 121Setting 122.Va opterr 123to a zero will disable these error messages. 124.Sh EXAMPLE 125.Bd -literal -compact 126extern char *optarg; 127extern int optind; 128int bflag, ch, fd; 129 130bflag = 0; 131while ((ch = getopt(argc, argv, "bf:")) != -1) 132 switch(ch) { 133 case 'b': 134 bflag = 1; 135 break; 136 case 'f': 137 if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 138 (void)fprintf(stderr, 139 "myname: unable to read file %s.\en", optarg); 140 exit(1) ; 141 } 142 break; 143 case '?': 144 default: 145 usage(); 146} 147argc -= optind; 148argv += optind; 149.Ed 150.Sh STANDARDS 151The 152.Fn getopt 153function conforms to 154.St -p1003.2-92 . 155.Sh HISTORY 156The 157.Fn getopt 158function appeared 159.Bx 4.3 . 160.Sh BUGS 161The 162.Fn getopt 163function was once specified to return 164.Dv EOF 165instead of \-1. 166This was changed by 167.St -p1003.2-92 168to decouple 169.Fn getopt 170from 171.Pa <stdio.h> . 172.Pp 173Option arguments are allowed to begin with 174.Dq Li \- ; 175this is reasonable but 176reduces the amount of error checking possible. 177.Pp 178A single dash 179.Dq Li - 180may be specified as an character in 181.Fa optstring , 182however it should 183.Em never 184have an argument associated with it. 185This allows 186.Fn getopt 187to be used with programs that expect 188.Dq Li - 189as an option flag. 190This practice is wrong, and should not be used in any current development. 191It is provided for backward compatibility 192.Em only . 193By default, a single dash causes 194.Fn getopt 195to returns \-1. 196This is, we believe, compatible with System V. 197.Pp 198It is also possible to handle digits as option letters. 199This allows 200.Fn getopt 201to be used with programs that expect a number 202.Pq Dq Li \&-\&3 203as an option. 204This practice is wrong, and should not be used in any current development. 205It is provided for backward compatibility 206.Em only . 207The following code fragment works fairly well. 208.Bd -literal -offset indent 209int length; 210char *p; 211 212while ((c = getopt(argc, argv, "0123456789")) != -1) 213 switch (c) { 214 case '0': case '1': case '2': case '3': case '4': 215 case '5': case '6': case '7': case '8': case '9': 216 p = argv[optind - 1]; 217 if (p[0] == '-' && p[1] == ch && !p[2]) 218 length = atoi(++p); 219 else 220 length = atoi(argv[optind] + 1); 221 break; 222 } 223} 224.Ed 225