1*a35a7e76SEric Melville.\" $NetBSD: getopt_long.3,v 1.8 2002/06/03 12:01:43 wiz Exp $ 2*a35a7e76SEric Melville.\" $FreeBSD$ 3*a35a7e76SEric Melville.\" 4*a35a7e76SEric Melville.\" Copyright (c) 1988, 1991, 1993 5*a35a7e76SEric Melville.\" The Regents of the University of California. All rights reserved. 6*a35a7e76SEric Melville.\" 7*a35a7e76SEric Melville.\" Redistribution and use in source and binary forms, with or without 8*a35a7e76SEric Melville.\" modification, are permitted provided that the following conditions 9*a35a7e76SEric Melville.\" are met: 10*a35a7e76SEric Melville.\" 1. Redistributions of source code must retain the above copyright 11*a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer. 12*a35a7e76SEric Melville.\" 2. Redistributions in binary form must reproduce the above copyright 13*a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer in the 14*a35a7e76SEric Melville.\" documentation and/or other materials provided with the distribution. 15*a35a7e76SEric Melville.\" 3. All advertising materials mentioning features or use of this software 16*a35a7e76SEric Melville.\" must display the following acknowledgement: 17*a35a7e76SEric Melville.\" This product includes software developed by the University of 18*a35a7e76SEric Melville.\" California, Berkeley and its contributors. 19*a35a7e76SEric Melville.\" 4. Neither the name of the University nor the names of its contributors 20*a35a7e76SEric Melville.\" may be used to endorse or promote products derived from this software 21*a35a7e76SEric Melville.\" without specific prior written permission. 22*a35a7e76SEric Melville.\" 23*a35a7e76SEric Melville.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24*a35a7e76SEric Melville.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25*a35a7e76SEric Melville.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26*a35a7e76SEric Melville.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27*a35a7e76SEric Melville.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28*a35a7e76SEric Melville.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29*a35a7e76SEric Melville.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30*a35a7e76SEric Melville.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31*a35a7e76SEric Melville.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32*a35a7e76SEric Melville.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*a35a7e76SEric Melville.\" SUCH DAMAGE. 34*a35a7e76SEric Melville.\" 35*a35a7e76SEric Melville.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 36*a35a7e76SEric Melville.\" 37*a35a7e76SEric Melville.Dd April 1, 2000 38*a35a7e76SEric Melville.Dt GETOPT_LONG 3 39*a35a7e76SEric Melville.Os 40*a35a7e76SEric Melville.Sh NAME 41*a35a7e76SEric Melville.Nm getopt_long 42*a35a7e76SEric Melville.Nd get long options from command line argument list 43*a35a7e76SEric Melville.Sh LIBRARY 44*a35a7e76SEric Melville.Lb libc 45*a35a7e76SEric Melville.Sh SYNOPSIS 46*a35a7e76SEric Melville.Fd #include \*[Lt]getopt.h\*[Gt] 47*a35a7e76SEric Melville.Ft int 48*a35a7e76SEric Melville.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index" 49*a35a7e76SEric Melville.Sh DESCRIPTION 50*a35a7e76SEric MelvilleThe 51*a35a7e76SEric Melville.Fn getopt_long 52*a35a7e76SEric Melvillefunction is similar to 53*a35a7e76SEric Melville.Xr getopt 3 54*a35a7e76SEric Melvillebut it accepts options in two forms: words and characters. The 55*a35a7e76SEric Melville.Fn getopt_long 56*a35a7e76SEric Melvillefunction provides a superset of of the functionality of 57*a35a7e76SEric Melville.Xr getopt 3 . 58*a35a7e76SEric Melville.Fn getopt_long 59*a35a7e76SEric Melvillecan be used in two ways. In the first way, every long option understood 60*a35a7e76SEric Melvilleby the program has a corresponding short option, and the option 61*a35a7e76SEric Melvillestructure is only used to translate from long options to short 62*a35a7e76SEric Melvilleoptions. When used in this fashion, 63*a35a7e76SEric Melville.Fn getopt_long 64*a35a7e76SEric Melvillebehaves identically to 65*a35a7e76SEric Melville.Xr getopt 3 . 66*a35a7e76SEric MelvilleThis is a good way to add long option processing to an existing program 67*a35a7e76SEric Melvillewith the minimum of rewriting. 68*a35a7e76SEric Melville.Pp 69*a35a7e76SEric MelvilleIn the second mechanism, a long option sets a flag in the 70*a35a7e76SEric Melville.Fa option 71*a35a7e76SEric Melvillestructure passed, or will store a pointer to the command line argument 72*a35a7e76SEric Melvillein the 73*a35a7e76SEric Melville.Fa option 74*a35a7e76SEric Melvillestructure passed to it for options that take arguments. Additionally, 75*a35a7e76SEric Melvillethe long option's argument may be specified as a single argument with 76*a35a7e76SEric Melvillean equal sign, e.g. 77*a35a7e76SEric Melville.Bd -literal 78*a35a7e76SEric Melvillemyprogram --myoption=somevalue 79*a35a7e76SEric Melville.Ed 80*a35a7e76SEric Melville.Pp 81*a35a7e76SEric MelvilleWhen a long option is processed the call to 82*a35a7e76SEric Melville.Fn getopt_long 83*a35a7e76SEric Melvillewill return 0. For this reason, long option processing without 84*a35a7e76SEric Melvilleshortcuts is not backwards compatible with 85*a35a7e76SEric Melville.Xr getopt 3 . 86*a35a7e76SEric Melville.Pp 87*a35a7e76SEric MelvilleIt is possible to combine these methods, providing for long options 88*a35a7e76SEric Melvilleprocessing with short option equivalents for some options. Less 89*a35a7e76SEric Melvillefrequently used options would be processed as long options only. 90*a35a7e76SEric Melville.Pp 91*a35a7e76SEric MelvilleThe 92*a35a7e76SEric Melville.Fn getopt_long 93*a35a7e76SEric Melvillecall requires a structure to be initialized describing the long 94*a35a7e76SEric Melvilleoptions. The structure is: 95*a35a7e76SEric Melville.Bd -literal 96*a35a7e76SEric Melvillestruct option { 97*a35a7e76SEric Melville char *name; 98*a35a7e76SEric Melville int has_arg; 99*a35a7e76SEric Melville int *flag; 100*a35a7e76SEric Melville int val; 101*a35a7e76SEric Melville}; 102*a35a7e76SEric Melville.Ed 103*a35a7e76SEric Melville.Pp 104*a35a7e76SEric MelvilleThe 105*a35a7e76SEric Melville.Fa name 106*a35a7e76SEric Melvillefield should contain the option name without the leading double dash. 107*a35a7e76SEric Melville.Pp 108*a35a7e76SEric MelvilleThe 109*a35a7e76SEric Melville.Fa has_arg 110*a35a7e76SEric Melvillefield should be one of: 111*a35a7e76SEric Melville.Bl -tag -width "optional_argument" 112*a35a7e76SEric Melville.It Li no_argument 113*a35a7e76SEric Melvilleno argument to the option is expect. 114*a35a7e76SEric Melville.It Li required_argument 115*a35a7e76SEric Melvillean argument to the option is required. 116*a35a7e76SEric Melville.It Li optional_argument 117*a35a7e76SEric Melvillean argument to the option may be presented. 118*a35a7e76SEric Melville.El 119*a35a7e76SEric Melville.Pp 120*a35a7e76SEric MelvilleIf 121*a35a7e76SEric Melville.Fa flag 122*a35a7e76SEric Melvilleis non-NULL, then the integer pointed to by it will be set to the 123*a35a7e76SEric Melvillevalue in the 124*a35a7e76SEric Melville.Fa val 125*a35a7e76SEric Melvillefield. If the 126*a35a7e76SEric Melville.Fa flag 127*a35a7e76SEric Melvillefield is NULL, then the 128*a35a7e76SEric Melville.Fa val 129*a35a7e76SEric Melvillefield will be returned. Setting 130*a35a7e76SEric Melville.Fa flag 131*a35a7e76SEric Melvilleto NULL and setting 132*a35a7e76SEric Melville.Fa val 133*a35a7e76SEric Melvilleto the corresponding short option will make this function act just 134*a35a7e76SEric Melvillelike 135*a35a7e76SEric Melville.Xr getopt 3 . 136*a35a7e76SEric Melville.Sh EXAMPLES 137*a35a7e76SEric Melville.Bd -literal -compact 138*a35a7e76SEric Melvilleextern char *optarg; 139*a35a7e76SEric Melvilleextern int optind; 140*a35a7e76SEric Melvilleint bflag, ch, fd; 141*a35a7e76SEric Melvilleint daggerset; 142*a35a7e76SEric Melville 143*a35a7e76SEric Melville/* options descriptor */ 144*a35a7e76SEric Melvillestatic struct option longopts[] = { 145*a35a7e76SEric Melville { "buffy", no_argument, 0, 'b' }, 146*a35a7e76SEric Melville { "floride", required_argument, 0, 'f' }, 147*a35a7e76SEric Melville { "daggerset", no_argument, \*[Am]daggerset, 1 }, 148*a35a7e76SEric Melville { 0, 0, 0, 0 } 149*a35a7e76SEric Melville}; 150*a35a7e76SEric Melville 151*a35a7e76SEric Melvillebflag = 0; 152*a35a7e76SEric Melvillewhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) 153*a35a7e76SEric Melville switch(ch) { 154*a35a7e76SEric Melville case 'b': 155*a35a7e76SEric Melville bflag = 1; 156*a35a7e76SEric Melville break; 157*a35a7e76SEric Melville case 'f': 158*a35a7e76SEric Melville if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { 159*a35a7e76SEric Melville (void)fprintf(stderr, 160*a35a7e76SEric Melville "myname: %s: %s\en", optarg, strerror(errno)); 161*a35a7e76SEric Melville exit(1); 162*a35a7e76SEric Melville } 163*a35a7e76SEric Melville break; 164*a35a7e76SEric Melville case 0: 165*a35a7e76SEric Melville if(daggerset) { 166*a35a7e76SEric Melville fprintf(stderr,"Buffy will use her dagger to " 167*a35a7e76SEric Melville "apply floride to dracula's teeth\en"); 168*a35a7e76SEric Melville } 169*a35a7e76SEric Melville break; 170*a35a7e76SEric Melville case '?': 171*a35a7e76SEric Melville default: 172*a35a7e76SEric Melville usage(); 173*a35a7e76SEric Melville} 174*a35a7e76SEric Melvilleargc -= optind; 175*a35a7e76SEric Melvilleargv += optind; 176*a35a7e76SEric Melville.Ed 177*a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES 178*a35a7e76SEric MelvilleThis section describes differences to the GNU implementation 179*a35a7e76SEric Melvillefound in glibc-2.1.3: 180*a35a7e76SEric Melville.Bl -tag -width "xxx" 181*a35a7e76SEric Melville.It Li o 182*a35a7e76SEric Melvillehandling of - as first char of option string in presence of 183*a35a7e76SEric Melvilleenvironment variable POSIXLY_CORRECT: 184*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 185*a35a7e76SEric Melville.It Li GNU 186*a35a7e76SEric Melvilleignores POSIXLY_CORRECT and returns non-options as 187*a35a7e76SEric Melvillearguments to option '\e1'. 188*a35a7e76SEric Melville.It Li NetBSD 189*a35a7e76SEric Melvillehonors POSIXLY_CORRECT and stops at the first non-option. 190*a35a7e76SEric Melville.El 191*a35a7e76SEric Melville.It Li o 192*a35a7e76SEric Melvillehandling of :: in options string in presence of POSIXLY_CORRECT: 193*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 194*a35a7e76SEric Melville.It Li Both 195*a35a7e76SEric MelvilleGNU and NetBSD ignore POSIXLY_CORRECT here and take :: to 196*a35a7e76SEric Melvillemean the preceding option takes an optional argument. 197*a35a7e76SEric Melville.El 198*a35a7e76SEric Melville.It Li o 199*a35a7e76SEric Melvillereturn value in case of missing argument if first character 200*a35a7e76SEric Melville(after + or -) in option string is not ':': 201*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 202*a35a7e76SEric Melville.It Li GNU 203*a35a7e76SEric Melvillereturns '?' 204*a35a7e76SEric Melville.It NetBSD 205*a35a7e76SEric Melvillereturns ':' (since NetBSD's getopt does). 206*a35a7e76SEric Melville.El 207*a35a7e76SEric Melville.It Li o 208*a35a7e76SEric Melvillehandling of --a in getopt: 209*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 210*a35a7e76SEric Melville.It Li GNU 211*a35a7e76SEric Melvilleparses this as option '-', option 'a'. 212*a35a7e76SEric Melville.It Li NetBSD 213*a35a7e76SEric Melvilleparses this as '--', and returns -1 (ignoring the a). (Because 214*a35a7e76SEric Melvillethe original getopt does.) 215*a35a7e76SEric Melville.El 216*a35a7e76SEric Melville.It Li o 217*a35a7e76SEric Melvillesetting of optopt for long options with flag != NULL: 218*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 219*a35a7e76SEric Melville.It Li GNU 220*a35a7e76SEric Melvillesets optopt to val. 221*a35a7e76SEric Melville.It Li NetBSD 222*a35a7e76SEric Melvillesets optopt to 0 (since val would never be returned). 223*a35a7e76SEric Melville.El 224*a35a7e76SEric Melville.It Li o 225*a35a7e76SEric Melvillehandling of -W with W; in option string in getopt (not getopt_long): 226*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 227*a35a7e76SEric Melville.It Li GNU 228*a35a7e76SEric Melvillecauses a segfault. 229*a35a7e76SEric Melville.It Li NetBSD 230*a35a7e76SEric Melvillereturns -1, with optind pointing past the argument of -W 231*a35a7e76SEric Melville(as if `-W arg' were `--arg', and thus '--' had been found). 232*a35a7e76SEric Melville.\" How should we treat W; in the option string when called via 233*a35a7e76SEric Melville.\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning? 234*a35a7e76SEric Melville.El 235*a35a7e76SEric Melville.It Li o 236*a35a7e76SEric Melvillesetting of optarg for long options without an argument that are 237*a35a7e76SEric Melvilleinvoked via -W (W; in option string): 238*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 239*a35a7e76SEric Melville.It Li GNU 240*a35a7e76SEric Melvillesets optarg to the option name (the argument of -W). 241*a35a7e76SEric Melville.It Li NetBSD 242*a35a7e76SEric Melvillesets optarg to NULL (the argument of the long option). 243*a35a7e76SEric Melville.El 244*a35a7e76SEric Melville.It Li o 245*a35a7e76SEric Melvillehandling of -W with an argument that is not (a prefix to) a known 246*a35a7e76SEric Melvillelong option (W; in option string): 247*a35a7e76SEric Melville.Bl -tag -width "NetBSD" 248*a35a7e76SEric Melville.It Li GNU 249*a35a7e76SEric Melvillereturns -W with optarg set to the unknown option. 250*a35a7e76SEric Melville.It Li NetBSD 251*a35a7e76SEric Melvilletreats this as an error (unknown option) and returns '?' with 252*a35a7e76SEric Melvilleoptopt set to 0 and optarg set to NULL (as GNU's man page 253*a35a7e76SEric Melvilledocuments). 254*a35a7e76SEric Melville.El 255*a35a7e76SEric Melville.It Li o 256*a35a7e76SEric MelvilleThe error messages are different. 257*a35a7e76SEric Melville.It Li o 258*a35a7e76SEric MelvilleNetBSD does not permute the argument vector at the same points in 259*a35a7e76SEric Melvillethe calling sequence as GNU does. The aspects normally used by 260*a35a7e76SEric Melvillethe caller (ordering after -1 is returned, value of optind relative 261*a35a7e76SEric Melvilleto current positions) are the same, though. (We do fewer variable 262*a35a7e76SEric Melvilleswaps.) 263*a35a7e76SEric Melville.El 264*a35a7e76SEric Melville.Sh SEE ALSO 265*a35a7e76SEric Melville.Xr getopt 3 266*a35a7e76SEric Melville.Sh HISTORY 267*a35a7e76SEric MelvilleThe 268*a35a7e76SEric Melville.Fn getopt_long 269*a35a7e76SEric Melvillefunction first appeared in GNU libiberty. The first 270*a35a7e76SEric Melville.Nx 271*a35a7e76SEric Melvilleimplementation appeared in 1.5. 272*a35a7e76SEric Melville.Sh BUGS 273*a35a7e76SEric MelvilleThe implementation, can completelely replace 274*a35a7e76SEric Melville.Xr getopt 3 , 275*a35a7e76SEric Melvillebut right now we are using separate code. 276