1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
.\"
.\" Copyright (c) 1988, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\"
.\" Copyright 2018 Jason King
.\" Copyright 2018, Joyent, Inc.
.\" Copyright 2020 Oxide Computer Company
.\"
.Dd August 10, 2020
.Dt GETOPT_LONG 3C
.Os
.Sh NAME
.Nm getopt_long ,
.Nm getopt_long_clip ,
.Nm getopt_long_only
.Nd get long options from command line argument list
.Sh SYNOPSIS
.In getopt.h
.Vt extern char *optarg ;
.Vt extern int optind ;
.Vt extern int optopt ;
.Vt extern int opterr ;
.Ft int
.Fo getopt_long
.Fa "int argc"
.Fa "char * const *argv"
.Fa "const char *optstring"
.Fa "const struct option *longopts"
.Fa "int *longindex"
.Fc
.Ft int
.Fo getopt_long_only
.Fa "int argc"
.Fa "char * const *argv"
.Fa "const char *optstring"
.Fa "const struct option *longopts"
.Fa "int *longindex"
.Fc
.Ft int
.Fo getopt_long_clip
.Fa "int argc"
.Fa "char * const *argv"
.Fa "const char *optstring"
.Fa "const struct option *longopts"
.Fa "int *longindex"
.Fc
.Sh DESCRIPTION
The
.Fn getopt_long
function is similar to
.Xr getopt 3C
but it accepts options in two forms: short options and long options.
Short options are the traditional option flags that use a hyphen followed
by a single character.
This is the only form of option that is portable and it is supported by
.Xr getopt 3C .
Note, some implementation of
.Xr getopt 3C
do support non-standard extensions for long options; however, these are
not portable and not considered in this manual page.
Common example of short options are:
.Fl a ,
.Fl l ,
and
.Fl r .
Long options use two hyphen characters are generally full words.
The long versions of the above might be:
.Fl -all ,
.Fl -list ,
and
.Fl -recursive .
.Pp
The
.Fn getopt_long
function can be used to:
.Bl -enum
.It
Support an option with both short and long forms.
.It
Support an option with only a short form.
.It
Support an option with only a long form.
.El
.Pp
To have a short option selected, as with
.Xr getopt 3C ,
it must be listed in
.Fa optstring .
Long options are instead listed in the
.Fa longopts
array.
For an option to have both a short and long form it must be present in
both
.Fa optstring
and
.Fa longopts .
.Pp
Long options can be handled in two different ways.
In the first way, every long option understood by the program has a
corresponding short option, and the option structure is only used to
translate from long options to short options.
When used in this fashion,
.Fn getopt_long
behaves identically to
.Xr getopt 3C .
This is a good way to add long option processing to an existing program
with the minimum of rewriting.
.Pp
In the second mechanism, a long option sets a flag in the
.Vt option
structure passed, or will store a pointer to the command line argument
in the
.Vt option
structure passed to it for options that take arguments.
Additionally,
the long option's argument may be specified as a single argument with
an equal sign, e.g.,
.Pp
.Dl "myprogram --myoption=somevalue"
.Pp
When a long option is processed, the call to
.Fn getopt_long
will return 0.
For this reason, long option processing without
shortcuts is not backwards compatible with
.Xr getopt 3C .
.Pp
It is possible to combine these methods, providing for long options
processing with short option equivalents for some options.
Less
frequently used options would be processed as long options only.
.Pp
In
.Fn getopt_long
and
.Fn getopt_long_only ,
.Fa optstring
acts similar to
.Fa optstring
in
.Xr getopt 3C ,
listing the set of supported short option flags.
In addition,
.Fa optstring
can begin with
.Ql +
or
.Ql - .
If
.Fa optstring
begins with
.Ql + ,
the first non-option terminates option processing.
This is equivalent to setting the environment variable
.Ev POSIXLY_CORRECT .
If
.Fa optstring
begins with
.Ql - ,
non-options are treated as options to the argument
.Ql \e1 .
.Pp
If
.Fa optstring
does not begin with
.Ql +
and
.Ev POSIXLY_CORRECT
is not set, if
.Ql W\&;
appears in
.Fa optstring ,
.Ql "-W myoption"
is treated the same as
.Ql "--myoption"
and
.Va optarg
is set to
.Ql myoption .
.Pp
In
.Fn getopt_long_clip ,
.Ql +
and
.Ql -
are ignored at the beginning of a string.
.Pp
The
.Fn getopt_long ,
.Fn getopt_long_only ,
and
.Fn getopt_long_clip
functions require a structure to be initialized describing the long
options.
The structure is:
.Bd -literal -offset indent
struct option {
char *name;
int has_arg;
int *flag;
int val;
};
.Ed
.Pp
The
.Fa name
field should contain the option name without the leading double hyphen.
.Pp
The
.Fa has_arg
field should be one of:
.Pp
.Bl -tag -width ".Dv optional_argument" -offset indent -compact
.It Dv no_argument
no argument to the option is expected
.It Dv required_argument
an argument to the option is required
.It Dv optional_argument
an argument to the option may be presented
.El
.Pp
If
.Fa flag
is not
.Dv NULL ,
then the integer pointed to by it will be set to the
value in the
.Fa val
field and
.Va optopt
will be set to
.Sy 0 .
If the
.Fa flag
field is
.Dv NULL ,
then the
.Fa val
field will be returned and
.Va optopt
is set to the value in the
.Fa val
field.
Setting
.Fa flag
to
.Dv NULL
and setting
.Fa val
to the corresponding short option will make this function act just
like
.Xr getopt 3C .
.Pp
If the
.Fa longindex
field is not
.Dv NULL ,
then the integer pointed to by it will be set to the index of the long
option relative to
.Fa longopts .
.Pp
The last element of the
.Fa longopts
array has to be filled with zeroes.
.Pp
The
.Fn getopt_long_only
function behaves identically to
.Fn getopt_long
with the exception that long options may start with
.Ql -
in addition to
.Ql -- .
If an option starting with
.Ql -
does not match a long option but does match a single-character option,
the single-character option is returned.
.Pp
The
.Fn getopt_long_clip
function is a variation of
.Fn getopt_long
except that options must also adhere to the Sun CLIP specification.
Specifically, the major differences from
.Fn getopt_long
are:
.Bl -bullet -offset indent
.It
All option arguments are required
.Po
.Dv optional_argument
is treated the same as
.Dv required_argument
.Pc .
.It
Long options cannot be abbreviated on the command line.
.It
Long options must use a double hyphen
.Pq Ql -- .
.It
Option processing stops at the first non-option.
.It
All long options must have an equivalent short option (single character) and
vice-versa.
.It
A leading
.Ql +
or
.Ql -
in
.Fa optstring
is ignored.
.Fa optstring
is treated as if it began after the leading
.Ql +
or
.Ql - .
.El
.Pp
On each call to
.Fn getopt_long ,
.Fn getopt_long_only ,
or
.Fn getopt_long ,
.Va optind
is set to the
.Va argv
index of the
.Em next
argument to be processed.
.Va optind
is initialized to
.Sy 1
prior to the first invocation of
.Fn getopt_long ,
.Fn getopt_long_only ,
or
.Fn getopt_long_clip .
.Pp
If
.Va opterr
is set to a non-zero value and
.Fa optstring
does not start with
.Ql \&: ,
.Fn getopt_long ,
.Fn getopt_long_only ,
and
.Fn getopt_long_clip
will print an error message to
.Sy stderr
when an error or invalid option is encountered.
.Sh RETURN VALUES
If the
.Fa flag
field in
.Vt "struct option"
is
.Dv NULL ,
.Fn getopt_long
and
.Fn getopt_long_only
return the value specified in the
.Fa val
field, which is usually just the corresponding short option.
If
.Fa flag
is not
.Dv NULL ,
these functions return 0 and store
.Fa val
in the location pointed to by
.Fa flag .
These functions return
.Ql \&:
if there was a missing option argument,
.Ql \&?
if the user specified an unknown or ambiguous option, and
\-1 when the argument list has been exhausted.
.Pp
If a long option to
.Fn getopt_long_clip
is missing its equivalent short option (or vice-versa),\-1 is returned on the
first call to
.Fn getopt_long_clip ,
and
.Dv errno
is set to
.Er EINVAL .
If
.Va opterr
is set to a non-zero value and
.Fa optstring
does not start with
.Ql \&: ,
an error message will be written to
.Sy stderr .
.Pp
If
.Fa optstring
does not start with
.Ql \&:
and
.Fn getopt_long ,
.Fn getopt_long_only ,
or
.Fn getopt_long_clip
return
.Ql \&:
or
.Ql \&? ,
if
.Va opterr
is set to a non-zero value, an error message is written to
.Dv stderr .
.Sh ENVIRONMENT
The following environment variables can effect the execution of
.Nm getopt_long ,
.Nm getopt_long_only ,
and
.Nm getopt_long_clip :
.Ev LANG ,
.Ev LC_ALL ,
.Ev LC_MESSAGES .
See
.Xr environ 5 .
.Bl -tag -width ".Ev POSIXLY_CORRECT"
.It Ev POSIXLY_CORRECT
If set, option processing stops when the first non-option is found and
a leading
.Ql -
or
.Ql +
in the
.Fa optstring
is ignored.
.El
.Sh USAGE
Similar to
.Xr getopt 3C ,
since there is no unambiguous way to detect a missing option-argument except when the
option is the last option on the command line, the
.Fn getopt_long ,
.Fn getopt_long_only ,
and
.Fn getopt_long_clip
functions cannot fully check for mandatory arguments.
For example, the option string
.Ql ho\&:
with an input of
.Ql Fl o Fl h
will assume that
.Ql Fl h
is the required argument to
.Fl o
instead of assuming that
.Fl o
is missing its option-argument.
.Pp
Like
.Xr getopt 3C ,
grouping options taking or requiring arguments with other options is a violation of the
Basic Utility Command syntax standard (see
.Xr Intro 1 ) .
For example, given the option string
.Ql cde\&: ,
running:
.Pp
.Dl cmd Fl cde Ar ieio
.Pp
is incorrect.
Current versions of
.Nm getopt_long ,
.Nm getopt_long_only ,
and
.Nm getopt_long_clip
accept this, however future versions may not support this.
The correct invocation would be:
.Pp
.Dl cmd Fl cd Fl e Ar ieio
.Sh EXAMPLES
.Sy Example 1
Basic usage of
.Fn getopt_long .
.Pp
In this example, the short options,
.Fl b
and
.Fl f
are treated the same way as their corresponding long options
.Fl -buffy
and
.Fl -fluoride .
The long option
.Fl -daggerset
is only matched as a long option.
.Pp
.Bd -literal -compact
int bflag, ch, fd;
int daggerset;
/* options descriptor */
static struct option longopts[] = {
{ "buffy", no_argument, NULL, 'b' },
{ "fluoride", required_argument, NULL, 'f' },
{ "daggerset", no_argument, \*[Am]daggerset, 1 },
{ NULL, 0, NULL, 0 }
};
bflag = 0;
while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 'f':
if ((fd = open(optarg, O_RDONLY, 0)) == -1)
err(1, "unable to open %s", optarg);
break;
case 0:
if (daggerset) {
fprintf(stderr,"Buffy will use her dagger to "
"apply fluoride to dracula's teeth\en");
}
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
.Ed
.Pp
.Sy Example 2
Mixing short-only and long-only options.
.Pp
This example has a program that uses both short and long options and
always causes the option to be handled in a way that is similar to
.Xr getopt 3C
regardless of if it is short or long.
Options that are only long options are assigned a character value that
is outside of the common 8-bit range
.Po
starting at
.Dv USHRT_MAX
+ 1.
.Pc
This allows them to still integrate into a normal
.Xr getopt 3C
style option processing loop.
.Pp
In the following code,
.Fl s
is only usable as a short option while
.Fl -long-only
is only usable as a long option, hence
.Fl s
is only specified in
.Fa optstring
and
.Fl -long-only
is only specified in the
.Fa longopts
.Vt option
array.
.Pp
.Bd -literal -compact
#include <getopt.h>
#include <stdio.h>
#include <limits.h>
enum longopt_chars {
LONG_ONLY = USHRT_MAX +1
};
static struct option longopts[] = {
{ "all", no_argument, NULL, 'a' },
{ "list", no_argument, NULL, 'l' },
{ "long-only", no_argument, NULL, LONG_ONLY },
{ "output", required_argument, NULL, 'o' },
{ NULL }
};
int
main(int argc, char *argv[])
{
int ch;
while ((ch = getopt_long(argc, argv, "alo:s", longopts,
NULL)) != -1) {
switch (ch) {
case 'a':
printf("found -a\en");
break;
case 'l':
printf("found -l\en");
break;
case 'o':
printf("found -o: %s\en", optarg);
break;
case 's':
printf("found -s\en");
break;
case LONG_ONLY:
printf("found --long-only\en");
break;
default:
break;
}
}
return (0);
}
.Ed
.Sh ERRORS
The
.Fn getopt_long_clip
function will fail if:
.Bl -tag -width EINVAL
.It Er EINVAL
A short option is missing a corresponding long option, or vice-versa.
.El
.Pp
There are no errors defined for
.Fn getopt_long
and
.Fn getopt_long_only .
.Sh IMPLEMENTATION DIFFERENCES
While the illumos implementations of
.Nm getopt_long
and
.Nm getopt_long_only
are broadly compatible with other implementations, the following edge cases
have historically been known to vary among implementations:
.Bl -bullet
.It
The setting of
.Va optopt
for long options with
.Fa flag
!=
.Dv NULL
in
.Vt struct option .
In illumos,
.Va optopt
is set to 0 (since
.Fa val
would never be returned).
.It
The setting of
.Va optarg
for long options without an argument that are
invoked via
.Ql -W
.Ql ( W\&;
in
.Fa optstring ) .
illumos sets
.Va optarg
to the option name (the argument of
.Ql -W ) .
.It
The handling of
.Ql -W
with an argument that is not (a prefix to) a known
long option
.Ql ( W\&;
in
.Fa optstring ) .
illumos treats this as an error (unknown option) and returns
.Ql \&?
with
.Va optopt
set to 0 and
.Va optarg
set to
.Dv NULL .
.It
illumos
may not permute the argument vector at the same points in
the calling sequence as other implementations.
The aspects normally used by
the caller (ordering after \-1 is returned, the value of
.Va optind
relative
to current positions) are the same, though.
(We often do fewer variable swaps.)
.El
.Sh INTERFACE STABILITY
Committed
.Sh MT-LEVEL
Unsafe
.Sh SEE ALSO
.Xr getopt 3C
.Sh BUGS
The
.Fa argv
argument is not really
.Vt const
as its elements may be permuted (unless
.Ev POSIXLY_CORRECT
is set).
|