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.\" 304381233dSPeter Wemm.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 317f3dea24SPeter Wemm.\" $FreeBSD$ 3258f0484fSRodney W. Grimes.\" 336aa22516SPedro F. Giffuni.Dd June 5, 2014 3458f0484fSRodney W. Grimes.Dt GETOPT 3 35a307d598SRuslan Ermilov.Os 3658f0484fSRodney W. Grimes.Sh NAME 3758f0484fSRodney W. Grimes.Nm getopt 3858f0484fSRodney W. Grimes.Nd get option character from command line argument list 3925bb73e0SAlexey Zelkin.Sh LIBRARY 4025bb73e0SAlexey Zelkin.Lb libc 4158f0484fSRodney W. Grimes.Sh SYNOPSIS 428aefde06SJeroen Ruigrok van der Werven.In unistd.h 4358f0484fSRodney W. Grimes.Vt extern char *optarg ; 4458f0484fSRodney W. Grimes.Vt extern int optind ; 4558f0484fSRodney W. Grimes.Vt extern int optopt ; 4658f0484fSRodney W. Grimes.Vt extern int opterr ; 4758f0484fSRodney W. Grimes.Vt extern int optreset ; 4858f0484fSRodney W. Grimes.Ft int 4905cfdd09SAndrey A. Chernov.Fn getopt "int argc" "char * const argv[]" "const char *optstring" 5058f0484fSRodney W. Grimes.Sh DESCRIPTION 5158f0484fSRodney W. GrimesThe 5258f0484fSRodney W. Grimes.Fn getopt 5358f0484fSRodney W. Grimesfunction incrementally parses a command line argument list 5458f0484fSRodney W. Grimes.Fa argv 5558f0484fSRodney W. Grimesand returns the next 5658f0484fSRodney W. Grimes.Em known 5758f0484fSRodney W. Grimesoption character. 5858f0484fSRodney W. GrimesAn option character is 5958f0484fSRodney W. Grimes.Em known 6058f0484fSRodney W. Grimesif it has been specified in the string of accepted option characters, 6158f0484fSRodney W. Grimes.Fa optstring . 6258f0484fSRodney W. Grimes.Pp 6358f0484fSRodney W. GrimesThe option string 6458f0484fSRodney W. Grimes.Fa optstring 6558f0484fSRodney W. Grimesmay contain the following elements: individual characters, and 6658f0484fSRodney W. Grimescharacters followed by a colon to indicate an option argument 6758f0484fSRodney W. Grimesis to follow. 686aa22516SPedro F. GiffuniIf an individual character is followed by two colons, then the 696aa22516SPedro F. Giffunioption argument is optional; 706aa22516SPedro F. Giffuni.Va optarg 716aa22516SPedro F. Giffuniis set to the rest of the current 726aa22516SPedro F. Giffuni.Va argv 736aa22516SPedro F. Giffuniword, or 746aa22516SPedro F. Giffuni.Dv NULL 756aa22516SPedro F. Giffuniif there were no more characters in the current word. 766aa22516SPedro F. GiffuniThis is a 77*74ba624bSSergey Kandaurov.Tn GNU 786aa22516SPedro F. Giffuniextension. 7958f0484fSRodney W. GrimesFor example, an option string 803d05ebbcSUlrich Spörlein.Li \&"x" 8158f0484fSRodney W. Grimesrecognizes an option 8258f0484fSRodney W. Grimes.Dq Fl x , 8358f0484fSRodney W. Grimesand an option string 843d05ebbcSUlrich Spörlein.Li \&"x:" 8558f0484fSRodney W. Grimesrecognizes an option and argument 8658f0484fSRodney W. Grimes.Dq Fl x Ar argument . 8758f0484fSRodney W. GrimesIt does not matter to 8858f0484fSRodney W. Grimes.Fn getopt 8958f0484fSRodney W. Grimesif a following argument has leading white space. 9058f0484fSRodney W. Grimes.Pp 9158f0484fSRodney W. GrimesOn return from 9258f0484fSRodney W. Grimes.Fn getopt , 9358f0484fSRodney W. Grimes.Va optarg 9458f0484fSRodney W. Grimespoints to an option argument, if it is anticipated, 9558f0484fSRodney W. Grimesand the variable 9658f0484fSRodney W. Grimes.Va optind 9758f0484fSRodney W. Grimescontains the index to the next 9858f0484fSRodney W. Grimes.Fa argv 9958f0484fSRodney W. Grimesargument for a subsequent call 10058f0484fSRodney W. Grimesto 10158f0484fSRodney W. Grimes.Fn getopt . 10258f0484fSRodney W. GrimesThe variable 10358f0484fSRodney W. Grimes.Va optopt 10458f0484fSRodney W. Grimessaves the last 10558f0484fSRodney W. Grimes.Em known 10658f0484fSRodney W. Grimesoption character returned by 10758f0484fSRodney W. Grimes.Fn getopt . 10858f0484fSRodney W. Grimes.Pp 10905cfdd09SAndrey A. ChernovThe variables 11058f0484fSRodney W. Grimes.Va opterr 11158f0484fSRodney W. Grimesand 11258f0484fSRodney W. Grimes.Va optind 11358f0484fSRodney W. Grimesare both initialized to 1. 11458f0484fSRodney W. GrimesThe 11558f0484fSRodney W. Grimes.Va optind 11658f0484fSRodney W. Grimesvariable may be set to another value before a set of calls to 11758f0484fSRodney W. Grimes.Fn getopt 11858f0484fSRodney W. Grimesin order to skip over more or less argv entries. 11958f0484fSRodney W. Grimes.Pp 12058f0484fSRodney W. GrimesIn order to use 12158f0484fSRodney W. Grimes.Fn getopt 12258f0484fSRodney W. Grimesto evaluate multiple sets of arguments, or to evaluate a single set of 12358f0484fSRodney W. Grimesarguments multiple times, 12458f0484fSRodney W. Grimesthe variable 12558f0484fSRodney W. Grimes.Va optreset 12658f0484fSRodney W. Grimesmust be set to 1 before the second and each additional set of calls to 12758f0484fSRodney W. Grimes.Fn getopt , 12858f0484fSRodney W. Grimesand the variable 12958f0484fSRodney W. Grimes.Va optind 13058f0484fSRodney W. Grimesmust be reinitialized. 13158f0484fSRodney W. Grimes.Pp 13258f0484fSRodney W. GrimesThe 13358f0484fSRodney W. Grimes.Fn getopt 13405cfdd09SAndrey A. Chernovfunction returns \-1 when the argument list is exhausted. 135f25c63afSPhilippe CharnierThe interpretation of options in the argument list may be cancelled 13658f0484fSRodney W. Grimesby the option 13758f0484fSRodney W. Grimes.Ql -- 13858f0484fSRodney W. Grimes(double dash) which causes 13958f0484fSRodney W. Grimes.Fn getopt 140a55fccb4SRobert Nordierto signal the end of argument processing and return \-1. 14158f0484fSRodney W. GrimesWhen all options have been processed (i.e., up to the first non-option 14258f0484fSRodney W. Grimesargument), 14358f0484fSRodney W. Grimes.Fn getopt 1444381233dSPeter Wemmreturns \-1. 14505cfdd09SAndrey A. Chernov.Sh RETURN VALUES 14605cfdd09SAndrey A. ChernovThe 14705cfdd09SAndrey A. Chernov.Fn getopt 14805cfdd09SAndrey A. Chernovfunction returns the next known option character in 14905cfdd09SAndrey A. Chernov.Fa optstring . 15005cfdd09SAndrey A. ChernovIf 15105cfdd09SAndrey A. Chernov.Fn getopt 15205cfdd09SAndrey A. Chernovencounters a character not found in 15305cfdd09SAndrey A. Chernov.Fa optstring 15405cfdd09SAndrey A. Chernovor if it detects a missing option argument, 15505cfdd09SAndrey A. Chernovit returns 15605cfdd09SAndrey A. Chernov.Ql \&? 15705cfdd09SAndrey A. Chernov(question mark). 15805cfdd09SAndrey A. ChernovIf 15905cfdd09SAndrey A. Chernov.Fa optstring 16005cfdd09SAndrey A. Chernovhas a leading 16105cfdd09SAndrey A. Chernov.Ql \&: 16205cfdd09SAndrey A. Chernovthen a missing option argument causes 16305cfdd09SAndrey A. Chernov.Ql \&: 16405cfdd09SAndrey A. Chernovto be returned instead of 16505cfdd09SAndrey A. Chernov.Ql \&? . 16605cfdd09SAndrey A. ChernovIn either case, the variable 16705cfdd09SAndrey A. Chernov.Va optopt 16805cfdd09SAndrey A. Chernovis set to the character that caused the error. 16905cfdd09SAndrey A. ChernovThe 17005cfdd09SAndrey A. Chernov.Fn getopt 17105cfdd09SAndrey A. Chernovfunction returns \-1 when the argument list is exhausted. 17205cfdd09SAndrey A. Chernov.Sh EXAMPLES 17305cfdd09SAndrey A. Chernov.Bd -literal -compact 174d68c1e59SWarner Losh#include <unistd.h> 17505cfdd09SAndrey A. Chernovint bflag, ch, fd; 17605cfdd09SAndrey A. Chernov 17705cfdd09SAndrey A. Chernovbflag = 0; 17805cfdd09SAndrey A. Chernovwhile ((ch = getopt(argc, argv, "bf:")) != -1) { 17905cfdd09SAndrey A. Chernov switch (ch) { 18005cfdd09SAndrey A. Chernov case 'b': 18105cfdd09SAndrey A. Chernov bflag = 1; 18205cfdd09SAndrey A. Chernov break; 18305cfdd09SAndrey A. Chernov case 'f': 18405cfdd09SAndrey A. Chernov if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { 18505cfdd09SAndrey A. Chernov (void)fprintf(stderr, 18605cfdd09SAndrey A. Chernov "myname: %s: %s\en", optarg, strerror(errno)); 18705cfdd09SAndrey A. Chernov exit(1); 18805cfdd09SAndrey A. Chernov } 18905cfdd09SAndrey A. Chernov break; 19005cfdd09SAndrey A. Chernov case '?': 19105cfdd09SAndrey A. Chernov default: 19205cfdd09SAndrey A. Chernov usage(); 19305cfdd09SAndrey A. Chernov } 19405cfdd09SAndrey A. Chernov} 19505cfdd09SAndrey A. Chernovargc -= optind; 19605cfdd09SAndrey A. Chernovargv += optind; 19705cfdd09SAndrey A. Chernov.Ed 19858f0484fSRodney W. Grimes.Sh DIAGNOSTICS 19958f0484fSRodney W. GrimesIf the 20058f0484fSRodney W. Grimes.Fn getopt 20158f0484fSRodney W. Grimesfunction encounters a character not found in the string 20205cfdd09SAndrey A. Chernov.Fa optstring 20358f0484fSRodney W. Grimesor detects 2047b4e5796SMike Pritcharda missing option argument it writes an error message to the 205ae828962SRuslan Ermilov.Dv stderr 2067b4e5796SMike Pritchardand returns 20705cfdd09SAndrey A. Chernov.Ql \&? . 20858f0484fSRodney W. GrimesSetting 20958f0484fSRodney W. Grimes.Va opterr 21058f0484fSRodney W. Grimesto a zero will disable these error messages. 21158f0484fSRodney W. GrimesIf 21205cfdd09SAndrey A. Chernov.Fa optstring 21358f0484fSRodney W. Grimeshas a leading 21458f0484fSRodney W. Grimes.Ql \&: 21558f0484fSRodney W. Grimesthen a missing option argument causes a 21658f0484fSRodney W. Grimes.Ql \&: 21758f0484fSRodney W. Grimesto be returned in addition to suppressing any error messages. 21858f0484fSRodney W. Grimes.Pp 21958f0484fSRodney W. GrimesOption arguments are allowed to begin with 22058f0484fSRodney W. Grimes.Dq Li \- ; 22105cfdd09SAndrey A. Chernovthis is reasonable but reduces the amount of error checking possible. 22205cfdd09SAndrey A. Chernov.Sh SEE ALSO 22305cfdd09SAndrey A. Chernov.Xr getopt 1 , 22405cfdd09SAndrey A. Chernov.Xr getopt_long 3 , 22505cfdd09SAndrey A. Chernov.Xr getsubopt 3 22605cfdd09SAndrey A. Chernov.Sh STANDARDS 22758f0484fSRodney W. GrimesThe 22858f0484fSRodney W. Grimes.Va optreset 22958f0484fSRodney W. Grimesvariable was added to make it possible to call the 23058f0484fSRodney W. Grimes.Fn getopt 23158f0484fSRodney W. Grimesfunction multiple times. 23258f0484fSRodney W. GrimesThis is an extension to the 23358f0484fSRodney W. Grimes.St -p1003.2 23458f0484fSRodney W. Grimesspecification. 23558f0484fSRodney W. Grimes.Sh HISTORY 23658f0484fSRodney W. GrimesThe 23758f0484fSRodney W. Grimes.Fn getopt 238a8b4fa4aSKris Kennawayfunction appeared in 23958f0484fSRodney W. Grimes.Bx 4.3 . 24058f0484fSRodney W. Grimes.Sh BUGS 2414381233dSPeter WemmThe 2424381233dSPeter Wemm.Fn getopt 2434381233dSPeter Wemmfunction was once specified to return 2444381233dSPeter Wemm.Dv EOF 2454381233dSPeter Wemminstead of \-1. 2464381233dSPeter WemmThis was changed by 2474381233dSPeter Wemm.St -p1003.2-92 2484381233dSPeter Wemmto decouple 2494381233dSPeter Wemm.Fn getopt 2504381233dSPeter Wemmfrom 251743d5d51SRuslan Ermilov.In stdio.h . 2524381233dSPeter Wemm.Pp 25358f0484fSRodney W. GrimesA single dash 25458f0484fSRodney W. Grimes.Dq Li - 2554c204da0SMasafumi Max NAKANEmay be specified as a character in 25658f0484fSRodney W. Grimes.Fa optstring , 25758f0484fSRodney W. Grimeshowever it should 25858f0484fSRodney W. Grimes.Em never 25958f0484fSRodney W. Grimeshave an argument associated with it. 26058f0484fSRodney W. GrimesThis allows 26158f0484fSRodney W. Grimes.Fn getopt 26258f0484fSRodney W. Grimesto be used with programs that expect 26358f0484fSRodney W. Grimes.Dq Li - 26458f0484fSRodney W. Grimesas an option flag. 26558f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development. 26658f0484fSRodney W. GrimesIt is provided for backward compatibility 26758f0484fSRodney W. Grimes.Em only . 26805cfdd09SAndrey A. ChernovCare should be taken not to use 26905cfdd09SAndrey A. Chernov.Ql \&- 27005cfdd09SAndrey A. Chernovas the first character in 27105cfdd09SAndrey A. Chernov.Fa optstring 27205cfdd09SAndrey A. Chernovto avoid a semantic conflict with 27305cfdd09SAndrey A. Chernov.Tn GNU 27405cfdd09SAndrey A. Chernov.Fn getopt , 27505cfdd09SAndrey A. Chernovwhich assigns different meaning to an 27605cfdd09SAndrey A. Chernov.Fa optstring 27705cfdd09SAndrey A. Chernovthat begins with a 27805cfdd09SAndrey A. Chernov.Ql \&- . 27958f0484fSRodney W. GrimesBy default, a single dash causes 28058f0484fSRodney W. Grimes.Fn getopt 2814381233dSPeter Wemmto return \-1. 28258f0484fSRodney W. Grimes.Pp 28358f0484fSRodney W. GrimesIt is also possible to handle digits as option letters. 28458f0484fSRodney W. GrimesThis allows 28558f0484fSRodney W. Grimes.Fn getopt 28658f0484fSRodney W. Grimesto be used with programs that expect a number 28758f0484fSRodney W. Grimes.Pq Dq Li \&-\&3 28858f0484fSRodney W. Grimesas an option. 28958f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development. 29058f0484fSRodney W. GrimesIt is provided for backward compatibility 29158f0484fSRodney W. Grimes.Em only . 29205cfdd09SAndrey A. ChernovThe following code fragment works in most cases. 29358f0484fSRodney W. Grimes.Bd -literal -offset indent 29405cfdd09SAndrey A. Chernovint ch; 29505cfdd09SAndrey A. Chernovlong length; 296838fb327STim Vanderhoekchar *p, *ep; 29758f0484fSRodney W. Grimes 298a55fccb4SRobert Nordierwhile ((ch = getopt(argc, argv, "0123456789")) != -1) 299a55fccb4SRobert Nordier switch (ch) { 30058f0484fSRodney W. Grimes case '0': case '1': case '2': case '3': case '4': 30158f0484fSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 30258f0484fSRodney W. Grimes p = argv[optind - 1]; 30305cfdd09SAndrey A. Chernov if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) { 30405cfdd09SAndrey A. Chernov length = ch - '0'; 30505cfdd09SAndrey A. Chernov ep = ""; 30605cfdd09SAndrey A. Chernov } else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) { 307838fb327STim Vanderhoek length = strtol((p = argv[optind] + 1), 30805cfdd09SAndrey A. Chernov \*[Am]ep, 10); 309838fb327STim Vanderhoek optind++; 310838fb327STim Vanderhoek optreset = 1; 311838fb327STim Vanderhoek } else 312838fb327STim Vanderhoek usage(); 31305cfdd09SAndrey A. Chernov if (*ep != '\e0') 314838fb327STim Vanderhoek errx(EX_USAGE, "illegal number -- %s", p); 31558f0484fSRodney W. Grimes break; 31658f0484fSRodney W. Grimes } 31758f0484fSRodney W. Grimes.Ed 318