Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507.\" $OpenBSD: getopt_long.3,v 1.10 2004/01/06 23:44:28 fgsch Exp $.\" $NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc 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..\" 3. 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_long.3,v 1.13 2005/01/20 09:17:04 ru Exp $.\".Dd April 1, 2000.Dt GETOPT_LONG 3.Os.Sh NAME.Nm getopt_long ,.Nm getopt_long_only.Nd get long options from command line argument list.Sh LIBRARY.Lb libc.Sh SYNOPSIS.In getopt.h.Vt extern char *optarg ;.Vt extern int optind ;.Vt extern int optopt ;.Vt extern int opterr ;.Vt extern int optreset ;.Ft int.Fo getopt_long.Fa "int argc" "char * const *argv" "const char *optstring".Fa "const struct option *longopts" "int *longindex".Fc.Ft int.Fo getopt_long_only.Fa "int argc" "char * const *argv" "const char *optstring".Fa "const struct option *longopts" "int *longindex".Fc.Sh DESCRIPTIONThe.Fn getopt_longfunction is similar to.Xr getopt 3but it accepts options in two forms: words and characters.The.Fn getopt_longfunction provides a superset of the functionality of.Xr getopt 3 .The.Fn getopt_longfunctioncan be used in two ways.In the first way, every long option understoodby the program has a corresponding short option, and the optionstructure is only used to translate from long options to shortoptions.When used in this fashion,.Fn getopt_longbehaves identically to.Xr getopt 3 .This is a good way to add long option processing to an existing programwith the minimum of rewriting..PpIn the second mechanism, a long option sets a flag in the.Vt optionstructure passed, or will store a pointer to the command line argumentin the.Vt optionstructure passed to it for options that take arguments.Additionally,the long option's argument may be specified as a single argument withan equal sign, e.g.,.Pp.Dl "myprogram --myoption=somevalue".PpWhen a long option is processed, the call to.Fn getopt_longwill return 0.For this reason, long option processing withoutshortcuts is not backwards compatible with.Xr getopt 3 ..PpIt is possible to combine these methods, providing for long optionsprocessing with short option equivalents for some options.Lessfrequently used options would be processed as long options only..PpThe.Fn getopt_longcall requires a structure to be initialized describing the longoptions.The structure is:.Bd -literal -offset indentstruct option { char *name; int has_arg; int *flag; int val;};.Ed.PpThe.Va namefield should contain the option name without the leading double dash..PpThe.Va has_argfield should be one of:.Pp.Bl -tag -width ".Dv optional_argument" -offset indent -compact.It Dv no_argumentno argument to the option is expect.It Dv required_argumentan argument to the option is required.It Dv optional_argumentan argument to the option may be presented..El.PpIf.Va flagis not.Dv NULL ,then the integer pointed to by it will be set to thevalue in the.Va valfield.If the.Va flagfield is.Dv NULL ,then the.Va valfield will be returned.Setting.Va flagto.Dv NULLand setting.Va valto the corresponding short option will make this function act justlike.Xr getopt 3 ..PpIf the.Fa longindexfield is not.Dv NULL ,then the integer pointed to by it will be set to the index of the longoption relative to.Fa longopts ..PpThe last element of the.Fa longoptsarray has to be filled with zeroes..PpThe.Fn getopt_long_onlyfunction behaves identically to.Fn getopt_longwith the exception that long options may start with.Ql -in addition to.Ql -- .If an option starting with.Ql -does not match a long option but does match a single-character option,the single-character option is returned..Sh RETURN VALUESIf the.Fa flagfield in.Vt "struct option"is.Dv NULL ,.Fn getopt_longand.Fn getopt_long_onlyreturn the value specified in the.Fa valfield, which is usually just the corresponding short option.If.Fa flagis not.Dv NULL ,these functions return 0 and store.Fa valin the location pointed to by.Fa flag .These functions return.Ql \&:if there was a missing option argument,.Ql \&?if the user specified an unknown or ambiguous option, and\-1 when the argument list has been exhausted..Sh ENVIRONMENT.Bl -tag -width ".Ev POSIXLY_CORRECT".It Ev POSIXLY_CORRECTIf set, option processing stops when the first non-option is found anda leading.Ql -or.Ql +in the.Fa optstringis ignored..El.Sh EXAMPLES.Bd -literal -compactint bflag, ch, fd;int daggerset;
/* options descriptor */static struct option longopts[] = { { "buffy", no_argument, NULL, 'b' }, { "fluoride", required_argument, NULL, 'f' }, { "daggerset", no_argument, \*[Am]daggerset, 1 }, { NULL, 0, NULL, 0 }};
bflag = 0;while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) switch (ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) == -1) err(1, "unable to open %s", optarg); break; case 0: if (daggerset) { fprintf(stderr,"Buffy will use her dagger to " "apply fluoride to dracula's teeth\en"); } break; default: usage();}argc -= optind;argv += optind;.Ed.Sh IMPLEMENTATION DIFFERENCESThis section describes differences to the.Tn GNUimplementationfound in glibc-2.1.3:.Bl -bullet.\" .It.\" Handling of.\" .Ql -.\" as first char of option string in presence of.\" environment variable.\" .Ev POSIXLY_CORRECT :.\" .Bl -tag -width ".Bx".\" .It Tn GNU.\" ignores.\" .Ev POSIXLY_CORRECT.\" and returns non-options as.\" arguments to option '\e1'..\" .It Bx.\" honors.\" .Ev POSIXLY_CORRECT.\" and stops at the first non-option..\" .El.\" .It.\" Handling of.\" .Ql -.\" within the option string (not the first character):.\" .Bl -tag -width ".Bx".\" .It Tn GNU.\" treats a.\" .Ql -.\" on the command line as a non-argument..\" .It Bx.\" a.\" .Ql -.\" within the option string matches a.\" .Ql -.\" (single dash) on the command line..\" This functionality is provided for backward compatibility with.\" programs, such as.\" .Xr su 1 ,.\" that use.\" .Ql -.\" as an option flag..\" This practice is wrong, and should not be used in any current development..\" .El.\" .It.\" Handling of.\" .Ql ::.\" in options string in presence of.\" .Ev POSIXLY_CORRECT :.\" .Bl -tag -width ".Bx".\" .It Both.\" .Tn GNU.\" and.\" .Bx.\" ignore.\" .Ev POSIXLY_CORRECT.\" here and take.\" .Ql ::.\" to.\" mean the preceding option takes an optional argument..\" .El.\" .It.\" Return value in case of missing argument if first character.\" (after.\" .Ql +.\" or.\" .Ql - ).\" in option string is not.\" .Ql \&: :.\" .Bl -tag -width ".Bx".\" .It Tn GNU.\" returns.\" .Ql \&?.\" .It Bx.\" returns.\" .Ql \&:.\" (since.\" .Bx Ns 's.\" .Fn getopt.\" does)..\" .El.\" .It.\" Handling of.\" .Ql --a.\" in getopt:.\" .Bl -tag -width ".Bx".\" .It Tn GNU.\" parses this as option.\" .Ql - ,.\" option.\" .Ql a ..\" .It Bx.\" parses this as.\" .Ql -- ,.\" and returns \-1 (ignoring the.\" .Ql a ) ..\" (Because the original.\" .Fn getopt.\" does.).\" .El.ItSetting of.Va optoptfor long options with.Va flag!=.Dv NULL :.Bl -tag -width ".Bx".It Tn GNUsets.Va optoptto.Va val ..It Bxsets.Va optoptto 0 (since.Va valwould never be returned)..El.\" .It.\" Handling of.\" .Ql -W.\" with.\" .Ql W;.\" in option string in.\" .Fn getopt.\" (not.\" .Fn getopt_long ) :.\" .Bl -tag -width ".Bx".\" .It Tn GNU.\" causes a segfault..\" .It Bx.\" no special handling is done;.\" .Ql W;.\" is interpreted as two separate options, neither of which take an argument..\" .El.ItSetting of.Va optargfor long options without an argument that areinvoked via.Ql -W.Ql ( W;in option string):.Bl -tag -width ".Bx".It Tn GNUsets.Va optargto the option name (the argument of.Ql -W ) ..It Bxsets.Va optargto.Dv NULL(the argument of the long option)..El.ItHandling of.Ql -Wwith an argument that is not (a prefix to) a knownlong option.Ql ( W;in option string):.Bl -tag -width ".Bx".It Tn GNUreturns.Ql -Wwith.Va optargset to the unknown option..It Bxtreats this as an error (unknown option) and returns.Ql \&?with.Va optoptset to 0 and.Va optargset to.Dv NULL(as.Tn GNU Ns 'sman page documents)..El.\" .It.\" The error messages are different..It.Bxdoes not permute the argument vector at the same points inthe calling sequence as.Tn GNUdoes.The aspects normally used bythe caller (ordering after \-1 is returned, value of.Va optindrelativeto current positions) are the same, though.(We do fewer variable swaps.).El.Sh SEE ALSO.Xr getopt 3.Sh HISTORYThe.Fn getopt_longand.Fn getopt_long_onlyfunctions first appeared in.Tn GNUlibiberty.The first.Bximplementation of.Fn getopt_longappeared in.Nx 1.5 ,the first.Bximplementation of.Fn getopt_long_onlyin.Ox 3.3 ..Fxfirst included.Fn getopt_longin.Fx 5.0 ,.Fn getopt_long_onlyin.Fx 5.2 ..Sh BUGSThe.Fa argvargument is not really.Vt constas its elements may be permuted (unless.Ev POSIXLY_CORRECTis set)..PpThe implementation can completely replace.Xr getopt 3 ,but right now we are using separate code.