2 .\" Copyright (c) 1988, 1991, 1993
3 .\" The Regents of the University of California. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. Neither the name of the University nor the names of its contributors
14 .\" may be used to endorse or promote products derived from this software
15 .\" without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" Copyright 2018 Jason King
31 .\" Copyright 2018, Joyent, Inc.
39 .Nd get long options from command line argument list
42 .Vt extern char *optarg ;
43 .Vt extern int optind ;
44 .Vt extern int optopt ;
45 .Vt extern int opterr ;
49 .Fa "char * const *argv"
50 .Fa "const char *optstring"
51 .Fa "const struct option *longopts"
57 .Fa "char * const *argv"
58 .Fa "const char *optstring"
59 .Fa "const struct option *longopts"
65 .Fa "char * const *argv"
66 .Fa "const char *optstring"
67 .Fa "const struct option *longopts"
73 function is similar to
75 but it accepts options in two forms: words and characters.
78 function provides a superset of the functionality of
83 can be used in two ways.
85 In the first way, every long option understood
86 by the program has a corresponding short option, and the option
87 structure is only used to translate from long options to short
89 When used in this fashion,
91 behaves identically to
93 This is a good way to add long option processing to an existing program
94 with the minimum of rewriting.
96 In the second mechanism, a long option sets a flag in the
98 structure passed, or will store a pointer to the command line argument
101 structure passed to it for options that take arguments.
103 the long option's argument may be specified as a single argument with
106 .Dl "myprogram --myoption=somevalue"
108 When a long option is processed, the call to
111 For this reason, long option processing without
112 shortcuts is not backwards compatible with
115 It is possible to combine these methods, providing for long options
116 processing with short option equivalents for some options.
118 frequently used options would be processed as long options only.
123 .Fn getopt_long_only ,
139 the first non-option terminates option processing.
140 This is equivalent to setting the environment variable
141 .Ev POSIXLY_CORRECT .
146 non-options are treated as options to the argument
160 is treated the same as
168 .Fn getopt_long_clip ,
172 are ignored at the beginning of a string.
176 .Fn getopt_long_only ,
179 functions require a structure to be initialized describing the long
182 .Bd -literal -offset indent
193 field should contain the option name without the leading double dash.
197 field should be one of:
199 .Bl -tag -width ".Dv optional_argument" -offset indent -compact
201 no argument to the option is expected
202 .It Dv required_argument
203 an argument to the option is required
204 .It Dv optional_argument
205 an argument to the option may be presented
212 then the integer pointed to by it will be set to the
225 field will be returned and
227 is set to the value in the
236 to the corresponding short option will make this function act just
244 then the integer pointed to by it will be set to the index of the long
248 The last element of the
250 array has to be filled with zeroes.
254 function behaves identically to
256 with the exception that long options may start with
260 If an option starting with
262 does not match a long option but does match a single-character option,
263 the single-character option is returned.
267 function is a variation of
269 except that options must also adhere to the Sun CLIP specification.
270 Specifically, the major differences from
273 .Bl -bullet -offset indent
275 All option arguments are required
277 .Dv optional_argument
278 is treated the same as
279 .Dv required_argument
282 Long options cannot be abbreviated on the command line.
284 Long options must use a double dash
287 Option processing stops at the first non-option.
289 All long options must have an eqivalent short option (single character) and
300 is treated as if it began after the leading
308 .Fn getopt_long_only ,
316 argument to be processed.
320 prior to the first invocation of
322 .Fn getopt_long_only ,
324 .Fn getopt_long_clip .
328 is set to a non-zero value and
333 .Fn getopt_long_only ,
336 will print an error message to
338 when an error or invalid option is encountered.
349 return the value specified in the
351 field, which is usually just the corresponding short option.
356 these functions return 0 and store
358 in the location pointed to by
360 These functions return
362 if there was a missing option argument,
364 if the user specified an unknown or ambiguous option, and
365 \-1 when the argument list has been exhausted.
369 is missing its equivalent short option (or vice-versa),\-1 is returned on the
371 .Fn getopt_long_clip ,
378 is set to a non-zero value and
382 an error message will be written to
391 .Fn getopt_long_only ,
400 is set to a non-zero value, an error message is written to
403 The following environment variables can effect the execution of
405 .Nm getopt_long_only ,
407 .Nm getopt_long_clip :
413 .Bl -tag -width ".Ev POSIXLY_CORRECT"
414 .It Ev POSIXLY_CORRECT
415 If set, option processing stops when the first non-option is found and
427 since there is no unambiguous way to detect a missing option-argument except when the
428 option is the last option on the command line, the
430 .Fn getopt_long_only ,
433 functions cannot fully check for mandatory arguments.
434 For example, the option string
440 is the required argument to
442 instead of assuming that
444 is missing its option-argument.
448 grouping options taking or requiring arguments with other options is a violation of the
449 Basic Utility Command syntax standard (see
451 For example, given the option string
455 .Dl cmd Fl cde Ar ieio
460 .Nm getopt_long_only ,
463 accept this, however future versions may not support this.
464 The correct invocation would be:
466 .Dl cmd Fl cd Fl e Ar ieio
468 .Bd -literal -compact
472 /* options descriptor */
473 static struct option longopts[] = {
474 { "buffy", no_argument, NULL, 'b' },
475 { "fluoride", required_argument, NULL, 'f' },
476 { "daggerset", no_argument, \*[Am]daggerset, 1 },
481 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
487 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
488 err(1, "unable to open %s", optarg);
492 fprintf(stderr,"Buffy will use her dagger to "
493 "apply fluoride to dracula's teeth\en");
506 function will fail if:
507 .Bl -tag -width EINVAL
509 A short option is missing a corresponding long option, or vice-versa.
512 There are no errors defined for
515 .Fn getopt_long_only .
516 .Sh IMPLEMENTATION DIFFERENCES
517 While the illumos implementations of
521 are broadly compatible with other implementations, the following edge cases
522 have historically been known to vary among implementations:
527 for long options with
537 would never be returned).
541 for long options without an argument that are
549 to the option name (the argument of
554 with an argument that is not (a prefix to) a known
559 illumos treats this as an error (unknown option) and returns
569 may not permute the argument vector at the same points in
570 the calling sequence as other implementations.
571 The aspects normally used by
572 the caller (ordering after \-1 is returned, the value of
575 to current positions) are the same, though.
576 (We often do fewer variable swaps.)
578 .Sh INTERFACE STABILITY
587 argument is not really
589 as its elements may be permuted (unless