Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307.\" $NetBSD: getopt.3,v 1.31 2003/09/23 10:26:54 wiz Exp $.\".\" Copyright (c) 1988, 1991, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)getopt.3 8.5 (Berkeley) 4/27/95.\" $FreeBSD: src/lib/libc/stdlib/getopt.3,v 1.26 2007/01/09 00:28:10 imp Exp $.\".Dd April 27, 1995.Dt GETOPT 3.Os.Sh NAME.Nm getopt.Nd get option character from command line argument list.Sh LIBRARY.Lb libc.Sh SYNOPSIS.In unistd.h.Vt extern char *optarg ;.Vt extern int optind ;.Vt extern int optopt ;.Vt extern int opterr ;.Vt extern int optreset ;.Ft int.Fn getopt "int argc" "char * const argv[]" "const char *optstring".Sh DESCRIPTIONThe.Fn getoptfunction incrementally parses a command line argument list.Fa argvand returns the next.Em knownoption character.An option character is.Em knownif it has been specified in the string of accepted option characters,.Fa optstring ..PpThe option string.Fa optstringmay contain the following elements: individual characters, andcharacters followed by a colon to indicate an option argumentis to follow.For example, an option string.Li "\&""x""recognizes an option.Dq Fl x ,and an option string.Li "\&""x:""recognizes an option and argument.Dq Fl x Ar argument .It does not matter to.Fn getoptif a following argument has leading white space..PpOn return from.Fn getopt ,.Va optargpoints to an option argument, if it is anticipated,and the variable.Va optindcontains the index to the next.Fa argvargument for a subsequent callto.Fn getopt .The variable.Va optoptsaves the last.Em knownoption character returned by.Fn getopt ..PpThe variables.Va opterrand.Va optindare both initialized to 1.The.Va optindvariable may be set to another value before a set of calls to.Fn getoptin order to skip over more or less argv entries..PpIn order to use.Fn getoptto evaluate multiple sets of arguments, or to evaluate a single set ofarguments multiple times,the variable.Va optresetmust be set to 1 before the second and each additional set of calls to.Fn getopt ,and the variable.Va optindmust be reinitialized..PpThe.Fn getoptfunction returns \-1 when the argument list is exhausted.The interpretation of options in the argument list may be cancelledby the option.Ql --(double dash) which causes.Fn getoptto signal the end of argument processing and return \-1.When all options have been processed (i.e., up to the first non-optionargument),.Fn getoptreturns \-1..Sh RETURN VALUESThe.Fn getoptfunction returns the next known option character in.Fa optstring .If.Fn getoptencounters a character not found in.Fa optstringor if it detects a missing option argument,it returns.Ql \&?(question mark).If.Fa optstringhas a leading.Ql \&:then a missing option argument causes.Ql \&:to be returned instead of.Ql \&? .In either case, the variable.Va optoptis set to the character that caused the error.The.Fn getoptfunction returns \-1 when the argument list is exhausted..Sh EXAMPLES.Bd -literal -compact#include <unistd.h>int bflag, ch, fd;
bflag = 0;while ((ch = getopt(argc, argv, "bf:")) != -1) { switch (ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { (void)fprintf(stderr, "myname: %s: %s\en", optarg, strerror(errno)); exit(1); } break; case '?': default: usage(); }}argc -= optind;argv += optind;.Ed.Sh DIAGNOSTICSIf the.Fn getoptfunction encounters a character not found in the string.Fa optstringor detectsa missing option argument it writes an error message to the.Dv stderrand returns.Ql \&? .Setting.Va opterrto a zero will disable these error messages.If.Fa optstringhas a leading.Ql \&:then a missing option argument causes a.Ql \&:to be returned in addition to suppressing any error messages..PpOption arguments are allowed to begin with.Dq Li \- ;this is reasonable but reduces the amount of error checking possible..Sh SEE ALSO.Xr getopt 1 ,.Xr getopt_long 3 ,.Xr getsubopt 3.Sh STANDARDSThe.Va optresetvariable was added to make it possible to call the.Fn getoptfunction multiple times.This is an extension to the.St -p1003.2specification..Sh HISTORYThe.Fn getoptfunction appeared in.Bx 4.3 ..Sh BUGSThe.Fn getoptfunction was once specified to return.Dv EOFinstead of \-1.This was changed by.St -p1003.2-92to decouple.Fn getoptfrom.In stdio.h ..PpA single dash.Dq Li -may be specified as a character in.Fa optstring ,however it should.Em neverhave an argument associated with it.This allows.Fn getoptto be used with programs that expect.Dq Li -as an option flag.This practice is wrong, and should not be used in any current development.It is provided for backward compatibility.Em only .Care should be taken not to use.Ql \&-as the first character in.Fa optstringto avoid a semantic conflict with.Tn GNU.Fn getopt ,which assigns different meaning to an.Fa optstringthat begins with a.Ql \&- .By default, a single dash causes.Fn getoptto return \-1..PpIt is also possible to handle digits as option letters.This allows.Fn getoptto be used with programs that expect a number.Pq Dq Li \&-\&3as an option.This practice is wrong, and should not be used in any current development.It is provided for backward compatibility.Em only .The following code fragment works in most cases..Bd -literal -offset indentint ch;long length;char *p, *ep;
while ((ch = getopt(argc, argv, "0123456789")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': p = argv[optind - 1]; if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) { length = ch - '0'; ep = ""; } else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) { length = strtol((p = argv[optind] + 1), \*[Am]ep, 10); optind++; optreset = 1; } else usage(); if (*ep != '\e0') errx(EX_USAGE, "illegal number -- %s", p); break; }.Ed