161180Sbostic.\" Copyright (c) 1988, 1991, 1993 261180Sbostic.\" The Regents of the University of California. All rights reserved. 320617Smckusick.\" 443571Strent.\" %sccs.include.redist.man% 520617Smckusick.\" 6*66946Sbostic.\" @(#)getopt.3 8.4 (Berkeley) 04/19/94 733992Sbostic.\" 848349Scael.Dd 948349Scael.Dt GETOPT 3 1048349Scael.Os BSD 4.3 1148349Scael.Sh NAME 1248349Scael.Nm getopt 1358389Sbostic.Nd get option character from command line argument list 1448349Scael.Sh SYNOPSIS 1548349Scael.Fd #include <stdlib.h> 1666455Sbostic.Vt extern char *optarg; 1766455Sbostic.Vt extern int optind; 1866455Sbostic.Vt extern int optopt; 1966455Sbostic.Vt extern int opterr; 2066455Sbostic.Vt extern int optreset; 2148349Scael.Ft int 2248349Scael.Fn getopt "int argc" "char * const *argv" "const char *optstring" 2348349Scael.Sh DESCRIPTION 2448349ScaelThe 2548349Scael.Fn getopt 2658389Sbosticfunction incrementally parses a command line argument list 2758389Sbostic.Fa argv 2858389Sbosticand returns the next 2948349Scael.Em known 3058389Sbosticoption character. 3148349ScaelAn option character is 3248349Scael.Em known 3348349Scaelif it has been specified in the string of accepted option characters, 3448349Scael.Fa optstring . 3548349Scael.Pp 3648349ScaelThe option string 3748349Scael.Fa optstring 3858389Sbosticmay contain the following elements: individual characters, and 3958389Sbosticcharacters followed by a colon to indicate an option argument 4054707Smarcis to follow. 4158389SbosticFor example, an option string 4258389Sbostic.Li "\&""x"" 4358389Sbosticrecognizes an option 4458389Sbostic.Dq Fl x , 4558389Sbosticand an option string 4658389Sbostic.Li "\&""x:"" 4758389Sbosticrecognizes an option and argument 4858389Sbostic.Dq Fl x Ar argument . 4958389SbosticIt does not matter to 5048349Scael.Fn getopt 5148349Scaelif a following argument has leading white space. 5248349Scael.Pp 5339478SbosticOn return from 5448349Scael.Fn getopt , 5548349Scael.Va optarg 5648349Scaelpoints to an option argument, if it is anticipated, 5748349Scaeland the variable 5848349Scael.Va optind 5948349Scaelcontains the index to the next 6048349Scael.Fa argv 6148349Scaelargument for a subsequent call 6248349Scaelto 6348349Scael.Fn getopt . 6458389SbosticThe variable 6558389Sbostic.Va optopt 6658389Sbosticsaves the last 6758389Sbostic.Em known 6858389Sbosticoption character returned by 6958389Sbostic.Fn getopt . 7048349Scael.Pp 7148349ScaelThe variable 7248349Scael.Va opterr 7339478Sbosticand 7448349Scael.Va optind 7539478Sbosticare both initialized to 1. 7654707SmarcThe 7754707Smarc.Va optind 7854707Smarcvariable may be set to another value before a set of calls to 7954707Smarc.Fn getopt 8054707Smarcin order to skip over more or less argv entries. 8154707Smarc.Pp 8242262SbosticIn order to use 8348349Scael.Fn getopt 8442262Sbosticto evaluate multiple sets of arguments, or to evaluate a single set of 8542262Sbosticarguments multiple times, 8654707Smarcthe variable 8754707Smarc.Va optreset 8854707Smarcmust be set to 1 before the second and each additional set of calls to 8954707Smarc.Fn getopt , 9054707Smarcand the variable 9148349Scael.Va optind 9254707Smarcmust be reinitialized. 9348349Scael.Pp 9448349ScaelThe 9548349Scael.Fn getopt 9648349Scaelfunction 9748349Scaelreturns an 9848349Scael.Dv EOF 9948349Scaelwhen the argument list is exhausted, or a non-recognized 10048349Scaeloption is encountered. 10148349ScaelThe interpretation of options in the argument list may be cancelled 10248349Scaelby the option 10348349Scael.Ql -- 10448349Scael(double dash) which causes 10548349Scael.Fn getopt 10648349Scaelto signal the end of argument processing and return an 10748349Scael.Dv EOF . 10839478SbosticWhen all options have been processed (i.e., up to the first non-option 10939478Sbosticargument), 11048349Scael.Fn getopt 11148349Scaelreturns 11248349Scael.Dv EOF . 11348349Scael.Sh DIAGNOSTICS 11448349ScaelIf the 11548349Scael.Fn getopt 11648349Scaelfunction encounters a character not found in the string 11748349Scael.Va optarg 11848349Scaelor detects 11954707Smarca missing option argument it writes an error message and returns 12048349Scael.Ql ? 12148349Scaelto the 12248349Scael.Em stderr . 12339478SbosticSetting 12448349Scael.Va opterr 12539478Sbosticto a zero will disable these error messages. 12654707SmarcIf 12754707Smarc.Va optstring 12854707Smarchas a leading 12954707Smarc.Ql \&: 130*66946Sbosticthen a missing option argument causes a 13154707Smarc.Ql \&: 132*66946Sbosticto be returned in addition to suppressing any error messages. 13354707Smarc.Pp 13454707SmarcOption arguments are allowed to begin with 13554707Smarc.Dq Li \- ; 13654707Smarcthis is reasonable but 13754707Smarcreduces the amount of error checking possible. 13854707Smarc.Sh EXTENSIONS 13954707SmarcThe 14054707Smarc.Va optreset 14154707Smarcvariable was added to make it possible to call the 14254707Smarc.Fn getopt 14354707Smarcfunction multiple times. 14454707SmarcThis is an extension to the 14554707Smarc.St -p1003.2 14654707Smarcspecification. 14748349Scael.Sh EXAMPLE 14848349Scael.Bd -literal -compact 14939478Sbosticextern char *optarg; 15039478Sbosticextern int optind; 15139478Sbosticint bflag, ch, fd; 15239478Sbostic 15339478Sbosticbflag = 0; 15439478Sbosticwhile ((ch = getopt(argc, argv, "bf:")) != EOF) 15539478Sbostic switch(ch) { 15639478Sbostic case 'b': 15739478Sbostic bflag = 1; 15839478Sbostic break; 15939478Sbostic case 'f': 16039478Sbostic if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 16139478Sbostic (void)fprintf(stderr, 16254707Smarc "myname: %s: %s\en", optarg, strerror(errno)); 16354707Smarc exit(1); 16420617Smckusick } 16539478Sbostic break; 16639478Sbostic case '?': 16739478Sbostic default: 16839478Sbostic usage(); 16948349Scael} 17039478Sbosticargc -= optind; 17139478Sbosticargv += optind; 17248349Scael.Ed 17348349Scael.Sh HISTORY 17448349ScaelThe 17548349Scael.Fn getopt 17648349Scaelfunction appeared 17748349Scael.Bx 4.3 . 17848349Scael.Sh BUGS 17948349ScaelA single dash 18048349Scael.Dq Li - 18148349Scaelmay be specified as an character in 18248349Scael.Fa optstring , 18339478Sbostichowever it should 18448349Scael.Em never 18539478Sbostichave an argument associated with it. 18639478SbosticThis allows 18748349Scael.Fn getopt 18848349Scaelto be used with programs that expect 18948349Scael.Dq Li - 19048349Scaelas an option flag. 19139478SbosticThis practice is wrong, and should not be used in any current development. 19239478SbosticIt is provided for backward compatibility 19348349Scael.Em only . 19439712SbosticBy default, a single dash causes 19548349Scael.Fn getopt 19648349Scaelto return 19748349Scael.Dv EOF . 19839712SbosticThis is, we believe, compatible with System V. 19948349Scael.Pp 20039478SbosticIt is also possible to handle digits as option letters. 20139478SbosticThis allows 20248349Scael.Fn getopt 20348349Scaelto be used with programs that expect a number 20448349Scael.Pq Dq Li \&-\&3 20548349Scaelas an option. 20639478SbosticThis practice is wrong, and should not be used in any current development. 20739478SbosticIt is provided for backward compatibility 20848349Scael.Em only . 20954707SmarcThe following code fragment works in most cases. 21048349Scael.Bd -literal -offset indent 21139478Sbosticint length; 21239478Sbosticchar *p; 21335982Sbostic 21439478Sbosticwhile ((c = getopt(argc, argv, "0123456789")) != EOF) 21539478Sbostic switch (c) { 21639478Sbostic case '0': case '1': case '2': case '3': case '4': 21739478Sbostic case '5': case '6': case '7': case '8': case '9': 21839478Sbostic p = argv[optind - 1]; 21939478Sbostic if (p[0] == '-' && p[1] == ch && !p[2]) 22039478Sbostic length = atoi(++p); 22139478Sbostic else 22239478Sbostic length = atoi(argv[optind] + 1); 22339478Sbostic break; 22435982Sbostic } 22539478Sbostic} 22648349Scael.Ed 227