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
|
'\" t
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH getopt_long 3 2025-12-25 "Linux man-pages (unreleased)"
.SH NAME
getopt_long
\- parse command-line options
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #define _GNU_SOURCE
.B #include <getopt.h>
.P
.BI "int getopt_long(int " argc ", char *" argv [],
.BI " const char *" optstring ,
.BI " const struct option *" longopts ", int *" longindex );
.fi
.SH DESCRIPTION
The
.BR getopt_long ()
function works like
.BR getopt (3)
except that it also accepts long options, started with two dashes.
(If the program accepts only long options, then
.I optstring
should be specified as an empty string (""), not NULL.)
Long option names may be abbreviated if the abbreviation is
unique or is an exact match for some defined option.
A long option
may take a parameter, of the form
.B \-\-arg=param
or
.BR "\-\-arg param" .
.P
.I longopts
is a pointer to the first element of an array of
.I struct option
declared in
.I <getopt.h>
as
.P
.in +4n
.EX
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
.EE
.in
.P
The meanings of the different fields are:
.TP
.I name
is the name of the long option.
.TP
.I has_arg
is:
.B no_argument
(or 0) if the option does not take an argument;
.B required_argument
(or 1) if the option requires an argument;
or
.B optional_argument
(or 2) if the option takes an optional argument.
.TP
.I flag
specifies how results are returned for a long option.
If
.I flag
is NULL, then
.BR getopt_long ()
returns
.IR val .
(For example, the calling program may set
.I val
to the equivalent short
option character.)
Otherwise,
.BR getopt_long ()
returns 0, and
.I flag
points to a variable which is set to
.I val
if the option is found,
but left unchanged if the option is not found.
.TP
.I val
is the value to return, or to load into the variable pointed
to by
.IR flag .
.P
The last element of the array has to be filled with zeros.
.P
If
.I longindex
is not NULL, it
points to a variable which is set to the index of the long option relative to
.IR longopts .
.P
Its analogue to
.BR getopt (3)'s
.I optopt
is
.RI \[lq] "argv[(optind \- 1)]" \[rq].
.SH RETURN VALUE
See
.BR getopt (3).
.P
.BR getopt_long ()
also returns the option
character when a short option is recognized.
For a long option,
it returns
.I val
if
.I flag
is NULL, and 0 otherwise.
Error and \-1 returns are the same as for
.BR getopt (3),
plus \[aq]?\[aq] for an
ambiguous match or an extraneous parameter.
.SH ENVIRONMENT
See
.BR getopt (3).
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lb lb lbx
l l l.
Interface Attribute Value
T{
.na
.nh
.BR getopt_long ()
T} Thread safety T{
.na
.nh
MT-Unsafe race:getopt env
T}
.TE
.SH STANDARDS
GNU.
.SH EXAMPLES
The following example program illustrates the use of
.BR getopt_long ()
with most of its features.
.P
.\" SRC BEGIN (getopt_long.c)
.EX
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
\&
int
main(int argc, char *argv[])
{
int c;
int digit_optind = 0;
\&
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, \[aq]c\[aq]},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
\&
c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == \-1)
break;
\&
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\[rs]n");
break;
\&
case \[aq]0\[aq]:
case \[aq]1\[aq]:
case \[aq]2\[aq]:
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv\-elements.\[rs]n");
digit_optind = this_option_optind;
printf("option %c\[rs]n", c);
break;
\&
case \[aq]a\[aq]:
printf("option a\[rs]n");
break;
\&
case \[aq]b\[aq]:
printf("option b\[rs]n");
break;
\&
case \[aq]c\[aq]:
printf("option c with value \[aq]%s\[aq]\[rs]n", optarg);
break;
\&
case \[aq]d\[aq]:
printf("option d with value \[aq]%s\[aq]\[rs]n", optarg);
break;
\&
case \[aq]?\[aq]:
break;
\&
default:
printf("?? getopt returned character code 0%o ??\[rs]n", c);
}
}
\&
if (optind < argc) {
printf("non\-option ARGV\-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\[rs]n");
}
\&
exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR getopt (1),
.BR getopt (3),
.BR getopt_long_only (3),
.BR getsubopt (3)
|