1*54feed06SJason King.\" 2*54feed06SJason King.\" Copyright (c) 1988, 1991, 1993 3*54feed06SJason King.\" The Regents of the University of California. All rights reserved. 4*54feed06SJason King.\" 5*54feed06SJason King.\" Redistribution and use in source and binary forms, with or without 6*54feed06SJason King.\" modification, are permitted provided that the following conditions 7*54feed06SJason King.\" are met: 8*54feed06SJason King.\" 1. Redistributions of source code must retain the above copyright 9*54feed06SJason King.\" notice, this list of conditions and the following disclaimer. 10*54feed06SJason King.\" 2. Redistributions in binary form must reproduce the above copyright 11*54feed06SJason King.\" notice, this list of conditions and the following disclaimer in the 12*54feed06SJason King.\" documentation and/or other materials provided with the distribution. 13*54feed06SJason King.\" 3. Neither the name of the University nor the names of its contributors 14*54feed06SJason King.\" may be used to endorse or promote products derived from this software 15*54feed06SJason King.\" without specific prior written permission. 16*54feed06SJason King.\" 17*54feed06SJason King.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18*54feed06SJason King.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*54feed06SJason King.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*54feed06SJason King.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21*54feed06SJason King.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*54feed06SJason King.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*54feed06SJason King.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*54feed06SJason King.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*54feed06SJason King.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*54feed06SJason King.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*54feed06SJason King.\" SUCH DAMAGE. 28*54feed06SJason King.\" 29*54feed06SJason King.\" 30*54feed06SJason King.\" Copyright 2018 Jason King 31*54feed06SJason King.\" Copyright 2018, Joyent, Inc. 32*54feed06SJason King.\" 33*54feed06SJason King.Dd July 17, 2018 34*54feed06SJason King.Dt GETOPT_LONG 3C 35*54feed06SJason King.Os 36*54feed06SJason King.Sh NAME 37*54feed06SJason King.Nm getopt_long , 38*54feed06SJason King.Nm getopt_long_only 39*54feed06SJason King.Nd get long options from command line argument list 40*54feed06SJason King.Sh SYNOPSIS 41*54feed06SJason King.In getopt.h 42*54feed06SJason King.Vt extern char *optarg ; 43*54feed06SJason King.Vt extern int optind ; 44*54feed06SJason King.Vt extern int optopt ; 45*54feed06SJason King.Vt extern int opterr ; 46*54feed06SJason King.Ft int 47*54feed06SJason King.Fo getopt_long 48*54feed06SJason King.Fa "int argc" 49*54feed06SJason King.Fa "char * const *argv" 50*54feed06SJason King.Fa "const char *optstring" 51*54feed06SJason King.Fa "const struct option *longopts" 52*54feed06SJason King.Fa "int *longindex" 53*54feed06SJason King.Fc 54*54feed06SJason King.Ft int 55*54feed06SJason King.Fo getopt_long_only 56*54feed06SJason King.Fa "int argc" 57*54feed06SJason King.Fa "char * const *argv" 58*54feed06SJason King.Fa "const char *optstring" 59*54feed06SJason King.Fa "const struct option *longopts" 60*54feed06SJason King.Fa "int *longindex" 61*54feed06SJason King.Fc 62*54feed06SJason King.Ft int 63*54feed06SJason King.Fo getopt_long_clip 64*54feed06SJason King.Fa "int argc" 65*54feed06SJason King.Fa "char * const *argv" 66*54feed06SJason King.Fa "const char *optstring" 67*54feed06SJason King.Fa "const struct option *longopts" 68*54feed06SJason King.Fa "int *longindex" 69*54feed06SJason King.Fc 70*54feed06SJason King.Sh DESCRIPTION 71*54feed06SJason KingThe 72*54feed06SJason King.Fn getopt_long 73*54feed06SJason Kingfunction is similar to 74*54feed06SJason King.Xr getopt 3C 75*54feed06SJason Kingbut it accepts options in two forms: words and characters. 76*54feed06SJason KingThe 77*54feed06SJason King.Fn getopt_long 78*54feed06SJason Kingfunction provides a superset of the functionality of 79*54feed06SJason King.Xr getopt 3C . 80*54feed06SJason KingThe 81*54feed06SJason King.Fn getopt_long 82*54feed06SJason Kingfunction 83*54feed06SJason Kingcan be used in two ways. 84*54feed06SJason King.Pp 85*54feed06SJason KingIn the first way, every long option understood 86*54feed06SJason Kingby the program has a corresponding short option, and the option 87*54feed06SJason Kingstructure is only used to translate from long options to short 88*54feed06SJason Kingoptions. 89*54feed06SJason KingWhen used in this fashion, 90*54feed06SJason King.Fn getopt_long 91*54feed06SJason Kingbehaves identically to 92*54feed06SJason King.Xr getopt 3C . 93*54feed06SJason KingThis is a good way to add long option processing to an existing program 94*54feed06SJason Kingwith the minimum of rewriting. 95*54feed06SJason King.Pp 96*54feed06SJason KingIn the second mechanism, a long option sets a flag in the 97*54feed06SJason King.Vt option 98*54feed06SJason Kingstructure passed, or will store a pointer to the command line argument 99*54feed06SJason Kingin the 100*54feed06SJason King.Vt option 101*54feed06SJason Kingstructure passed to it for options that take arguments. 102*54feed06SJason KingAdditionally, 103*54feed06SJason Kingthe long option's argument may be specified as a single argument with 104*54feed06SJason Kingan equal sign, e.g., 105*54feed06SJason King.Pp 106*54feed06SJason King.Dl "myprogram --myoption=somevalue" 107*54feed06SJason King.Pp 108*54feed06SJason KingWhen a long option is processed, the call to 109*54feed06SJason King.Fn getopt_long 110*54feed06SJason Kingwill return 0. 111*54feed06SJason KingFor this reason, long option processing without 112*54feed06SJason Kingshortcuts is not backwards compatible with 113*54feed06SJason King.Xr getopt 3C . 114*54feed06SJason King.Pp 115*54feed06SJason KingIt is possible to combine these methods, providing for long options 116*54feed06SJason Kingprocessing with short option equivalents for some options. 117*54feed06SJason KingLess 118*54feed06SJason Kingfrequently used options would be processed as long options only. 119*54feed06SJason King.Pp 120*54feed06SJason KingIn 121*54feed06SJason King.Fn getopt_long 122*54feed06SJason Kingand 123*54feed06SJason King.Fn getopt_long_only , 124*54feed06SJason King.Fa optstring 125*54feed06SJason Kingacts similar to 126*54feed06SJason King.Fa optstring 127*54feed06SJason Kingin 128*54feed06SJason King.Xr getopt 3C . 129*54feed06SJason KingIn addition, 130*54feed06SJason King.Fa optstring 131*54feed06SJason Kingcan begin with 132*54feed06SJason King.Ql + 133*54feed06SJason Kingor 134*54feed06SJason King.Ql - . 135*54feed06SJason KingIf 136*54feed06SJason King.Fa optstring 137*54feed06SJason Kingbegins with 138*54feed06SJason King.Ql + , 139*54feed06SJason Kingthe first non-option terminates option processing. 140*54feed06SJason KingThis is equivalent to setting the environment variable 141*54feed06SJason King.Ev POSIXLY_CORRECT . 142*54feed06SJason KingIf 143*54feed06SJason King.Fa optstring 144*54feed06SJason Kingbegins with 145*54feed06SJason King.Ql - , 146*54feed06SJason Kingnon-options are treated as options to the argument 147*54feed06SJason King.Ql \e1 . 148*54feed06SJason King.Pp 149*54feed06SJason KingIf 150*54feed06SJason King.Fa optstring 151*54feed06SJason Kingdoes not begin with 152*54feed06SJason King.Ql + 153*54feed06SJason Kingand 154*54feed06SJason King.Ev POSIXLY_CORRECT 155*54feed06SJason Kingis not set, if 156*54feed06SJason King.Ql W\&; 157*54feed06SJason Kingappears in 158*54feed06SJason King.Fa optstring , 159*54feed06SJason King.Ql "-W myoption" 160*54feed06SJason Kingis treated the same as 161*54feed06SJason King.Ql "--myoption" 162*54feed06SJason Kingand 163*54feed06SJason King.Va optarg 164*54feed06SJason Kingis set to 165*54feed06SJason King.Ql myoption . 166*54feed06SJason King.Pp 167*54feed06SJason KingIn 168*54feed06SJason King.Fn getopt_long_clip , 169*54feed06SJason King.Ql + 170*54feed06SJason Kingand 171*54feed06SJason King.Ql - 172*54feed06SJason Kingare ignored at the beginning of a string. 173*54feed06SJason King.Pp 174*54feed06SJason KingThe 175*54feed06SJason King.Fn getopt_long , 176*54feed06SJason King.Fn getopt_long_only , 177*54feed06SJason Kingand 178*54feed06SJason King.Fn getopt_long_clip 179*54feed06SJason Kingfunctions require a structure to be initialized describing the long 180*54feed06SJason Kingoptions. 181*54feed06SJason KingThe structure is: 182*54feed06SJason King.Bd -literal -offset indent 183*54feed06SJason Kingstruct option { 184*54feed06SJason King char *name; 185*54feed06SJason King int has_arg; 186*54feed06SJason King int *flag; 187*54feed06SJason King int val; 188*54feed06SJason King}; 189*54feed06SJason King.Ed 190*54feed06SJason King.Pp 191*54feed06SJason KingThe 192*54feed06SJason King.Fa name 193*54feed06SJason Kingfield should contain the option name without the leading double dash. 194*54feed06SJason King.Pp 195*54feed06SJason KingThe 196*54feed06SJason King.Fa has_arg 197*54feed06SJason Kingfield should be one of: 198*54feed06SJason King.Pp 199*54feed06SJason King.Bl -tag -width ".Dv optional_argument" -offset indent -compact 200*54feed06SJason King.It Dv no_argument 201*54feed06SJason Kingno argument to the option is expected 202*54feed06SJason King.It Dv required_argument 203*54feed06SJason Kingan argument to the option is required 204*54feed06SJason King.It Dv optional_argument 205*54feed06SJason Kingan argument to the option may be presented 206*54feed06SJason King.El 207*54feed06SJason King.Pp 208*54feed06SJason KingIf 209*54feed06SJason King.Fa flag 210*54feed06SJason Kingis not 211*54feed06SJason King.Dv NULL , 212*54feed06SJason Kingthen the integer pointed to by it will be set to the 213*54feed06SJason Kingvalue in the 214*54feed06SJason King.Fa val 215*54feed06SJason Kingfield and 216*54feed06SJason King.Va optopt 217*54feed06SJason Kingwill be set to 218*54feed06SJason King.Sy 0 . 219*54feed06SJason KingIf the 220*54feed06SJason King.Fa flag 221*54feed06SJason Kingfield is 222*54feed06SJason King.Dv NULL , 223*54feed06SJason Kingthen the 224*54feed06SJason King.Fa val 225*54feed06SJason Kingfield will be returned and 226*54feed06SJason King.Va optopt 227*54feed06SJason Kingis set to the value in the 228*54feed06SJason King.Fa val 229*54feed06SJason Kingfield. 230*54feed06SJason KingSetting 231*54feed06SJason King.Fa flag 232*54feed06SJason Kingto 233*54feed06SJason King.Dv NULL 234*54feed06SJason Kingand setting 235*54feed06SJason King.Fa val 236*54feed06SJason Kingto the corresponding short option will make this function act just 237*54feed06SJason Kinglike 238*54feed06SJason King.Xr getopt 3C . 239*54feed06SJason King.Pp 240*54feed06SJason KingIf the 241*54feed06SJason King.Fa longindex 242*54feed06SJason Kingfield is not 243*54feed06SJason King.Dv NULL , 244*54feed06SJason Kingthen the integer pointed to by it will be set to the index of the long 245*54feed06SJason Kingoption relative to 246*54feed06SJason King.Fa longopts . 247*54feed06SJason King.Pp 248*54feed06SJason KingThe last element of the 249*54feed06SJason King.Fa longopts 250*54feed06SJason Kingarray has to be filled with zeroes. 251*54feed06SJason King.Pp 252*54feed06SJason KingThe 253*54feed06SJason King.Fn getopt_long_only 254*54feed06SJason Kingfunction behaves identically to 255*54feed06SJason King.Fn getopt_long 256*54feed06SJason Kingwith the exception that long options may start with 257*54feed06SJason King.Ql - 258*54feed06SJason Kingin addition to 259*54feed06SJason King.Ql -- . 260*54feed06SJason KingIf an option starting with 261*54feed06SJason King.Ql - 262*54feed06SJason Kingdoes not match a long option but does match a single-character option, 263*54feed06SJason Kingthe single-character option is returned. 264*54feed06SJason King.Pp 265*54feed06SJason KingThe 266*54feed06SJason King.Fn getopt_long_clip 267*54feed06SJason Kingfunction is a variation of 268*54feed06SJason King.Fn getopt_long 269*54feed06SJason Kingexcept that options must also adhere to the Sun CLIP specification. 270*54feed06SJason KingSpecifically, the major differences from 271*54feed06SJason King.Fn getopt_long 272*54feed06SJason Kingare: 273*54feed06SJason King.Bl -bullet -offset indent 274*54feed06SJason King.It 275*54feed06SJason KingAll option arguments are required 276*54feed06SJason King.Po 277*54feed06SJason King.Dv optional_argument 278*54feed06SJason Kingis treated the same as 279*54feed06SJason King.Dv required_argument 280*54feed06SJason King.Pc . 281*54feed06SJason King.It 282*54feed06SJason KingLong options cannot be abbreviated on the command line. 283*54feed06SJason King.It 284*54feed06SJason KingLong options must use a double dash 285*54feed06SJason King.Pq Ql -- . 286*54feed06SJason King.It 287*54feed06SJason KingOption processing stops at the first non-option. 288*54feed06SJason King.It 289*54feed06SJason KingAll long options must have an eqivalent short option (single character) and 290*54feed06SJason Kingvice-versa. 291*54feed06SJason King.It 292*54feed06SJason KingA leading 293*54feed06SJason King.Ql + 294*54feed06SJason Kingor 295*54feed06SJason King.Ql - 296*54feed06SJason Kingin 297*54feed06SJason King.Fa optstring 298*54feed06SJason Kingis ignored. 299*54feed06SJason King.Fa optstring 300*54feed06SJason Kingis treated as if it began after the leading 301*54feed06SJason King.Ql + 302*54feed06SJason Kingor 303*54feed06SJason King.Ql - . 304*54feed06SJason King.El 305*54feed06SJason King.Pp 306*54feed06SJason KingOn each call to 307*54feed06SJason King.Fn getopt_long , 308*54feed06SJason King.Fn getopt_long_only , 309*54feed06SJason Kingor 310*54feed06SJason King.Fn getopt_long , 311*54feed06SJason King.Va optind 312*54feed06SJason Kingis set to the 313*54feed06SJason King.Va argv 314*54feed06SJason Kingindex of the 315*54feed06SJason King.Em next 316*54feed06SJason Kingargument to be processed. 317*54feed06SJason King.Va optind 318*54feed06SJason Kingis initialized to 319*54feed06SJason King.Sy 1 320*54feed06SJason Kingprior to the first invocation of 321*54feed06SJason King.Fn getopt_long , 322*54feed06SJason King.Fn getopt_long_only , 323*54feed06SJason Kingor 324*54feed06SJason King.Fn getopt_long_clip . 325*54feed06SJason King.Pp 326*54feed06SJason KingIf 327*54feed06SJason King.Va opterr 328*54feed06SJason Kingis set to a non-zero value and 329*54feed06SJason King.Fa optstring 330*54feed06SJason Kingdoes not start with 331*54feed06SJason King.Ql \&: , 332*54feed06SJason King.Fn getopt_long , 333*54feed06SJason King.Fn getopt_long_only , 334*54feed06SJason Kingand 335*54feed06SJason King.Fn getopt_long_clip 336*54feed06SJason Kingwill print an error message to 337*54feed06SJason King.Sy stderr 338*54feed06SJason Kingwhen an error or invalid option is encountered. 339*54feed06SJason King.Sh RETURN VALUES 340*54feed06SJason KingIf the 341*54feed06SJason King.Fa flag 342*54feed06SJason Kingfield in 343*54feed06SJason King.Vt "struct option" 344*54feed06SJason Kingis 345*54feed06SJason King.Dv NULL , 346*54feed06SJason King.Fn getopt_long 347*54feed06SJason Kingand 348*54feed06SJason King.Fn getopt_long_only 349*54feed06SJason Kingreturn the value specified in the 350*54feed06SJason King.Fa val 351*54feed06SJason Kingfield, which is usually just the corresponding short option. 352*54feed06SJason KingIf 353*54feed06SJason King.Fa flag 354*54feed06SJason Kingis not 355*54feed06SJason King.Dv NULL , 356*54feed06SJason Kingthese functions return 0 and store 357*54feed06SJason King.Fa val 358*54feed06SJason Kingin the location pointed to by 359*54feed06SJason King.Fa flag . 360*54feed06SJason KingThese functions return 361*54feed06SJason King.Ql \&: 362*54feed06SJason Kingif there was a missing option argument, 363*54feed06SJason King.Ql \&? 364*54feed06SJason Kingif the user specified an unknown or ambiguous option, and 365*54feed06SJason King\-1 when the argument list has been exhausted. 366*54feed06SJason King.Pp 367*54feed06SJason KingIf a long option to 368*54feed06SJason King.Fn getopt_long_clip 369*54feed06SJason Kingis missing its equivalent short option (or vice-versa),\-1 is returned on the 370*54feed06SJason Kingfirst call to 371*54feed06SJason King.Fn getopt_long_clip , 372*54feed06SJason Kingand 373*54feed06SJason King.Dv errno 374*54feed06SJason Kingis set to 375*54feed06SJason King.Er EINVAL . 376*54feed06SJason KingIf 377*54feed06SJason King.Va opterr 378*54feed06SJason Kingis set to a non-zero value and 379*54feed06SJason King.Fa optstring 380*54feed06SJason Kingdoes not start with 381*54feed06SJason King.Ql \&: , 382*54feed06SJason Kingan error message will be written to 383*54feed06SJason King.Sy stderr . 384*54feed06SJason King.Pp 385*54feed06SJason KingIf 386*54feed06SJason King.Fa optstring 387*54feed06SJason Kingdoes not start with 388*54feed06SJason King.Ql \&: 389*54feed06SJason Kingand 390*54feed06SJason King.Fn getopt_long , 391*54feed06SJason King.Fn getopt_long_only , 392*54feed06SJason Kingor 393*54feed06SJason King.Fn getopt_long_clip 394*54feed06SJason Kingreturn 395*54feed06SJason King.Ql \&: 396*54feed06SJason Kingor 397*54feed06SJason King.Ql \&? , 398*54feed06SJason Kingif 399*54feed06SJason King.Va opterr 400*54feed06SJason Kingis set to a non-zero value, an error message is written to 401*54feed06SJason King.Dv stderr . 402*54feed06SJason King.Sh ENVIRONMENT 403*54feed06SJason KingThe following environment variables can effect the execution of 404*54feed06SJason King.Nm getopt_long , 405*54feed06SJason King.Nm getopt_long_only , 406*54feed06SJason Kingand 407*54feed06SJason King.Nm getopt_long_clip : 408*54feed06SJason King.Ev LANG , 409*54feed06SJason King.Ev LC_ALL , 410*54feed06SJason King.Ev LC_MESSAGES . 411*54feed06SJason KingSee 412*54feed06SJason King.Xr environ 5 . 413*54feed06SJason King.Bl -tag -width ".Ev POSIXLY_CORRECT" 414*54feed06SJason King.It Ev POSIXLY_CORRECT 415*54feed06SJason KingIf set, option processing stops when the first non-option is found and 416*54feed06SJason Kinga leading 417*54feed06SJason King.Ql - 418*54feed06SJason Kingor 419*54feed06SJason King.Ql + 420*54feed06SJason Kingin the 421*54feed06SJason King.Fa optstring 422*54feed06SJason Kingis ignored. 423*54feed06SJason King.El 424*54feed06SJason King.Sh USAGE 425*54feed06SJason KingSimilar to 426*54feed06SJason King.Xr getopt 3C , 427*54feed06SJason Kingsince there is no unambiguous way to detect a missing option-argument except when the 428*54feed06SJason Kingoption is the last option on the command line, the 429*54feed06SJason King.Fn getopt_long , 430*54feed06SJason King.Fn getopt_long_only , 431*54feed06SJason Kingand 432*54feed06SJason King.Fn getopt_long_clip 433*54feed06SJason Kingfunctions cannot fully check for mandatory arguments. 434*54feed06SJason KingFor example, the option string 435*54feed06SJason King.Ql ho\&: 436*54feed06SJason Kingwith an input of 437*54feed06SJason King.Ql Fl o Fl h 438*54feed06SJason Kingwill assume that 439*54feed06SJason King.Ql Fl h 440*54feed06SJason Kingis the required argument to 441*54feed06SJason King.Fl o 442*54feed06SJason Kinginstead of assuming that 443*54feed06SJason King.Fl o 444*54feed06SJason Kingis missing its option-argument. 445*54feed06SJason King.Pp 446*54feed06SJason KingLike 447*54feed06SJason King.Xr getopt 3C , 448*54feed06SJason Kinggrouping options taking or requiring arguments with other options is a violation of the 449*54feed06SJason KingBasic Utility Command syntax standard (see 450*54feed06SJason King.Xr Intro 1 ) . 451*54feed06SJason KingFor example, given the option string 452*54feed06SJason King.Ql cde\&: , 453*54feed06SJason Kingrunning: 454*54feed06SJason King.Pp 455*54feed06SJason King.Dl cmd Fl cde Ar ieio 456*54feed06SJason King.Pp 457*54feed06SJason Kingis incorrect. 458*54feed06SJason KingCurrent versions of 459*54feed06SJason King.Nm getopt_long , 460*54feed06SJason King.Nm getopt_long_only , 461*54feed06SJason Kingand 462*54feed06SJason King.Nm getopt_long_clip 463*54feed06SJason Kingaccept this, however future versions may not support this. 464*54feed06SJason KingThe correct invocation would be: 465*54feed06SJason King.Pp 466*54feed06SJason King.Dl cmd Fl cd Fl e Ar ieio 467*54feed06SJason King.Sh EXAMPLES 468*54feed06SJason King.Bd -literal -compact 469*54feed06SJason Kingint bflag, ch, fd; 470*54feed06SJason Kingint daggerset; 471*54feed06SJason King 472*54feed06SJason King/* options descriptor */ 473*54feed06SJason Kingstatic struct option longopts[] = { 474*54feed06SJason King { "buffy", no_argument, NULL, 'b' }, 475*54feed06SJason King { "fluoride", required_argument, NULL, 'f' }, 476*54feed06SJason King { "daggerset", no_argument, \*[Am]daggerset, 1 }, 477*54feed06SJason King { NULL, 0, NULL, 0 } 478*54feed06SJason King}; 479*54feed06SJason King 480*54feed06SJason Kingbflag = 0; 481*54feed06SJason Kingwhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) { 482*54feed06SJason King switch (ch) { 483*54feed06SJason King case 'b': 484*54feed06SJason King bflag = 1; 485*54feed06SJason King break; 486*54feed06SJason King case 'f': 487*54feed06SJason King if ((fd = open(optarg, O_RDONLY, 0)) == -1) 488*54feed06SJason King err(1, "unable to open %s", optarg); 489*54feed06SJason King break; 490*54feed06SJason King case 0: 491*54feed06SJason King if (daggerset) { 492*54feed06SJason King fprintf(stderr,"Buffy will use her dagger to " 493*54feed06SJason King "apply fluoride to dracula's teeth\en"); 494*54feed06SJason King } 495*54feed06SJason King break; 496*54feed06SJason King default: 497*54feed06SJason King usage(); 498*54feed06SJason King } 499*54feed06SJason King} 500*54feed06SJason Kingargc -= optind; 501*54feed06SJason Kingargv += optind; 502*54feed06SJason King.Ed 503*54feed06SJason King.Sh ERRORS 504*54feed06SJason KingThe 505*54feed06SJason King.Fn getopt_long_clip 506*54feed06SJason Kingfunction will fail if: 507*54feed06SJason King.Bl -tag -width EINVAL 508*54feed06SJason King.It Er EINVAL 509*54feed06SJason KingA short option is missing a corresponding long option, or vice-versa. 510*54feed06SJason King.El 511*54feed06SJason King.Pp 512*54feed06SJason KingThere are no errors defined for 513*54feed06SJason King.Fn getopt_long 514*54feed06SJason Kingand 515*54feed06SJason King.Fn getopt_long_only . 516*54feed06SJason King.Sh IMPLEMENTATION DIFFERENCES 517*54feed06SJason KingWhile the illumos implementations of 518*54feed06SJason King.Nm getopt_long 519*54feed06SJason Kingand 520*54feed06SJason King.Nm getopt_long_only 521*54feed06SJason Kingare broadly compatible with other implementations, the following edge cases 522*54feed06SJason Kinghave historically been known to vary among implementations: 523*54feed06SJason King.Bl -bullet 524*54feed06SJason King.It 525*54feed06SJason KingThe setting of 526*54feed06SJason King.Va optopt 527*54feed06SJason Kingfor long options with 528*54feed06SJason King.Fa flag 529*54feed06SJason King!= 530*54feed06SJason King.Dv NULL 531*54feed06SJason Kingin 532*54feed06SJason King.Vt struct option . 533*54feed06SJason KingIn illumos, 534*54feed06SJason King.Va optopt 535*54feed06SJason Kingis set to 0 (since 536*54feed06SJason King.Fa val 537*54feed06SJason Kingwould never be returned). 538*54feed06SJason King.It 539*54feed06SJason KingThe setting of 540*54feed06SJason King.Va optarg 541*54feed06SJason Kingfor long options without an argument that are 542*54feed06SJason Kinginvoked via 543*54feed06SJason King.Ql -W 544*54feed06SJason King.Ql ( W\&; 545*54feed06SJason Kingin 546*54feed06SJason King.Fa optstring ) . 547*54feed06SJason Kingillumos sets 548*54feed06SJason King.Va optarg 549*54feed06SJason Kingto the option name (the argument of 550*54feed06SJason King.Ql -W ) . 551*54feed06SJason King.It 552*54feed06SJason KingThe handling of 553*54feed06SJason King.Ql -W 554*54feed06SJason Kingwith an argument that is not (a prefix to) a known 555*54feed06SJason Kinglong option 556*54feed06SJason King.Ql ( W\&; 557*54feed06SJason Kingin 558*54feed06SJason King.Fa optstring ) . 559*54feed06SJason Kingillumos treats this as an error (unknown option) and returns 560*54feed06SJason King.Ql \&? 561*54feed06SJason Kingwith 562*54feed06SJason King.Va optopt 563*54feed06SJason Kingset to 0 and 564*54feed06SJason King.Va optarg 565*54feed06SJason Kingset to 566*54feed06SJason King.Dv NULL . 567*54feed06SJason King.It 568*54feed06SJason Kingillumos 569*54feed06SJason Kingmay not permute the argument vector at the same points in 570*54feed06SJason Kingthe calling sequence as other implementations. 571*54feed06SJason KingThe aspects normally used by 572*54feed06SJason Kingthe caller (ordering after \-1 is returned, the value of 573*54feed06SJason King.Va optind 574*54feed06SJason Kingrelative 575*54feed06SJason Kingto current positions) are the same, though. 576*54feed06SJason King(We often do fewer variable swaps.) 577*54feed06SJason King.El 578*54feed06SJason King.Sh INTERFACE STABILITY 579*54feed06SJason KingCommitted 580*54feed06SJason King.Sh MT-LEVEL 581*54feed06SJason KingUnsafe 582*54feed06SJason King.Sh SEE ALSO 583*54feed06SJason King.Xr getopt 3C 584*54feed06SJason King.Sh BUGS 585*54feed06SJason KingThe 586*54feed06SJason King.Fa argv 587*54feed06SJason Kingargument is not really 588*54feed06SJason King.Vt const 589*54feed06SJason Kingas its elements may be permuted (unless 590*54feed06SJason King.Ev POSIXLY_CORRECT 591*54feed06SJason Kingis set). 592