1a35a7e76SEric Melville.\" $NetBSD: getopt_long.3,v 1.8 2002/06/03 12:01:43 wiz Exp $ 2a35a7e76SEric Melville.\" 3a35a7e76SEric Melville.\" Copyright (c) 1988, 1991, 1993 4a35a7e76SEric Melville.\" The Regents of the University of California. All rights reserved. 5a35a7e76SEric Melville.\" 6a35a7e76SEric Melville.\" Redistribution and use in source and binary forms, with or without 7a35a7e76SEric Melville.\" modification, are permitted provided that the following conditions 8a35a7e76SEric Melville.\" are met: 9a35a7e76SEric Melville.\" 1. Redistributions of source code must retain the above copyright 10a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer. 11a35a7e76SEric Melville.\" 2. Redistributions in binary form must reproduce the above copyright 12a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer in the 13a35a7e76SEric Melville.\" documentation and/or other materials provided with the distribution. 14a35a7e76SEric Melville.\" 3. All advertising materials mentioning features or use of this software 15a35a7e76SEric Melville.\" must display the following acknowledgement: 16a35a7e76SEric Melville.\" This product includes software developed by the University of 17a35a7e76SEric Melville.\" California, Berkeley and its contributors. 18a35a7e76SEric Melville.\" 4. Neither the name of the University nor the names of its contributors 19a35a7e76SEric Melville.\" may be used to endorse or promote products derived from this software 20a35a7e76SEric Melville.\" without specific prior written permission. 21a35a7e76SEric Melville.\" 22a35a7e76SEric Melville.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23a35a7e76SEric Melville.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24a35a7e76SEric Melville.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25a35a7e76SEric Melville.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26a35a7e76SEric Melville.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27a35a7e76SEric Melville.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28a35a7e76SEric Melville.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29a35a7e76SEric Melville.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30a35a7e76SEric Melville.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31a35a7e76SEric Melville.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32a35a7e76SEric Melville.\" SUCH DAMAGE. 33a35a7e76SEric Melville.\" 34a35a7e76SEric Melville.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 35187f61dfSRuslan Ermilov.\" $FreeBSD$ 36a35a7e76SEric Melville.\" 37a35a7e76SEric Melville.Dd April 1, 2000 38a35a7e76SEric Melville.Dt GETOPT_LONG 3 39a35a7e76SEric Melville.Os 40a35a7e76SEric Melville.Sh NAME 41a35a7e76SEric Melville.Nm getopt_long 42a35a7e76SEric Melville.Nd get long options from command line argument list 43a35a7e76SEric Melville.Sh LIBRARY 44a35a7e76SEric Melville.Lb libc 45a35a7e76SEric Melville.Sh SYNOPSIS 46187f61dfSRuslan Ermilov.In getopt.h 47a35a7e76SEric Melville.Ft int 48187f61dfSRuslan Ermilov.Fo getopt_long 49187f61dfSRuslan Ermilov.Fa "int argc" "char * const *argv" "const char *optstring" 50187f61dfSRuslan Ermilov.Fa "struct option *long options" "int *index" 51187f61dfSRuslan Ermilov.Fc 52a35a7e76SEric Melville.Sh DESCRIPTION 53a35a7e76SEric MelvilleThe 54a35a7e76SEric Melville.Fn getopt_long 55a35a7e76SEric Melvillefunction is similar to 56a35a7e76SEric Melville.Xr getopt 3 57187f61dfSRuslan Ermilovbut it accepts options in two forms: words and characters. 58187f61dfSRuslan ErmilovThe 59a35a7e76SEric Melville.Fn getopt_long 60187f61dfSRuslan Ermilovfunction provides a superset of the functionality of 61a35a7e76SEric Melville.Xr getopt 3 . 621fae73b1SRuslan ErmilovThe 63a35a7e76SEric Melville.Fn getopt_long 641fae73b1SRuslan Ermilovfunction 65187f61dfSRuslan Ermilovcan be used in two ways. 66187f61dfSRuslan ErmilovIn the first way, every long option understood 67a35a7e76SEric Melvilleby the program has a corresponding short option, and the option 68a35a7e76SEric Melvillestructure is only used to translate from long options to short 69187f61dfSRuslan Ermilovoptions. 70187f61dfSRuslan ErmilovWhen used in this fashion, 71a35a7e76SEric Melville.Fn getopt_long 72a35a7e76SEric Melvillebehaves identically to 73a35a7e76SEric Melville.Xr getopt 3 . 74a35a7e76SEric MelvilleThis is a good way to add long option processing to an existing program 75a35a7e76SEric Melvillewith the minimum of rewriting. 76a35a7e76SEric Melville.Pp 77a35a7e76SEric MelvilleIn the second mechanism, a long option sets a flag in the 78187f61dfSRuslan Ermilov.Vt option 79a35a7e76SEric Melvillestructure passed, or will store a pointer to the command line argument 80a35a7e76SEric Melvillein the 81187f61dfSRuslan Ermilov.Vt option 82187f61dfSRuslan Ermilovstructure passed to it for options that take arguments. 83187f61dfSRuslan ErmilovAdditionally, 84a35a7e76SEric Melvillethe long option's argument may be specified as a single argument with 85187f61dfSRuslan Ermilovan equal sign, e.g., 86a35a7e76SEric Melville.Pp 87187f61dfSRuslan Ermilov.Dl "myprogram --myoption=somevalue" 88187f61dfSRuslan Ermilov.Pp 89187f61dfSRuslan ErmilovWhen a long option is processed, the call to 90a35a7e76SEric Melville.Fn getopt_long 91187f61dfSRuslan Ermilovwill return 0. 92187f61dfSRuslan ErmilovFor this reason, long option processing without 93a35a7e76SEric Melvilleshortcuts is not backwards compatible with 94a35a7e76SEric Melville.Xr getopt 3 . 95a35a7e76SEric Melville.Pp 96a35a7e76SEric MelvilleIt is possible to combine these methods, providing for long options 97187f61dfSRuslan Ermilovprocessing with short option equivalents for some options. 98187f61dfSRuslan ErmilovLess 99a35a7e76SEric Melvillefrequently used options would be processed as long options only. 100a35a7e76SEric Melville.Pp 101a35a7e76SEric MelvilleThe 102a35a7e76SEric Melville.Fn getopt_long 103a35a7e76SEric Melvillecall requires a structure to be initialized describing the long 104187f61dfSRuslan Ermilovoptions. 105187f61dfSRuslan ErmilovThe structure is: 106187f61dfSRuslan Ermilov.Bd -literal -offset indent 107a35a7e76SEric Melvillestruct option { 108a35a7e76SEric Melville char *name; 109a35a7e76SEric Melville int has_arg; 110a35a7e76SEric Melville int *flag; 111a35a7e76SEric Melville int val; 112a35a7e76SEric Melville}; 113a35a7e76SEric Melville.Ed 114a35a7e76SEric Melville.Pp 115a35a7e76SEric MelvilleThe 116187f61dfSRuslan Ermilov.Va name 117a35a7e76SEric Melvillefield should contain the option name without the leading double dash. 118a35a7e76SEric Melville.Pp 119a35a7e76SEric MelvilleThe 120187f61dfSRuslan Ermilov.Va has_arg 121a35a7e76SEric Melvillefield should be one of: 122187f61dfSRuslan Ermilov.Pp 123187f61dfSRuslan Ermilov.Bl -tag -width ".Dv optional_argument" -offset indent -compact 124187f61dfSRuslan Ermilov.It Dv no_argument 125187f61dfSRuslan Ermilovno argument to the option is expect 126187f61dfSRuslan Ermilov.It Dv required_argument 127187f61dfSRuslan Ermilovan argument to the option is required 128a35a7e76SEric Melville.It Li optional_argument 129a35a7e76SEric Melvillean argument to the option may be presented. 130a35a7e76SEric Melville.El 131a35a7e76SEric Melville.Pp 132a35a7e76SEric MelvilleIf 133187f61dfSRuslan Ermilov.Va flag 134187f61dfSRuslan Ermilovis not 135187f61dfSRuslan Ermilov.Dv NULL , 136187f61dfSRuslan Ermilovthen the integer pointed to by it will be set to the 137a35a7e76SEric Melvillevalue in the 138187f61dfSRuslan Ermilov.Va val 139187f61dfSRuslan Ermilovfield. 140187f61dfSRuslan ErmilovIf the 141187f61dfSRuslan Ermilov.Va flag 142187f61dfSRuslan Ermilovfield is 143187f61dfSRuslan Ermilov.Dv NULL , 144187f61dfSRuslan Ermilovthen the 145187f61dfSRuslan Ermilov.Va val 146187f61dfSRuslan Ermilovfield will be returned. 147187f61dfSRuslan ErmilovSetting 148187f61dfSRuslan Ermilov.Va flag 149187f61dfSRuslan Ermilovto 150187f61dfSRuslan Ermilov.Dv NULL 151187f61dfSRuslan Ermilovand setting 152187f61dfSRuslan Ermilov.Va val 153a35a7e76SEric Melvilleto the corresponding short option will make this function act just 154a35a7e76SEric Melvillelike 155a35a7e76SEric Melville.Xr getopt 3 . 156a35a7e76SEric Melville.Sh EXAMPLES 157a35a7e76SEric Melville.Bd -literal -compact 158a35a7e76SEric Melvilleextern char *optarg; 159a35a7e76SEric Melvilleextern int optind; 160a35a7e76SEric Melvilleint bflag, ch, fd; 161a35a7e76SEric Melvilleint daggerset; 162a35a7e76SEric Melville 163a35a7e76SEric Melville/* options descriptor */ 164a35a7e76SEric Melvillestatic struct option longopts[] = { 165a35a7e76SEric Melville { "buffy", no_argument, 0, 'b' }, 166a35a7e76SEric Melville { "floride", required_argument, 0, 'f' }, 167a35a7e76SEric Melville { "daggerset", no_argument, \*[Am]daggerset, 1 }, 168a35a7e76SEric Melville { 0, 0, 0, 0 } 169a35a7e76SEric Melville}; 170a35a7e76SEric Melville 171a35a7e76SEric Melvillebflag = 0; 172a35a7e76SEric Melvillewhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) 173a35a7e76SEric Melville switch(ch) { 174a35a7e76SEric Melville case 'b': 175a35a7e76SEric Melville bflag = 1; 176a35a7e76SEric Melville break; 177a35a7e76SEric Melville case 'f': 178a35a7e76SEric Melville if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { 179a35a7e76SEric Melville (void)fprintf(stderr, 180a35a7e76SEric Melville "myname: %s: %s\en", optarg, strerror(errno)); 181a35a7e76SEric Melville exit(1); 182a35a7e76SEric Melville } 183a35a7e76SEric Melville break; 184a35a7e76SEric Melville case 0: 185a35a7e76SEric Melville if(daggerset) { 186a35a7e76SEric Melville fprintf(stderr,"Buffy will use her dagger to " 187a35a7e76SEric Melville "apply floride to dracula's teeth\en"); 188a35a7e76SEric Melville } 189a35a7e76SEric Melville break; 190a35a7e76SEric Melville case '?': 191a35a7e76SEric Melville default: 192a35a7e76SEric Melville usage(); 193a35a7e76SEric Melville} 194a35a7e76SEric Melvilleargc -= optind; 195a35a7e76SEric Melvilleargv += optind; 196a35a7e76SEric Melville.Ed 197a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES 198187f61dfSRuslan ErmilovThis section describes differences to the 199187f61dfSRuslan Ermilov.Tn GNU 200187f61dfSRuslan Ermilovimplementation 201a35a7e76SEric Melvillefound in glibc-2.1.3: 202187f61dfSRuslan Ermilov.Bl -bullet 203187f61dfSRuslan Ermilov.It 204187f61dfSRuslan ErmilovHandling of 205187f61dfSRuslan Ermilov.Ql - 206187f61dfSRuslan Ermilovas first char of option string in presence of 207187f61dfSRuslan Ermilovenvironment variable 208187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT : 209187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 210187f61dfSRuslan Ermilov.It Tn GNU 211187f61dfSRuslan Ermilovignores 212187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 213187f61dfSRuslan Ermilovand returns non-options as 214a35a7e76SEric Melvillearguments to option '\e1'. 215187f61dfSRuslan Ermilov.It Nx 216187f61dfSRuslan Ermilovhonors 217187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 218187f61dfSRuslan Ermilovand stops at the first non-option. 219a35a7e76SEric Melville.El 220187f61dfSRuslan Ermilov.It 221187f61dfSRuslan ErmilovHandling of 222187f61dfSRuslan Ermilov.Ql :: 223187f61dfSRuslan Ermilovin options string in presence of 224187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT : 225187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 226187f61dfSRuslan Ermilov.It Both 227187f61dfSRuslan Ermilov.Tn GNU 228187f61dfSRuslan Ermilovand 229187f61dfSRuslan Ermilov.Nx 230187f61dfSRuslan Ermilovignore 231187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 232187f61dfSRuslan Ermilovhere and take 233187f61dfSRuslan Ermilov.Ql :: 234187f61dfSRuslan Ermilovto 235a35a7e76SEric Melvillemean the preceding option takes an optional argument. 236a35a7e76SEric Melville.El 237187f61dfSRuslan Ermilov.It 238187f61dfSRuslan ErmilovReturn value in case of missing argument if first character 239187f61dfSRuslan Ermilov(after 240187f61dfSRuslan Ermilov.Ql + 241187f61dfSRuslan Ermilovor 242187f61dfSRuslan Ermilov.Ql - ) 243187f61dfSRuslan Ermilovin option string is not 244187f61dfSRuslan Ermilov.Ql \&: : 245187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 246187f61dfSRuslan Ermilov.It Tn GNU 247187f61dfSRuslan Ermilovreturns 248187f61dfSRuslan Ermilov.Ql \&? 249187f61dfSRuslan Ermilov.It Nx 250187f61dfSRuslan Ermilovreturns 251187f61dfSRuslan Ermilov.Ql \&: 252187f61dfSRuslan Ermilov(since 253187f61dfSRuslan Ermilov.Nx Ns 's 254187f61dfSRuslan Ermilov.Fn getopt 255187f61dfSRuslan Ermilovdoes). 256a35a7e76SEric Melville.El 257187f61dfSRuslan Ermilov.It 258187f61dfSRuslan ErmilovHandling of 259187f61dfSRuslan Ermilov.Ql --a 260187f61dfSRuslan Ermilovin getopt: 261187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 262187f61dfSRuslan Ermilov.It Tn GNU 263187f61dfSRuslan Ermilovparses this as option 264187f61dfSRuslan Ermilov.Ql - , 265187f61dfSRuslan Ermilovoption 266187f61dfSRuslan Ermilov.Ql a . 267187f61dfSRuslan Ermilov.It Nx 268187f61dfSRuslan Ermilovparses this as 269187f61dfSRuslan Ermilov.Ql -- , 270187f61dfSRuslan Ermilovand returns \-1 (ignoring the 271187f61dfSRuslan Ermilov.Ql a ) . 272187f61dfSRuslan Ermilov(Because the original 273187f61dfSRuslan Ermilov.Fn getopt 274187f61dfSRuslan Ermilovdoes.) 275a35a7e76SEric Melville.El 276187f61dfSRuslan Ermilov.It 277187f61dfSRuslan ErmilovSetting of 278187f61dfSRuslan Ermilov.Va optopt 279187f61dfSRuslan Ermilovfor long options with 280187f61dfSRuslan Ermilov.Va flag 281187f61dfSRuslan Ermilov!= 282187f61dfSRuslan Ermilov.Dv NULL : 283187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 284187f61dfSRuslan Ermilov.It Tn GNU 285187f61dfSRuslan Ermilovsets 286187f61dfSRuslan Ermilov.Va optopt 287187f61dfSRuslan Ermilovto 288187f61dfSRuslan Ermilov.Va val . 289187f61dfSRuslan Ermilov.It Nx 290187f61dfSRuslan Ermilovsets 291187f61dfSRuslan Ermilov.Va optopt 292187f61dfSRuslan Ermilovto 0 (since 293187f61dfSRuslan Ermilov.Va val 294187f61dfSRuslan Ermilovwould never be returned). 295a35a7e76SEric Melville.El 296187f61dfSRuslan Ermilov.It 297187f61dfSRuslan ErmilovHandling of 298187f61dfSRuslan Ermilov.Ql -W 299187f61dfSRuslan Ermilovwith 300187f61dfSRuslan Ermilov.Ql W ; 301187f61dfSRuslan Ermilovin option string in 302187f61dfSRuslan Ermilov.Fn getopt 303187f61dfSRuslan Ermilov(not 304187f61dfSRuslan Ermilov.Fn getopt_long ) : 305187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 306187f61dfSRuslan Ermilov.It Tn GNU 307a35a7e76SEric Melvillecauses a segfault. 308187f61dfSRuslan Ermilov.It Nx 309187f61dfSRuslan Ermilovreturns \-1, with 310187f61dfSRuslan Ermilov.Va optind 311187f61dfSRuslan Ermilovpointing past the argument of 312187f61dfSRuslan Ermilov.Ql -W 313187f61dfSRuslan Ermilov(as if 314187f61dfSRuslan Ermilov.Ql "-W arg" 315187f61dfSRuslan Ermilovwere 316187f61dfSRuslan Ermilov.Ql --arg , 317187f61dfSRuslan Ermilovand thus 318187f61dfSRuslan Ermilov.Ql -- 319187f61dfSRuslan Ermilovhad been found). 320a35a7e76SEric Melville.\" How should we treat W; in the option string when called via 321a35a7e76SEric Melville.\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning? 322a35a7e76SEric Melville.El 323187f61dfSRuslan Ermilov.It 324187f61dfSRuslan ErmilovSetting of 325187f61dfSRuslan Ermilov.Va optarg 326187f61dfSRuslan Ermilovfor long options without an argument that are 327187f61dfSRuslan Ermilovinvoked via 328187f61dfSRuslan Ermilov.Ql -W 329187f61dfSRuslan Ermilov.Ql ( W ; 330187f61dfSRuslan Ermilovin option string): 331187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 332187f61dfSRuslan Ermilov.It Tn GNU 333187f61dfSRuslan Ermilovsets 334187f61dfSRuslan Ermilov.Va optarg 335187f61dfSRuslan Ermilovto the option name (the argument of 336187f61dfSRuslan Ermilov.Ql -W ) . 337187f61dfSRuslan Ermilov.It Nx 338187f61dfSRuslan Ermilovsets 339187f61dfSRuslan Ermilov.Va optarg 340187f61dfSRuslan Ermilovto 341187f61dfSRuslan Ermilov.Dv NULL 342187f61dfSRuslan Ermilov(the argument of the long option). 343a35a7e76SEric Melville.El 344187f61dfSRuslan Ermilov.It 345187f61dfSRuslan ErmilovHandling of 346187f61dfSRuslan Ermilov.Ql -W 347187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known 348187f61dfSRuslan Ermilovlong option 349187f61dfSRuslan Ermilov.Ql ( W ; 350187f61dfSRuslan Ermilovin option string): 351187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 352187f61dfSRuslan Ermilov.It Tn GNU 353187f61dfSRuslan Ermilovreturns 354187f61dfSRuslan Ermilov.Ql -W 355187f61dfSRuslan Ermilovwith 356187f61dfSRuslan Ermilov.Va optarg 357187f61dfSRuslan Ermilovset to the unknown option. 358187f61dfSRuslan Ermilov.It Nx 359187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns 360187f61dfSRuslan Ermilov.Ql \&? 361187f61dfSRuslan Ermilovwith 362187f61dfSRuslan Ermilov.Va optopt 363187f61dfSRuslan Ermilovset to 0 and 364187f61dfSRuslan Ermilov.Va optarg 365187f61dfSRuslan Ermilovset to 366187f61dfSRuslan Ermilov.Dv NULL 367187f61dfSRuslan Ermilov(as 368187f61dfSRuslan Ermilov.Tn GNU Ns 's 369187f61dfSRuslan Ermilovman page documents). 370a35a7e76SEric Melville.El 371187f61dfSRuslan Ermilov.It 372a35a7e76SEric MelvilleThe error messages are different. 373187f61dfSRuslan Ermilov.It 374187f61dfSRuslan Ermilov.Nx 375187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in 376187f61dfSRuslan Ermilovthe calling sequence as 377187f61dfSRuslan Ermilov.Tn GNU 378187f61dfSRuslan Ermilovdoes. 379187f61dfSRuslan ErmilovThe aspects normally used by 380187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of 381187f61dfSRuslan Ermilov.Va optind 382187f61dfSRuslan Ermilovrelative 383187f61dfSRuslan Ermilovto current positions) are the same, though. 384187f61dfSRuslan Ermilov(We do fewer variable swaps.) 385a35a7e76SEric Melville.El 386a35a7e76SEric Melville.Sh SEE ALSO 387a35a7e76SEric Melville.Xr getopt 3 388a35a7e76SEric Melville.Sh HISTORY 389a35a7e76SEric MelvilleThe 390a35a7e76SEric Melville.Fn getopt_long 391187f61dfSRuslan Ermilovfunction first appeared in 392187f61dfSRuslan Ermilov.Tn GNU 393187f61dfSRuslan Ermilovlibiberty. 394187f61dfSRuslan ErmilovThe first 395a35a7e76SEric Melville.Nx 396a35a7e76SEric Melvilleimplementation appeared in 1.5. 397a35a7e76SEric Melville.Sh BUGS 398187f61dfSRuslan ErmilovThe implementation can completely replace 399a35a7e76SEric Melville.Xr getopt 3 , 400a35a7e76SEric Melvillebut right now we are using separate code. 401