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 4775207c7aSAndrey A. Chernov.Vt extern char *optarg ; 4875207c7aSAndrey A. Chernov.Vt extern int optind ; 4975207c7aSAndrey A. Chernov.Vt extern int optopt ; 5075207c7aSAndrey A. Chernov.Vt extern int opterr ; 5175207c7aSAndrey A. Chernov.Vt extern int optreset ; 52a35a7e76SEric Melville.Ft int 53187f61dfSRuslan Ermilov.Fo getopt_long 54187f61dfSRuslan Ermilov.Fa "int argc" "char * const *argv" "const char *optstring" 55187f61dfSRuslan Ermilov.Fa "struct option *long options" "int *index" 56187f61dfSRuslan Ermilov.Fc 57a35a7e76SEric Melville.Sh DESCRIPTION 58a35a7e76SEric MelvilleThe 59a35a7e76SEric Melville.Fn getopt_long 60a35a7e76SEric Melvillefunction is similar to 61a35a7e76SEric Melville.Xr getopt 3 62187f61dfSRuslan Ermilovbut it accepts options in two forms: words and characters. 63187f61dfSRuslan ErmilovThe 64a35a7e76SEric Melville.Fn getopt_long 65187f61dfSRuslan Ermilovfunction provides a superset of the functionality of 66a35a7e76SEric Melville.Xr getopt 3 . 671fae73b1SRuslan ErmilovThe 68a35a7e76SEric Melville.Fn getopt_long 691fae73b1SRuslan Ermilovfunction 70187f61dfSRuslan Ermilovcan be used in two ways. 71187f61dfSRuslan ErmilovIn the first way, every long option understood 72a35a7e76SEric Melvilleby the program has a corresponding short option, and the option 73a35a7e76SEric Melvillestructure is only used to translate from long options to short 74187f61dfSRuslan Ermilovoptions. 75187f61dfSRuslan ErmilovWhen used in this fashion, 76a35a7e76SEric Melville.Fn getopt_long 77a35a7e76SEric Melvillebehaves identically to 78a35a7e76SEric Melville.Xr getopt 3 . 79a35a7e76SEric MelvilleThis is a good way to add long option processing to an existing program 80a35a7e76SEric Melvillewith the minimum of rewriting. 81a35a7e76SEric Melville.Pp 82a35a7e76SEric MelvilleIn the second mechanism, a long option sets a flag in the 83187f61dfSRuslan Ermilov.Vt option 84a35a7e76SEric Melvillestructure passed, or will store a pointer to the command line argument 85a35a7e76SEric Melvillein the 86187f61dfSRuslan Ermilov.Vt option 87187f61dfSRuslan Ermilovstructure passed to it for options that take arguments. 88187f61dfSRuslan ErmilovAdditionally, 89a35a7e76SEric Melvillethe long option's argument may be specified as a single argument with 90187f61dfSRuslan Ermilovan equal sign, e.g., 91a35a7e76SEric Melville.Pp 92187f61dfSRuslan Ermilov.Dl "myprogram --myoption=somevalue" 93187f61dfSRuslan Ermilov.Pp 94187f61dfSRuslan ErmilovWhen a long option is processed, the call to 95a35a7e76SEric Melville.Fn getopt_long 96187f61dfSRuslan Ermilovwill return 0. 97187f61dfSRuslan ErmilovFor this reason, long option processing without 98a35a7e76SEric Melvilleshortcuts is not backwards compatible with 99a35a7e76SEric Melville.Xr getopt 3 . 100a35a7e76SEric Melville.Pp 101a35a7e76SEric MelvilleIt is possible to combine these methods, providing for long options 102187f61dfSRuslan Ermilovprocessing with short option equivalents for some options. 103187f61dfSRuslan ErmilovLess 104a35a7e76SEric Melvillefrequently used options would be processed as long options only. 105a35a7e76SEric Melville.Pp 106a35a7e76SEric MelvilleThe 107a35a7e76SEric Melville.Fn getopt_long 108a35a7e76SEric Melvillecall requires a structure to be initialized describing the long 109187f61dfSRuslan Ermilovoptions. 110187f61dfSRuslan ErmilovThe structure is: 111187f61dfSRuslan Ermilov.Bd -literal -offset indent 112a35a7e76SEric Melvillestruct option { 113a35a7e76SEric Melville char *name; 114a35a7e76SEric Melville int has_arg; 115a35a7e76SEric Melville int *flag; 116a35a7e76SEric Melville int val; 117a35a7e76SEric Melville}; 118a35a7e76SEric Melville.Ed 119a35a7e76SEric Melville.Pp 120a35a7e76SEric MelvilleThe 121187f61dfSRuslan Ermilov.Va name 122a35a7e76SEric Melvillefield should contain the option name without the leading double dash. 123a35a7e76SEric Melville.Pp 124a35a7e76SEric MelvilleThe 125187f61dfSRuslan Ermilov.Va has_arg 126a35a7e76SEric Melvillefield should be one of: 127187f61dfSRuslan Ermilov.Pp 128187f61dfSRuslan Ermilov.Bl -tag -width ".Dv optional_argument" -offset indent -compact 129187f61dfSRuslan Ermilov.It Dv no_argument 130187f61dfSRuslan Ermilovno argument to the option is expect 131187f61dfSRuslan Ermilov.It Dv required_argument 132187f61dfSRuslan Ermilovan argument to the option is required 133a35a7e76SEric Melville.It Li optional_argument 134a35a7e76SEric Melvillean argument to the option may be presented. 135a35a7e76SEric Melville.El 136a35a7e76SEric Melville.Pp 137a35a7e76SEric MelvilleIf 138187f61dfSRuslan Ermilov.Va flag 139187f61dfSRuslan Ermilovis not 140187f61dfSRuslan Ermilov.Dv NULL , 141187f61dfSRuslan Ermilovthen the integer pointed to by it will be set to the 142a35a7e76SEric Melvillevalue in the 143187f61dfSRuslan Ermilov.Va val 144187f61dfSRuslan Ermilovfield. 145187f61dfSRuslan ErmilovIf the 146187f61dfSRuslan Ermilov.Va flag 147187f61dfSRuslan Ermilovfield is 148187f61dfSRuslan Ermilov.Dv NULL , 149187f61dfSRuslan Ermilovthen the 150187f61dfSRuslan Ermilov.Va val 151187f61dfSRuslan Ermilovfield will be returned. 152187f61dfSRuslan ErmilovSetting 153187f61dfSRuslan Ermilov.Va flag 154187f61dfSRuslan Ermilovto 155187f61dfSRuslan Ermilov.Dv NULL 156187f61dfSRuslan Ermilovand setting 157187f61dfSRuslan Ermilov.Va val 158a35a7e76SEric Melvilleto the corresponding short option will make this function act just 159a35a7e76SEric Melvillelike 160a35a7e76SEric Melville.Xr getopt 3 . 161a35a7e76SEric Melville.Sh EXAMPLES 162a35a7e76SEric Melville.Bd -literal -compact 163a35a7e76SEric Melvilleextern char *optarg; 164a35a7e76SEric Melvilleextern int optind; 165a35a7e76SEric Melvilleint bflag, ch, fd; 166a35a7e76SEric Melvilleint daggerset; 167a35a7e76SEric Melville 168a35a7e76SEric Melville/* options descriptor */ 169a35a7e76SEric Melvillestatic struct option longopts[] = { 170a35a7e76SEric Melville { "buffy", no_argument, 0, 'b' }, 171a35a7e76SEric Melville { "floride", required_argument, 0, 'f' }, 172a35a7e76SEric Melville { "daggerset", no_argument, \*[Am]daggerset, 1 }, 173a35a7e76SEric Melville { 0, 0, 0, 0 } 174a35a7e76SEric Melville}; 175a35a7e76SEric Melville 176a35a7e76SEric Melvillebflag = 0; 177a35a7e76SEric Melvillewhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) 178a35a7e76SEric Melville switch(ch) { 179a35a7e76SEric Melville case 'b': 180a35a7e76SEric Melville bflag = 1; 181a35a7e76SEric Melville break; 182a35a7e76SEric Melville case 'f': 183a35a7e76SEric Melville if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) { 184a35a7e76SEric Melville (void)fprintf(stderr, 185a35a7e76SEric Melville "myname: %s: %s\en", optarg, strerror(errno)); 186a35a7e76SEric Melville exit(1); 187a35a7e76SEric Melville } 188a35a7e76SEric Melville break; 189a35a7e76SEric Melville case 0: 190a35a7e76SEric Melville if(daggerset) { 191a35a7e76SEric Melville fprintf(stderr,"Buffy will use her dagger to " 192a35a7e76SEric Melville "apply floride to dracula's teeth\en"); 193a35a7e76SEric Melville } 194a35a7e76SEric Melville break; 195a35a7e76SEric Melville case '?': 196a35a7e76SEric Melville default: 197a35a7e76SEric Melville usage(); 198a35a7e76SEric Melville} 199a35a7e76SEric Melvilleargc -= optind; 200a35a7e76SEric Melvilleargv += optind; 201a35a7e76SEric Melville.Ed 202a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES 203187f61dfSRuslan ErmilovThis section describes differences to the 204187f61dfSRuslan Ermilov.Tn GNU 205187f61dfSRuslan Ermilovimplementation 206a35a7e76SEric Melvillefound in glibc-2.1.3: 207187f61dfSRuslan Ermilov.Bl -bullet 208187f61dfSRuslan Ermilov.It 209187f61dfSRuslan ErmilovHandling of 210187f61dfSRuslan Ermilov.Ql - 211187f61dfSRuslan Ermilovas first char of option string in presence of 212187f61dfSRuslan Ermilovenvironment variable 213187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT : 214187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 215187f61dfSRuslan Ermilov.It Tn GNU 216187f61dfSRuslan Ermilovignores 217187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 218187f61dfSRuslan Ermilovand returns non-options as 219a35a7e76SEric Melvillearguments to option '\e1'. 220187f61dfSRuslan Ermilov.It Nx 221187f61dfSRuslan Ermilovhonors 222187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 223187f61dfSRuslan Ermilovand stops at the first non-option. 224a35a7e76SEric Melville.El 225187f61dfSRuslan Ermilov.It 226187f61dfSRuslan ErmilovHandling of 227187f61dfSRuslan Ermilov.Ql :: 228187f61dfSRuslan Ermilovin options string in presence of 229187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT : 230187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 231187f61dfSRuslan Ermilov.It Both 232187f61dfSRuslan Ermilov.Tn GNU 233187f61dfSRuslan Ermilovand 234187f61dfSRuslan Ermilov.Nx 235187f61dfSRuslan Ermilovignore 236187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT 237187f61dfSRuslan Ermilovhere and take 238187f61dfSRuslan Ermilov.Ql :: 239187f61dfSRuslan Ermilovto 240a35a7e76SEric Melvillemean the preceding option takes an optional argument. 241a35a7e76SEric Melville.El 242187f61dfSRuslan Ermilov.It 243187f61dfSRuslan ErmilovReturn value in case of missing argument if first character 244187f61dfSRuslan Ermilov(after 245187f61dfSRuslan Ermilov.Ql + 246187f61dfSRuslan Ermilovor 247187f61dfSRuslan Ermilov.Ql - ) 248187f61dfSRuslan Ermilovin option string is not 249187f61dfSRuslan Ermilov.Ql \&: : 250187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 251187f61dfSRuslan Ermilov.It Tn GNU 252187f61dfSRuslan Ermilovreturns 253187f61dfSRuslan Ermilov.Ql \&? 254187f61dfSRuslan Ermilov.It Nx 255187f61dfSRuslan Ermilovreturns 256187f61dfSRuslan Ermilov.Ql \&: 257187f61dfSRuslan Ermilov(since 258187f61dfSRuslan Ermilov.Nx Ns 's 259187f61dfSRuslan Ermilov.Fn getopt 260187f61dfSRuslan Ermilovdoes). 261a35a7e76SEric Melville.El 262187f61dfSRuslan Ermilov.It 263187f61dfSRuslan ErmilovHandling of 264187f61dfSRuslan Ermilov.Ql --a 265187f61dfSRuslan Ermilovin getopt: 266187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 267187f61dfSRuslan Ermilov.It Tn GNU 268187f61dfSRuslan Ermilovparses this as option 269187f61dfSRuslan Ermilov.Ql - , 270187f61dfSRuslan Ermilovoption 271187f61dfSRuslan Ermilov.Ql a . 272187f61dfSRuslan Ermilov.It Nx 273187f61dfSRuslan Ermilovparses this as 274187f61dfSRuslan Ermilov.Ql -- , 275187f61dfSRuslan Ermilovand returns \-1 (ignoring the 276187f61dfSRuslan Ermilov.Ql a ) . 277187f61dfSRuslan Ermilov(Because the original 278187f61dfSRuslan Ermilov.Fn getopt 279187f61dfSRuslan Ermilovdoes.) 280a35a7e76SEric Melville.El 281187f61dfSRuslan Ermilov.It 282187f61dfSRuslan ErmilovSetting of 283187f61dfSRuslan Ermilov.Va optopt 284187f61dfSRuslan Ermilovfor long options with 285187f61dfSRuslan Ermilov.Va flag 286187f61dfSRuslan Ermilov!= 287187f61dfSRuslan Ermilov.Dv NULL : 288187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 289187f61dfSRuslan Ermilov.It Tn GNU 290187f61dfSRuslan Ermilovsets 291187f61dfSRuslan Ermilov.Va optopt 292187f61dfSRuslan Ermilovto 293187f61dfSRuslan Ermilov.Va val . 294187f61dfSRuslan Ermilov.It Nx 295187f61dfSRuslan Ermilovsets 296187f61dfSRuslan Ermilov.Va optopt 297187f61dfSRuslan Ermilovto 0 (since 298187f61dfSRuslan Ermilov.Va val 299187f61dfSRuslan Ermilovwould never be returned). 300a35a7e76SEric Melville.El 301187f61dfSRuslan Ermilov.It 302187f61dfSRuslan ErmilovHandling of 303187f61dfSRuslan Ermilov.Ql -W 304187f61dfSRuslan Ermilovwith 305187f61dfSRuslan Ermilov.Ql W ; 306187f61dfSRuslan Ermilovin option string in 307187f61dfSRuslan Ermilov.Fn getopt 308187f61dfSRuslan Ermilov(not 309187f61dfSRuslan Ermilov.Fn getopt_long ) : 310187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 311187f61dfSRuslan Ermilov.It Tn GNU 312a35a7e76SEric Melvillecauses a segfault. 313187f61dfSRuslan Ermilov.It Nx 314187f61dfSRuslan Ermilovreturns \-1, with 315187f61dfSRuslan Ermilov.Va optind 316187f61dfSRuslan Ermilovpointing past the argument of 317187f61dfSRuslan Ermilov.Ql -W 318187f61dfSRuslan Ermilov(as if 319187f61dfSRuslan Ermilov.Ql "-W arg" 320187f61dfSRuslan Ermilovwere 321187f61dfSRuslan Ermilov.Ql --arg , 322187f61dfSRuslan Ermilovand thus 323187f61dfSRuslan Ermilov.Ql -- 324187f61dfSRuslan Ermilovhad been found). 325a35a7e76SEric Melville.\" How should we treat W; in the option string when called via 326a35a7e76SEric Melville.\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning? 327a35a7e76SEric Melville.El 328187f61dfSRuslan Ermilov.It 329187f61dfSRuslan ErmilovSetting of 330187f61dfSRuslan Ermilov.Va optarg 331187f61dfSRuslan Ermilovfor long options without an argument that are 332187f61dfSRuslan Ermilovinvoked via 333187f61dfSRuslan Ermilov.Ql -W 334187f61dfSRuslan Ermilov.Ql ( W ; 335187f61dfSRuslan Ermilovin option string): 336187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 337187f61dfSRuslan Ermilov.It Tn GNU 338187f61dfSRuslan Ermilovsets 339187f61dfSRuslan Ermilov.Va optarg 340187f61dfSRuslan Ermilovto the option name (the argument of 341187f61dfSRuslan Ermilov.Ql -W ) . 342187f61dfSRuslan Ermilov.It Nx 343187f61dfSRuslan Ermilovsets 344187f61dfSRuslan Ermilov.Va optarg 345187f61dfSRuslan Ermilovto 346187f61dfSRuslan Ermilov.Dv NULL 347187f61dfSRuslan Ermilov(the argument of the long option). 348a35a7e76SEric Melville.El 349187f61dfSRuslan Ermilov.It 350187f61dfSRuslan ErmilovHandling of 351187f61dfSRuslan Ermilov.Ql -W 352187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known 353187f61dfSRuslan Ermilovlong option 354187f61dfSRuslan Ermilov.Ql ( W ; 355187f61dfSRuslan Ermilovin option string): 356187f61dfSRuslan Ermilov.Bl -tag -width ".Nx" 357187f61dfSRuslan Ermilov.It Tn GNU 358187f61dfSRuslan Ermilovreturns 359187f61dfSRuslan Ermilov.Ql -W 360187f61dfSRuslan Ermilovwith 361187f61dfSRuslan Ermilov.Va optarg 362187f61dfSRuslan Ermilovset to the unknown option. 363187f61dfSRuslan Ermilov.It Nx 364187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns 365187f61dfSRuslan Ermilov.Ql \&? 366187f61dfSRuslan Ermilovwith 367187f61dfSRuslan Ermilov.Va optopt 368187f61dfSRuslan Ermilovset to 0 and 369187f61dfSRuslan Ermilov.Va optarg 370187f61dfSRuslan Ermilovset to 371187f61dfSRuslan Ermilov.Dv NULL 372187f61dfSRuslan Ermilov(as 373187f61dfSRuslan Ermilov.Tn GNU Ns 's 374187f61dfSRuslan Ermilovman page documents). 375a35a7e76SEric Melville.El 376187f61dfSRuslan Ermilov.It 377a35a7e76SEric MelvilleThe error messages are different. 378187f61dfSRuslan Ermilov.It 379187f61dfSRuslan Ermilov.Nx 380187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in 381187f61dfSRuslan Ermilovthe calling sequence as 382187f61dfSRuslan Ermilov.Tn GNU 383187f61dfSRuslan Ermilovdoes. 384187f61dfSRuslan ErmilovThe aspects normally used by 385187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of 386187f61dfSRuslan Ermilov.Va optind 387187f61dfSRuslan Ermilovrelative 388187f61dfSRuslan Ermilovto current positions) are the same, though. 389187f61dfSRuslan Ermilov(We do fewer variable swaps.) 390a35a7e76SEric Melville.El 391a35a7e76SEric Melville.Sh SEE ALSO 392a35a7e76SEric Melville.Xr getopt 3 393a35a7e76SEric Melville.Sh HISTORY 394a35a7e76SEric MelvilleThe 395a35a7e76SEric Melville.Fn getopt_long 396187f61dfSRuslan Ermilovfunction first appeared in 397187f61dfSRuslan Ermilov.Tn GNU 398187f61dfSRuslan Ermilovlibiberty. 399187f61dfSRuslan ErmilovThe first 400a35a7e76SEric Melville.Nx 401a35a7e76SEric Melvilleimplementation appeared in 1.5. 402a35a7e76SEric Melville.Sh BUGS 403187f61dfSRuslan ErmilovThe implementation can completely replace 404a35a7e76SEric Melville.Xr getopt 3 , 405a35a7e76SEric Melvillebut right now we are using separate code. 406