1829a229dSAndrey A. Chernov.\" $OpenBSD: getopt_long.3,v 1.10 2004/01/06 23:44:28 fgsch Exp $ 2829a229dSAndrey A. Chernov.\" $NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc Exp $ 3a35a7e76SEric Melville.\" 4a35a7e76SEric Melville.\" Copyright (c) 1988, 1991, 1993 5a35a7e76SEric Melville.\" The Regents of the University of California. All rights reserved. 6a35a7e76SEric Melville.\" 7a35a7e76SEric Melville.\" Redistribution and use in source and binary forms, with or without 8a35a7e76SEric Melville.\" modification, are permitted provided that the following conditions 9a35a7e76SEric Melville.\" are met: 10a35a7e76SEric Melville.\" 1. Redistributions of source code must retain the above copyright 11a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer. 12a35a7e76SEric Melville.\" 2. Redistributions in binary form must reproduce the above copyright 13a35a7e76SEric Melville.\" notice, this list of conditions and the following disclaimer in the 14a35a7e76SEric Melville.\" documentation and/or other materials provided with the distribution. 15829a229dSAndrey A. Chernov.\" 3. Neither the name of the University nor the names of its contributors 16a35a7e76SEric Melville.\" may be used to endorse or promote products derived from this software 17a35a7e76SEric Melville.\" without specific prior written permission. 18a35a7e76SEric Melville.\" 19a35a7e76SEric Melville.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20a35a7e76SEric Melville.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21a35a7e76SEric Melville.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22a35a7e76SEric Melville.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23a35a7e76SEric Melville.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24a35a7e76SEric Melville.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25a35a7e76SEric Melville.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26a35a7e76SEric Melville.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27a35a7e76SEric Melville.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28a35a7e76SEric Melville.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29a35a7e76SEric Melville.\" SUCH DAMAGE. 30a35a7e76SEric Melville.\" 31*d646513eSKyle Evans.Dd December 24, 2022 32a35a7e76SEric Melville.Dt GETOPT_LONG 3 33a35a7e76SEric Melville.Os 34a35a7e76SEric Melville.Sh NAME 35829a229dSAndrey A. Chernov.Nm getopt_long , 36829a229dSAndrey A. Chernov.Nm getopt_long_only 37a35a7e76SEric Melville.Nd get long options from command line argument list 38a35a7e76SEric Melville.Sh LIBRARY 39a35a7e76SEric Melville.Lb libc 40a35a7e76SEric Melville.Sh SYNOPSIS 41187f61dfSRuslan Ermilov.In getopt.h 4275207c7aSAndrey A. Chernov.Vt extern char *optarg ; 4375207c7aSAndrey A. Chernov.Vt extern int optind ; 4475207c7aSAndrey A. Chernov.Vt extern int optopt ; 4575207c7aSAndrey A. Chernov.Vt extern int opterr ; 4675207c7aSAndrey A. Chernov.Vt extern int optreset ; 47a35a7e76SEric Melville.Ft int 48187f61dfSRuslan Ermilov.Fo getopt_long 49187f61dfSRuslan Ermilov.Fa "int argc" "char * const *argv" "const char *optstring" 50829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex" 51829a229dSAndrey A. Chernov.Fc 52829a229dSAndrey A. Chernov.Ft int 53829a229dSAndrey A. Chernov.Fo getopt_long_only 54829a229dSAndrey A. Chernov.Fa "int argc" "char * const *argv" "const char *optstring" 55829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex" 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 1300603b75aSEitan Adlerno argument to the option is expected 131187f61dfSRuslan Ermilov.It Dv required_argument 132187f61dfSRuslan Ermilovan argument to the option is required 133629a7369SRuslan Ermilov.It Dv optional_argument 1340603b75aSEitan Adleran 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 . 161829a229dSAndrey A. Chernov.Pp 162829a229dSAndrey A. ChernovIf the 163829a229dSAndrey A. Chernov.Fa longindex 164829a229dSAndrey A. Chernovfield is not 165829a229dSAndrey A. Chernov.Dv NULL , 166829a229dSAndrey A. Chernovthen the integer pointed to by it will be set to the index of the long 167829a229dSAndrey A. Chernovoption relative to 168829a229dSAndrey A. Chernov.Fa longopts . 169829a229dSAndrey A. Chernov.Pp 170829a229dSAndrey A. ChernovThe last element of the 171829a229dSAndrey A. Chernov.Fa longopts 172829a229dSAndrey A. Chernovarray has to be filled with zeroes. 173829a229dSAndrey A. Chernov.Pp 174829a229dSAndrey A. ChernovThe 175829a229dSAndrey A. Chernov.Fn getopt_long_only 176829a229dSAndrey A. Chernovfunction behaves identically to 177829a229dSAndrey A. Chernov.Fn getopt_long 178829a229dSAndrey A. Chernovwith the exception that long options may start with 179829a229dSAndrey A. Chernov.Ql - 180829a229dSAndrey A. Chernovin addition to 181829a229dSAndrey A. Chernov.Ql -- . 182829a229dSAndrey A. ChernovIf an option starting with 183829a229dSAndrey A. Chernov.Ql - 184829a229dSAndrey A. Chernovdoes not match a long option but does match a single-character option, 185829a229dSAndrey A. Chernovthe single-character option is returned. 186829a229dSAndrey A. Chernov.Sh RETURN VALUES 187829a229dSAndrey A. ChernovIf the 188829a229dSAndrey A. Chernov.Fa flag 189829a229dSAndrey A. Chernovfield in 190629a7369SRuslan Ermilov.Vt "struct option" 191829a229dSAndrey A. Chernovis 192829a229dSAndrey A. Chernov.Dv NULL , 193829a229dSAndrey A. Chernov.Fn getopt_long 194829a229dSAndrey A. Chernovand 195829a229dSAndrey A. Chernov.Fn getopt_long_only 196829a229dSAndrey A. Chernovreturn the value specified in the 197829a229dSAndrey A. Chernov.Fa val 198829a229dSAndrey A. Chernovfield, which is usually just the corresponding short option. 199829a229dSAndrey A. ChernovIf 200829a229dSAndrey A. Chernov.Fa flag 201829a229dSAndrey A. Chernovis not 202829a229dSAndrey A. Chernov.Dv NULL , 203829a229dSAndrey A. Chernovthese functions return 0 and store 204829a229dSAndrey A. Chernov.Fa val 205829a229dSAndrey A. Chernovin the location pointed to by 206829a229dSAndrey A. Chernov.Fa flag . 2073b890189SKyle Evans.Pp 208829a229dSAndrey A. ChernovThese functions return 209829a229dSAndrey A. Chernov.Ql \&: 2103b890189SKyle Evansif there was a missing option argument and error messages are suppressed, 211829a229dSAndrey A. Chernov.Ql \&? 212829a229dSAndrey A. Chernovif the user specified an unknown or ambiguous option, and 213829a229dSAndrey A. Chernov\-1 when the argument list has been exhausted. 2143b890189SKyle EvansThe default behavior when a missing option argument is encountered is to write 2153b890189SKyle Evansan error and return 2163b890189SKyle Evans.Ql \&? . 2173b890189SKyle EvansSpecifying 2183b890189SKyle Evans.Ql \&: 2193b890189SKyle Evansin 2203b890189SKyle Evans.Fa optstr 2213b890189SKyle Evanswill cause the error message to be suppressed and 2223b890189SKyle Evans.Ql \&: 2233b890189SKyle Evansto be returned instead. 2243b890189SKyle Evans.Pp 2253b890189SKyle EvansIn addition to 2263b890189SKyle Evans.Ql \&: , 2273b890189SKyle Evansa leading 2283b890189SKyle Evans.Ql \&+ 2293b890189SKyle Evansor 2303b890189SKyle Evans.Ql \&- 2313b890189SKyle Evansin 2323b890189SKyle Evans.Fa optstr 2333b890189SKyle Evansalso has special meaning. 2343b890189SKyle EvansIf either of these are specified, they must appear before 2353b890189SKyle Evans.Ql \&: . 2363b890189SKyle Evans.Pp 2373b890189SKyle EvansA leading 2383b890189SKyle Evans.Ql \&+ 2393b890189SKyle Evansindicates that processing should be halted at the first non-option argument, 2403b890189SKyle Evansmatching the default behavior of 2413b890189SKyle Evans.Xr getopt 3 . 2423b890189SKyle EvansThe default behavior without 2433b890189SKyle Evans.Ql \&+ 244d50ff7deSBenedict Reuschlingis to permute non-option arguments to the end of 2453b890189SKyle Evans.Fa argv . 2463b890189SKyle Evans.Pp 2473b890189SKyle EvansA leading 2483b890189SKyle Evans.Ql \&- 2493b890189SKyle Evansindicates that all non-option arguments should be treated as if they are 2503b890189SKyle Evansarguments to a literal 2513b890189SKyle Evans.Ql \&1 2523b890189SKyle Evansflag (i.e., the function call will return the value 1, rather than the char 2533b890189SKyle Evansliteral '1'). 25424a0682cSRuslan Ermilov.Sh ENVIRONMENT 25524a0682cSRuslan Ermilov.Bl -tag -width ".Ev POSIXLY_CORRECT" 25624a0682cSRuslan Ermilov.It Ev POSIXLY_CORRECT 25724a0682cSRuslan ErmilovIf set, option processing stops when the first non-option is found and 25824a0682cSRuslan Ermilova leading 25924a0682cSRuslan Ermilov.Ql - 26024a0682cSRuslan Ermilovor 26124a0682cSRuslan Ermilov.Ql + 26224a0682cSRuslan Ermilovin the 26324a0682cSRuslan Ermilov.Fa optstring 26424a0682cSRuslan Ermilovis ignored. 26524a0682cSRuslan Ermilov.El 266a35a7e76SEric Melville.Sh EXAMPLES 267a35a7e76SEric Melville.Bd -literal -compact 268a35a7e76SEric Melvilleint bflag, ch, fd; 269a35a7e76SEric Melvilleint daggerset; 270a35a7e76SEric Melville 271a35a7e76SEric Melville/* options descriptor */ 272a35a7e76SEric Melvillestatic struct option longopts[] = { 273829a229dSAndrey A. Chernov { "buffy", no_argument, NULL, 'b' }, 274829a229dSAndrey A. Chernov { "fluoride", required_argument, NULL, 'f' }, 275a35a7e76SEric Melville { "daggerset", no_argument, \*[Am]daggerset, 1 }, 276829a229dSAndrey A. Chernov { NULL, 0, NULL, 0 } 277a35a7e76SEric Melville}; 278a35a7e76SEric Melville 279a35a7e76SEric Melvillebflag = 0; 280bd45f567SGlen Barberwhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) { 281a35a7e76SEric Melville switch (ch) { 282a35a7e76SEric Melville case 'b': 283a35a7e76SEric Melville bflag = 1; 284a35a7e76SEric Melville break; 285a35a7e76SEric Melville case 'f': 286829a229dSAndrey A. Chernov if ((fd = open(optarg, O_RDONLY, 0)) == -1) 287829a229dSAndrey A. Chernov err(1, "unable to open %s", optarg); 288a35a7e76SEric Melville break; 289a35a7e76SEric Melville case 0: 290a35a7e76SEric Melville if (daggerset) { 291a35a7e76SEric Melville fprintf(stderr,"Buffy will use her dagger to " 292829a229dSAndrey A. Chernov "apply fluoride to dracula's teeth\en"); 293a35a7e76SEric Melville } 294a35a7e76SEric Melville break; 295a35a7e76SEric Melville default: 296a35a7e76SEric Melville usage(); 297a35a7e76SEric Melville } 298bd45f567SGlen Barber} 299a35a7e76SEric Melvilleargc -= optind; 300a35a7e76SEric Melvilleargv += optind; 301a35a7e76SEric Melville.Ed 302a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES 303187f61dfSRuslan ErmilovThis section describes differences to the 304187f61dfSRuslan Ermilov.Tn GNU 305187f61dfSRuslan Ermilovimplementation 306a35a7e76SEric Melvillefound in glibc-2.1.3: 307187f61dfSRuslan Ermilov.Bl -bullet 3088a4dd785SAndrey A. Chernov.\" .It 3098a4dd785SAndrey A. Chernov.\" Handling of 3108a4dd785SAndrey A. Chernov.\" .Ql - 3118a4dd785SAndrey A. Chernov.\" as first char of option string in presence of 3128a4dd785SAndrey A. Chernov.\" environment variable 3138a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT : 3148a4dd785SAndrey A. Chernov.\" .Bl -tag -width ".Bx" 3158a4dd785SAndrey A. Chernov.\" .It Tn GNU 3168a4dd785SAndrey A. Chernov.\" ignores 3178a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT 3188a4dd785SAndrey A. Chernov.\" and returns non-options as 3198a4dd785SAndrey A. Chernov.\" arguments to option '\e1'. 3208a4dd785SAndrey A. Chernov.\" .It Bx 3218a4dd785SAndrey A. Chernov.\" honors 3228a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT 3238a4dd785SAndrey A. Chernov.\" and stops at the first non-option. 3248a4dd785SAndrey A. Chernov.\" .El 3253700175bSAndrey A. Chernov.\" .It 3263700175bSAndrey A. Chernov.\" Handling of 3273700175bSAndrey A. Chernov.\" .Ql - 3283700175bSAndrey A. Chernov.\" within the option string (not the first character): 3293700175bSAndrey A. Chernov.\" .Bl -tag -width ".Bx" 3303700175bSAndrey A. Chernov.\" .It Tn GNU 3313700175bSAndrey A. Chernov.\" treats a 3323700175bSAndrey A. Chernov.\" .Ql - 3333700175bSAndrey A. Chernov.\" on the command line as a non-argument. 3343700175bSAndrey A. Chernov.\" .It Bx 3353700175bSAndrey A. Chernov.\" a 3363700175bSAndrey A. Chernov.\" .Ql - 3373700175bSAndrey A. Chernov.\" within the option string matches a 3383700175bSAndrey A. Chernov.\" .Ql - 3393700175bSAndrey A. Chernov.\" (single dash) on the command line. 3403700175bSAndrey A. Chernov.\" This functionality is provided for backward compatibility with 3413700175bSAndrey A. Chernov.\" programs, such as 3423700175bSAndrey A. Chernov.\" .Xr su 1 , 3433700175bSAndrey A. Chernov.\" that use 3443700175bSAndrey A. Chernov.\" .Ql - 3453700175bSAndrey A. Chernov.\" as an option flag. 3463700175bSAndrey A. Chernov.\" This practice is wrong, and should not be used in any current development. 3473700175bSAndrey A. Chernov.\" .El 348f2fd86b7SAndrey A. Chernov.\" .It 349f2fd86b7SAndrey A. Chernov.\" Handling of 350f2fd86b7SAndrey A. Chernov.\" .Ql :: 351f2fd86b7SAndrey A. Chernov.\" in options string in presence of 352f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT : 353f2fd86b7SAndrey A. Chernov.\" .Bl -tag -width ".Bx" 354f2fd86b7SAndrey A. Chernov.\" .It Both 355f2fd86b7SAndrey A. Chernov.\" .Tn GNU 356f2fd86b7SAndrey A. Chernov.\" and 357f2fd86b7SAndrey A. Chernov.\" .Bx 358f2fd86b7SAndrey A. Chernov.\" ignore 359f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT 360f2fd86b7SAndrey A. Chernov.\" here and take 361f2fd86b7SAndrey A. Chernov.\" .Ql :: 362f2fd86b7SAndrey A. Chernov.\" to 363f2fd86b7SAndrey A. Chernov.\" mean the preceding option takes an optional argument. 364f2fd86b7SAndrey A. Chernov.\" .El 36588485399SAndrey A. Chernov.\" .It 36688485399SAndrey A. Chernov.\" Return value in case of missing argument if first character 36788485399SAndrey A. Chernov.\" (after 36888485399SAndrey A. Chernov.\" .Ql + 36988485399SAndrey A. Chernov.\" or 37088485399SAndrey A. Chernov.\" .Ql - ) 37188485399SAndrey A. Chernov.\" in option string is not 37288485399SAndrey A. Chernov.\" .Ql \&: : 37388485399SAndrey A. Chernov.\" .Bl -tag -width ".Bx" 37488485399SAndrey A. Chernov.\" .It Tn GNU 37588485399SAndrey A. Chernov.\" returns 37688485399SAndrey A. Chernov.\" .Ql \&? 37788485399SAndrey A. Chernov.\" .It Bx 37888485399SAndrey A. Chernov.\" returns 37988485399SAndrey A. Chernov.\" .Ql \&: 38088485399SAndrey A. Chernov.\" (since 38188485399SAndrey A. Chernov.\" .Bx Ns 's 38288485399SAndrey A. Chernov.\" .Fn getopt 38388485399SAndrey A. Chernov.\" does). 38488485399SAndrey A. Chernov.\" .El 3852298a6e7SAndrey A. Chernov.\" .It 3862298a6e7SAndrey A. Chernov.\" Handling of 3872298a6e7SAndrey A. Chernov.\" .Ql --a 3882298a6e7SAndrey A. Chernov.\" in getopt: 3892298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx" 3902298a6e7SAndrey A. Chernov.\" .It Tn GNU 3912298a6e7SAndrey A. Chernov.\" parses this as option 3922298a6e7SAndrey A. Chernov.\" .Ql - , 3932298a6e7SAndrey A. Chernov.\" option 3942298a6e7SAndrey A. Chernov.\" .Ql a . 3952298a6e7SAndrey A. Chernov.\" .It Bx 3962298a6e7SAndrey A. Chernov.\" parses this as 3972298a6e7SAndrey A. Chernov.\" .Ql -- , 3982298a6e7SAndrey A. Chernov.\" and returns \-1 (ignoring the 3992298a6e7SAndrey A. Chernov.\" .Ql a ) . 4002298a6e7SAndrey A. Chernov.\" (Because the original 4012298a6e7SAndrey A. Chernov.\" .Fn getopt 4022298a6e7SAndrey A. Chernov.\" does.) 4032298a6e7SAndrey A. Chernov.\" .El 404187f61dfSRuslan Ermilov.It 405187f61dfSRuslan ErmilovSetting of 406187f61dfSRuslan Ermilov.Va optopt 407187f61dfSRuslan Ermilovfor long options with 408187f61dfSRuslan Ermilov.Va flag 409187f61dfSRuslan Ermilov!= 410187f61dfSRuslan Ermilov.Dv NULL : 411829a229dSAndrey A. Chernov.Bl -tag -width ".Bx" 412187f61dfSRuslan Ermilov.It Tn GNU 413187f61dfSRuslan Ermilovsets 414187f61dfSRuslan Ermilov.Va optopt 415187f61dfSRuslan Ermilovto 416187f61dfSRuslan Ermilov.Va val . 417829a229dSAndrey A. Chernov.It Bx 418187f61dfSRuslan Ermilovsets 419187f61dfSRuslan Ermilov.Va optopt 420187f61dfSRuslan Ermilovto 0 (since 421187f61dfSRuslan Ermilov.Va val 422187f61dfSRuslan Ermilovwould never be returned). 423a35a7e76SEric Melville.El 4242298a6e7SAndrey A. Chernov.\" .It 4252298a6e7SAndrey A. Chernov.\" Handling of 4262298a6e7SAndrey A. Chernov.\" .Ql -W 4272298a6e7SAndrey A. Chernov.\" with 4282298a6e7SAndrey A. Chernov.\" .Ql W; 4292298a6e7SAndrey A. Chernov.\" in option string in 4302298a6e7SAndrey A. Chernov.\" .Fn getopt 4312298a6e7SAndrey A. Chernov.\" (not 4322298a6e7SAndrey A. Chernov.\" .Fn getopt_long ) : 4332298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx" 4342298a6e7SAndrey A. Chernov.\" .It Tn GNU 4352298a6e7SAndrey A. Chernov.\" causes a segfault. 4362298a6e7SAndrey A. Chernov.\" .It Bx 4372298a6e7SAndrey A. Chernov.\" no special handling is done; 4382298a6e7SAndrey A. Chernov.\" .Ql W; 4392298a6e7SAndrey A. Chernov.\" is interpreted as two separate options, neither of which take an argument. 4402298a6e7SAndrey A. Chernov.\" .El 441187f61dfSRuslan Ermilov.It 442187f61dfSRuslan ErmilovSetting of 443187f61dfSRuslan Ermilov.Va optarg 444187f61dfSRuslan Ermilovfor long options without an argument that are 445187f61dfSRuslan Ermilovinvoked via 446187f61dfSRuslan Ermilov.Ql -W 447187f61dfSRuslan Ermilov.Ql ( W; 448187f61dfSRuslan Ermilovin option string): 449829a229dSAndrey A. Chernov.Bl -tag -width ".Bx" 450187f61dfSRuslan Ermilov.It Tn GNU 451187f61dfSRuslan Ermilovsets 452187f61dfSRuslan Ermilov.Va optarg 453187f61dfSRuslan Ermilovto the option name (the argument of 454187f61dfSRuslan Ermilov.Ql -W ) . 455829a229dSAndrey A. Chernov.It Bx 456187f61dfSRuslan Ermilovsets 457187f61dfSRuslan Ermilov.Va optarg 458187f61dfSRuslan Ermilovto 459187f61dfSRuslan Ermilov.Dv NULL 460187f61dfSRuslan Ermilov(the argument of the long option). 461a35a7e76SEric Melville.El 462187f61dfSRuslan Ermilov.It 463187f61dfSRuslan ErmilovHandling of 464187f61dfSRuslan Ermilov.Ql -W 465187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known 466187f61dfSRuslan Ermilovlong option 467187f61dfSRuslan Ermilov.Ql ( W; 468187f61dfSRuslan Ermilovin option string): 469829a229dSAndrey A. Chernov.Bl -tag -width ".Bx" 470187f61dfSRuslan Ermilov.It Tn GNU 471187f61dfSRuslan Ermilovreturns 472187f61dfSRuslan Ermilov.Ql -W 473187f61dfSRuslan Ermilovwith 474187f61dfSRuslan Ermilov.Va optarg 475187f61dfSRuslan Ermilovset to the unknown option. 476829a229dSAndrey A. Chernov.It Bx 477187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns 478187f61dfSRuslan Ermilov.Ql \&? 479187f61dfSRuslan Ermilovwith 480187f61dfSRuslan Ermilov.Va optopt 481187f61dfSRuslan Ermilovset to 0 and 482187f61dfSRuslan Ermilov.Va optarg 483187f61dfSRuslan Ermilovset to 484187f61dfSRuslan Ermilov.Dv NULL 485187f61dfSRuslan Ermilov(as 486187f61dfSRuslan Ermilov.Tn GNU Ns 's 487187f61dfSRuslan Ermilovman page documents). 488a35a7e76SEric Melville.El 489f2fd86b7SAndrey A. Chernov.\" .It 490f2fd86b7SAndrey A. Chernov.\" The error messages are different. 491187f61dfSRuslan Ermilov.It 492829a229dSAndrey A. Chernov.Bx 493187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in 494187f61dfSRuslan Ermilovthe calling sequence as 495187f61dfSRuslan Ermilov.Tn GNU 496187f61dfSRuslan Ermilovdoes. 497187f61dfSRuslan ErmilovThe aspects normally used by 498187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of 499187f61dfSRuslan Ermilov.Va optind 500187f61dfSRuslan Ermilovrelative 501187f61dfSRuslan Ermilovto current positions) are the same, though. 502187f61dfSRuslan Ermilov(We do fewer variable swaps.) 503a35a7e76SEric Melville.El 504a35a7e76SEric Melville.Sh SEE ALSO 505a35a7e76SEric Melville.Xr getopt 3 506a35a7e76SEric Melville.Sh HISTORY 507a35a7e76SEric MelvilleThe 508a35a7e76SEric Melville.Fn getopt_long 509829a229dSAndrey A. Chernovand 510829a229dSAndrey A. Chernov.Fn getopt_long_only 511c2025a76SJoel Dahlfunctions first appeared in the 512187f61dfSRuslan Ermilov.Tn GNU 513c2025a76SJoel Dahllibiberty library. 514187f61dfSRuslan ErmilovThe first 515829a229dSAndrey A. Chernov.Bx 516829a229dSAndrey A. Chernovimplementation of 517829a229dSAndrey A. Chernov.Fn getopt_long 518829a229dSAndrey A. Chernovappeared in 519829a229dSAndrey A. Chernov.Nx 1.5 , 520829a229dSAndrey A. Chernovthe first 521829a229dSAndrey A. Chernov.Bx 522829a229dSAndrey A. Chernovimplementation of 523829a229dSAndrey A. Chernov.Fn getopt_long_only 524829a229dSAndrey A. Chernovin 525829a229dSAndrey A. Chernov.Ox 3.3 . 526829a229dSAndrey A. Chernov.Fx 527829a229dSAndrey A. Chernovfirst included 528829a229dSAndrey A. Chernov.Fn getopt_long 529829a229dSAndrey A. Chernovin 530829a229dSAndrey A. Chernov.Fx 5.0 , 531829a229dSAndrey A. Chernov.Fn getopt_long_only 532829a229dSAndrey A. Chernovin 533829a229dSAndrey A. Chernov.Fx 5.2 . 534a35a7e76SEric Melville.Sh BUGS 535829a229dSAndrey A. ChernovThe 536629a7369SRuslan Ermilov.Fa argv 537829a229dSAndrey A. Chernovargument is not really 538629a7369SRuslan Ermilov.Vt const 539829a229dSAndrey A. Chernovas its elements may be permuted (unless 540829a229dSAndrey A. Chernov.Ev POSIXLY_CORRECT 541829a229dSAndrey A. Chernovis set). 542829a229dSAndrey A. Chernov.Pp 543187f61dfSRuslan ErmilovThe implementation can completely replace 544a35a7e76SEric Melville.Xr getopt 3 , 545a35a7e76SEric Melvillebut right now we are using separate code. 546*d646513eSKyle Evans.Pp 547*d646513eSKyle Evans.Nm 548*d646513eSKyle Evansmakes the assumption that the first argument should always be skipped because 549*d646513eSKyle Evansit's typically the program name. 550*d646513eSKyle EvansAs a result, setting 551*d646513eSKyle Evans.Va optind 552*d646513eSKyle Evansto 0 will indicate that 553*d646513eSKyle Evans.Nm 554*d646513eSKyle Evansshould reset, and 555*d646513eSKyle Evans.Va optind 556*d646513eSKyle Evanswill be set to 1 in the process. 557*d646513eSKyle EvansThis behavior differs from 558*d646513eSKyle Evans.Xr getopt 3 , 559*d646513eSKyle Evanswhich will handle an 560*d646513eSKyle Evans.Va optind 561*d646513eSKyle Evansvalue of 0 as expected and process the first element. 562