16aa22516SPedro F. Giffuni.\" $NetBSD: getopt.3,v 1.34 2014/06/05 22:09:50 wiz Exp $ 205cfdd09SAndrey A. Chernov.\" 358f0484fSRodney W. Grimes.\" Copyright (c) 1988, 1991, 1993 458f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes.\" 658f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 758f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 858f0484fSRodney W. Grimes.\" are met: 958f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 1158f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1258f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1358f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 143fb3b97cSEd Maste.\" 3. Neither the name of the University nor the names of its contributors 1558f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1658f0484fSRodney W. Grimes.\" without specific prior written permission. 1758f0484fSRodney W. Grimes.\" 1858f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1958f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2058f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2158f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2258f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2358f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2458f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2558f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2658f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2758f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2858f0484fSRodney W. Grimes.\" SUCH DAMAGE. 2958f0484fSRodney W. Grimes.\" 306aa22516SPedro F. Giffuni.Dd June 5, 2014 3158f0484fSRodney W. Grimes.Dt GETOPT 3 32a307d598SRuslan Ermilov.Os 3358f0484fSRodney W. Grimes.Sh NAME 3458f0484fSRodney W. Grimes.Nm getopt 3558f0484fSRodney W. Grimes.Nd get option character from command line argument list 3625bb73e0SAlexey Zelkin.Sh LIBRARY 3725bb73e0SAlexey Zelkin.Lb libc 3858f0484fSRodney W. Grimes.Sh SYNOPSIS 398aefde06SJeroen Ruigrok van der Werven.In unistd.h 4058f0484fSRodney W. Grimes.Vt extern char *optarg ; 4158f0484fSRodney W. Grimes.Vt extern int optind ; 4258f0484fSRodney W. Grimes.Vt extern int optopt ; 4358f0484fSRodney W. Grimes.Vt extern int opterr ; 4458f0484fSRodney W. Grimes.Vt extern int optreset ; 4558f0484fSRodney W. Grimes.Ft int 4605cfdd09SAndrey A. Chernov.Fn getopt "int argc" "char * const argv[]" "const char *optstring" 4758f0484fSRodney W. Grimes.Sh DESCRIPTION 4858f0484fSRodney W. GrimesThe 4958f0484fSRodney W. Grimes.Fn getopt 5058f0484fSRodney W. Grimesfunction incrementally parses a command line argument list 5158f0484fSRodney W. Grimes.Fa argv 5258f0484fSRodney W. Grimesand returns the next 5358f0484fSRodney W. Grimes.Em known 5458f0484fSRodney W. Grimesoption character. 5558f0484fSRodney W. GrimesAn option character is 5658f0484fSRodney W. Grimes.Em known 5758f0484fSRodney W. Grimesif it has been specified in the string of accepted option characters, 5858f0484fSRodney W. Grimes.Fa optstring . 5958f0484fSRodney W. Grimes.Pp 6058f0484fSRodney W. GrimesThe option string 6158f0484fSRodney W. Grimes.Fa optstring 6258f0484fSRodney W. Grimesmay contain the following elements: individual characters, and 6358f0484fSRodney W. Grimescharacters followed by a colon to indicate an option argument 6458f0484fSRodney W. Grimesis to follow. 656aa22516SPedro F. GiffuniIf an individual character is followed by two colons, then the 666aa22516SPedro F. Giffunioption argument is optional; 676aa22516SPedro F. Giffuni.Va optarg 686aa22516SPedro F. Giffuniis set to the rest of the current 696aa22516SPedro F. Giffuni.Va argv 706aa22516SPedro F. Giffuniword, or 716aa22516SPedro F. Giffuni.Dv NULL 726aa22516SPedro F. Giffuniif there were no more characters in the current word. 736aa22516SPedro F. GiffuniThis is a 74*74ba624bSSergey Kandaurov.Tn GNU 756aa22516SPedro F. Giffuniextension. 7658f0484fSRodney W. GrimesFor example, an option string 773d05ebbcSUlrich Spörlein.Li \&"x" 7858f0484fSRodney W. Grimesrecognizes an option 7958f0484fSRodney W. Grimes.Dq Fl x , 8058f0484fSRodney W. Grimesand an option string 813d05ebbcSUlrich Spörlein.Li \&"x:" 8258f0484fSRodney W. Grimesrecognizes an option and argument 8358f0484fSRodney W. Grimes.Dq Fl x Ar argument . 8458f0484fSRodney W. GrimesIt does not matter to 8558f0484fSRodney W. Grimes.Fn getopt 8658f0484fSRodney W. Grimesif a following argument has leading white space. 8758f0484fSRodney W. Grimes.Pp 8858f0484fSRodney W. GrimesOn return from 8958f0484fSRodney W. Grimes.Fn getopt , 9058f0484fSRodney W. Grimes.Va optarg 9158f0484fSRodney W. Grimespoints to an option argument, if it is anticipated, 9258f0484fSRodney W. Grimesand the variable 9358f0484fSRodney W. Grimes.Va optind 9458f0484fSRodney W. Grimescontains the index to the next 9558f0484fSRodney W. Grimes.Fa argv 9658f0484fSRodney W. Grimesargument for a subsequent call 9758f0484fSRodney W. Grimesto 9858f0484fSRodney W. Grimes.Fn getopt . 9958f0484fSRodney W. GrimesThe variable 10058f0484fSRodney W. Grimes.Va optopt 10158f0484fSRodney W. Grimessaves the last 10258f0484fSRodney W. Grimes.Em known 10358f0484fSRodney W. Grimesoption character returned by 10458f0484fSRodney W. Grimes.Fn getopt . 10558f0484fSRodney W. Grimes.Pp 10605cfdd09SAndrey A. ChernovThe variables 10758f0484fSRodney W. Grimes.Va opterr 10858f0484fSRodney W. Grimesand 10958f0484fSRodney W. Grimes.Va optind 11058f0484fSRodney W. Grimesare both initialized to 1. 11158f0484fSRodney W. GrimesThe 11258f0484fSRodney W. Grimes.Va optind 11358f0484fSRodney W. Grimesvariable may be set to another value before a set of calls to 11458f0484fSRodney W. Grimes.Fn getopt 11558f0484fSRodney W. Grimesin order to skip over more or less argv entries. 11658f0484fSRodney W. Grimes.Pp 11758f0484fSRodney W. GrimesIn order to use 11858f0484fSRodney W. Grimes.Fn getopt 11958f0484fSRodney W. Grimesto evaluate multiple sets of arguments, or to evaluate a single set of 12058f0484fSRodney W. Grimesarguments multiple times, 12158f0484fSRodney W. Grimesthe variable 12258f0484fSRodney W. Grimes.Va optreset 12358f0484fSRodney W. Grimesmust be set to 1 before the second and each additional set of calls to 12458f0484fSRodney W. Grimes.Fn getopt , 12558f0484fSRodney W. Grimesand the variable 12658f0484fSRodney W. Grimes.Va optind 12758f0484fSRodney W. Grimesmust be reinitialized. 12858f0484fSRodney W. Grimes.Pp 12958f0484fSRodney W. GrimesThe 13058f0484fSRodney W. Grimes.Fn getopt 13105cfdd09SAndrey A. Chernovfunction returns \-1 when the argument list is exhausted. 132f25c63afSPhilippe CharnierThe interpretation of options in the argument list may be cancelled 13358f0484fSRodney W. Grimesby the option 13458f0484fSRodney W. Grimes.Ql -- 13558f0484fSRodney W. Grimes(double dash) which causes 13658f0484fSRodney W. Grimes.Fn getopt 137a55fccb4SRobert Nordierto signal the end of argument processing and return \-1. 13858f0484fSRodney W. GrimesWhen all options have been processed (i.e., up to the first non-option 13958f0484fSRodney W. Grimesargument), 14058f0484fSRodney W. Grimes.Fn getopt 1414381233dSPeter Wemmreturns \-1. 14205cfdd09SAndrey A. Chernov.Sh RETURN VALUES 14305cfdd09SAndrey A. ChernovThe 14405cfdd09SAndrey A. Chernov.Fn getopt 14505cfdd09SAndrey A. Chernovfunction returns the next known option character in 14605cfdd09SAndrey A. Chernov.Fa optstring . 14705cfdd09SAndrey A. ChernovIf 14805cfdd09SAndrey A. Chernov.Fn getopt 14905cfdd09SAndrey A. Chernovencounters a character not found in 15005cfdd09SAndrey A. Chernov.Fa optstring 15105cfdd09SAndrey A. Chernovor if it detects a missing option argument, 15205cfdd09SAndrey A. Chernovit returns 15305cfdd09SAndrey A. Chernov.Ql \&? 15405cfdd09SAndrey A. Chernov(question mark). 15505cfdd09SAndrey A. ChernovIf 15605cfdd09SAndrey A. Chernov.Fa optstring 15705cfdd09SAndrey A. Chernovhas a leading 15805cfdd09SAndrey A. Chernov.Ql \&: 15905cfdd09SAndrey A. Chernovthen a missing option argument causes 16005cfdd09SAndrey A. Chernov.Ql \&: 16105cfdd09SAndrey A. Chernovto be returned instead of 16205cfdd09SAndrey A. Chernov.Ql \&? . 16305cfdd09SAndrey A. ChernovIn either case, the variable 16405cfdd09SAndrey A. Chernov.Va optopt 16505cfdd09SAndrey A. Chernovis set to the character that caused the error. 16605cfdd09SAndrey A. ChernovThe 16705cfdd09SAndrey A. Chernov.Fn getopt 16805cfdd09SAndrey A. Chernovfunction returns \-1 when the argument list is exhausted. 16905cfdd09SAndrey A. Chernov.Sh EXAMPLES 17005cfdd09SAndrey A. Chernov.Bd -literal -compact 171d68c1e59SWarner Losh#include <unistd.h> 17205cfdd09SAndrey A. Chernovint bflag, ch, fd; 17305cfdd09SAndrey A. Chernov 17405cfdd09SAndrey A. Chernovbflag = 0; 17505cfdd09SAndrey A. Chernovwhile ((ch = getopt(argc, argv, "bf:")) != -1) { 17605cfdd09SAndrey A. Chernov switch (ch) { 17705cfdd09SAndrey A. Chernov case 'b': 17805cfdd09SAndrey A. Chernov bflag = 1; 17905cfdd09SAndrey A. Chernov break; 18005cfdd09SAndrey A. Chernov case 'f': 18105cfdd09SAndrey A. Chernov if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { 18205cfdd09SAndrey A. Chernov (void)fprintf(stderr, 18305cfdd09SAndrey A. Chernov "myname: %s: %s\en", optarg, strerror(errno)); 18405cfdd09SAndrey A. Chernov exit(1); 18505cfdd09SAndrey A. Chernov } 18605cfdd09SAndrey A. Chernov break; 18705cfdd09SAndrey A. Chernov case '?': 18805cfdd09SAndrey A. Chernov default: 18905cfdd09SAndrey A. Chernov usage(); 19005cfdd09SAndrey A. Chernov } 19105cfdd09SAndrey A. Chernov} 19205cfdd09SAndrey A. Chernovargc -= optind; 19305cfdd09SAndrey A. Chernovargv += optind; 19405cfdd09SAndrey A. Chernov.Ed 19558f0484fSRodney W. Grimes.Sh DIAGNOSTICS 19658f0484fSRodney W. GrimesIf the 19758f0484fSRodney W. Grimes.Fn getopt 19858f0484fSRodney W. Grimesfunction encounters a character not found in the string 19905cfdd09SAndrey A. Chernov.Fa optstring 20058f0484fSRodney W. Grimesor detects 2017b4e5796SMike Pritcharda missing option argument it writes an error message to the 202ae828962SRuslan Ermilov.Dv stderr 2037b4e5796SMike Pritchardand returns 20405cfdd09SAndrey A. Chernov.Ql \&? . 20558f0484fSRodney W. GrimesSetting 20658f0484fSRodney W. Grimes.Va opterr 20758f0484fSRodney W. Grimesto a zero will disable these error messages. 20858f0484fSRodney W. GrimesIf 20905cfdd09SAndrey A. Chernov.Fa optstring 21058f0484fSRodney W. Grimeshas a leading 21158f0484fSRodney W. Grimes.Ql \&: 21258f0484fSRodney W. Grimesthen a missing option argument causes a 21358f0484fSRodney W. Grimes.Ql \&: 21458f0484fSRodney W. Grimesto be returned in addition to suppressing any error messages. 21558f0484fSRodney W. Grimes.Pp 21658f0484fSRodney W. GrimesOption arguments are allowed to begin with 21758f0484fSRodney W. Grimes.Dq Li \- ; 21805cfdd09SAndrey A. Chernovthis is reasonable but reduces the amount of error checking possible. 21905cfdd09SAndrey A. Chernov.Sh SEE ALSO 22005cfdd09SAndrey A. Chernov.Xr getopt 1 , 22105cfdd09SAndrey A. Chernov.Xr getopt_long 3 , 22205cfdd09SAndrey A. Chernov.Xr getsubopt 3 22305cfdd09SAndrey A. Chernov.Sh STANDARDS 22458f0484fSRodney W. GrimesThe 22558f0484fSRodney W. Grimes.Va optreset 22658f0484fSRodney W. Grimesvariable was added to make it possible to call the 22758f0484fSRodney W. Grimes.Fn getopt 22858f0484fSRodney W. Grimesfunction multiple times. 22958f0484fSRodney W. GrimesThis is an extension to the 23058f0484fSRodney W. Grimes.St -p1003.2 23158f0484fSRodney W. Grimesspecification. 23258f0484fSRodney W. Grimes.Sh HISTORY 23358f0484fSRodney W. GrimesThe 23458f0484fSRodney W. Grimes.Fn getopt 235a8b4fa4aSKris Kennawayfunction appeared in 23658f0484fSRodney W. Grimes.Bx 4.3 . 23758f0484fSRodney W. Grimes.Sh BUGS 2384381233dSPeter WemmThe 2394381233dSPeter Wemm.Fn getopt 2404381233dSPeter Wemmfunction was once specified to return 2414381233dSPeter Wemm.Dv EOF 2424381233dSPeter Wemminstead of \-1. 2434381233dSPeter WemmThis was changed by 2444381233dSPeter Wemm.St -p1003.2-92 2454381233dSPeter Wemmto decouple 2464381233dSPeter Wemm.Fn getopt 2474381233dSPeter Wemmfrom 248743d5d51SRuslan Ermilov.In stdio.h . 2494381233dSPeter Wemm.Pp 25058f0484fSRodney W. GrimesA single dash 25158f0484fSRodney W. Grimes.Dq Li - 2524c204da0SMasafumi Max NAKANEmay be specified as a character in 25358f0484fSRodney W. Grimes.Fa optstring , 25458f0484fSRodney W. Grimeshowever it should 25558f0484fSRodney W. Grimes.Em never 25658f0484fSRodney W. Grimeshave an argument associated with it. 25758f0484fSRodney W. GrimesThis allows 25858f0484fSRodney W. Grimes.Fn getopt 25958f0484fSRodney W. Grimesto be used with programs that expect 26058f0484fSRodney W. Grimes.Dq Li - 26158f0484fSRodney W. Grimesas an option flag. 26258f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development. 26358f0484fSRodney W. GrimesIt is provided for backward compatibility 26458f0484fSRodney W. Grimes.Em only . 26505cfdd09SAndrey A. ChernovCare should be taken not to use 26605cfdd09SAndrey A. Chernov.Ql \&- 26705cfdd09SAndrey A. Chernovas the first character in 26805cfdd09SAndrey A. Chernov.Fa optstring 26905cfdd09SAndrey A. Chernovto avoid a semantic conflict with 27005cfdd09SAndrey A. Chernov.Tn GNU 27105cfdd09SAndrey A. Chernov.Fn getopt , 27205cfdd09SAndrey A. Chernovwhich assigns different meaning to an 27305cfdd09SAndrey A. Chernov.Fa optstring 27405cfdd09SAndrey A. Chernovthat begins with a 27505cfdd09SAndrey A. Chernov.Ql \&- . 27658f0484fSRodney W. GrimesBy default, a single dash causes 27758f0484fSRodney W. Grimes.Fn getopt 2784381233dSPeter Wemmto return \-1. 27958f0484fSRodney W. Grimes.Pp 28058f0484fSRodney W. GrimesIt is also possible to handle digits as option letters. 28158f0484fSRodney W. GrimesThis allows 28258f0484fSRodney W. Grimes.Fn getopt 28358f0484fSRodney W. Grimesto be used with programs that expect a number 28458f0484fSRodney W. Grimes.Pq Dq Li \&-\&3 28558f0484fSRodney W. Grimesas an option. 28658f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development. 28758f0484fSRodney W. GrimesIt is provided for backward compatibility 28858f0484fSRodney W. Grimes.Em only . 28905cfdd09SAndrey A. ChernovThe following code fragment works in most cases. 29058f0484fSRodney W. Grimes.Bd -literal -offset indent 29105cfdd09SAndrey A. Chernovint ch; 29205cfdd09SAndrey A. Chernovlong length; 293838fb327STim Vanderhoekchar *p, *ep; 29458f0484fSRodney W. Grimes 295a55fccb4SRobert Nordierwhile ((ch = getopt(argc, argv, "0123456789")) != -1) 296a55fccb4SRobert Nordier switch (ch) { 29758f0484fSRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 29858f0484fSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 29958f0484fSRodney W. Grimes p = argv[optind - 1]; 30005cfdd09SAndrey A. Chernov if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) { 30105cfdd09SAndrey A. Chernov length = ch - '0'; 30205cfdd09SAndrey A. Chernov ep = ""; 30305cfdd09SAndrey A. Chernov } else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) { 304838fb327STim Vanderhoek length = strtol((p = argv[optind] + 1), 30505cfdd09SAndrey A. Chernov \*[Am]ep, 10); 306838fb327STim Vanderhoek optind++; 307838fb327STim Vanderhoek optreset = 1; 308838fb327STim Vanderhoek } else 309838fb327STim Vanderhoek usage(); 31005cfdd09SAndrey A. Chernov if (*ep != '\e0') 311838fb327STim Vanderhoek errx(EX_USAGE, "illegal number -- %s", p); 31258f0484fSRodney W. Grimes break; 31358f0484fSRodney W. Grimes } 31458f0484fSRodney W. Grimes.Ed 315