sl@0
|
1 |
/** @file ../include/getopt.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @fn getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex)
|
sl@0
|
6 |
@param argc
|
sl@0
|
7 |
@param argv
|
sl@0
|
8 |
@param optstring
|
sl@0
|
9 |
@param longopts
|
sl@0
|
10 |
@param longindex
|
sl@0
|
11 |
@return If the flag field in struct option
|
sl@0
|
12 |
is NULL, getopt_long returns the value specified in the val field, which is usually just the corresponding short option.
|
sl@0
|
13 |
If flag is not NULL, this function returns 0 and stores val in the location pointed to by flag. This function returns ' : '
|
sl@0
|
14 |
if there was a missing option argument, ' ? '
|
sl@0
|
15 |
if the user specified an unknown or ambiguous option, and
|
sl@0
|
16 |
-1 when the argument list has been exhausted.
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
The getopt_long function is similar to getopt but it accepts options in two forms: words and characters.
|
sl@0
|
20 |
The getopt_long function provides a superset of the functionality of getopt .
|
sl@0
|
21 |
The getopt_long function
|
sl@0
|
22 |
can be used in two ways.
|
sl@0
|
23 |
In the first way, every long option understood
|
sl@0
|
24 |
by the program has a corresponding short option, and the option
|
sl@0
|
25 |
structure is only used to translate from long options to short
|
sl@0
|
26 |
options.
|
sl@0
|
27 |
When used in this fashion, getopt_long behaves identically to getopt .
|
sl@0
|
28 |
This is a good way to add long option processing to an existing program
|
sl@0
|
29 |
with the minimum of rewriting.
|
sl@0
|
30 |
|
sl@0
|
31 |
In the second mechanism, a long option sets a flag in the option
|
sl@0
|
32 |
structure passed, or will store a pointer to the command line argument
|
sl@0
|
33 |
in the option
|
sl@0
|
34 |
structure passed to it for options that take arguments.
|
sl@0
|
35 |
Additionally,
|
sl@0
|
36 |
the long option's argument may be specified as a single argument with
|
sl@0
|
37 |
an equal sign, e.g.,
|
sl@0
|
38 |
|
sl@0
|
39 |
@code
|
sl@0
|
40 |
myprogram --myoption=somevalue
|
sl@0
|
41 |
@endcode
|
sl@0
|
42 |
|
sl@0
|
43 |
When a long option is processed, the call to getopt_long will return 0.
|
sl@0
|
44 |
For this reason, long option processing without
|
sl@0
|
45 |
shortcuts is not backwards compatible with getopt . It is possible to combine these methods, providing for long options
|
sl@0
|
46 |
processing with short option equivalents for some options.
|
sl@0
|
47 |
Less
|
sl@0
|
48 |
frequently used options would be processed as long options only. The getopt_long call requires a structure to be initialized describing the long
|
sl@0
|
49 |
options.
|
sl@0
|
50 |
@code
|
sl@0
|
51 |
The structure is: struct option {
|
sl@0
|
52 |
char *name;
|
sl@0
|
53 |
int has_arg;
|
sl@0
|
54 |
int *flag;
|
sl@0
|
55 |
int val;
|
sl@0
|
56 |
};
|
sl@0
|
57 |
@endcode
|
sl@0
|
58 |
The name field should contain the option name without the leading double dash.
|
sl@0
|
59 |
|
sl@0
|
60 |
The has_arg field should be one of:
|
sl@0
|
61 |
|
sl@0
|
62 |
no argument no argument to the option is expect required_argument
|
sl@0
|
63 |
an argument to the option is required optional_argument an argument to the option may be presented.
|
sl@0
|
64 |
|
sl@0
|
65 |
If flag is not NULL, then the integer pointed to by it will be set to the
|
sl@0
|
66 |
value in the val field.
|
sl@0
|
67 |
If the flag field is NULL, then the val field will be returned.
|
sl@0
|
68 |
Setting flag to NULL and setting val to the corresponding short option will make this function act just
|
sl@0
|
69 |
like getopt .
|
sl@0
|
70 |
|
sl@0
|
71 |
If the longindex field is not NULL, then the integer pointed to by it will be set to the index of the long
|
sl@0
|
72 |
option relative to longopts.
|
sl@0
|
73 |
|
sl@0
|
74 |
The last element of the longopts array has to be filled with zeroes.
|
sl@0
|
75 |
|
sl@0
|
76 |
|
sl@0
|
77 |
|
sl@0
|
78 |
Examples:
|
sl@0
|
79 |
@code
|
sl@0
|
80 |
#include <unistd.h>
|
sl@0
|
81 |
#include <stdio.h>
|
sl@0
|
82 |
#include <fcntl.h>
|
sl@0
|
83 |
#include <getopt.h>
|
sl@0
|
84 |
#include <errno.h>
|
sl@0
|
85 |
int main()
|
sl@0
|
86 |
{
|
sl@0
|
87 |
int bflag, ch, fd;
|
sl@0
|
88 |
int daggerset;
|
sl@0
|
89 |
|
sl@0
|
90 |
int argc = 3;
|
sl@0
|
91 |
|
sl@0
|
92 |
char *argv[] = { "getopt", "--daggerset","hi" };
|
sl@0
|
93 |
|
sl@0
|
94 |
struct option longopts[] = {
|
sl@0
|
95 |
{ "buffy", no_argument, NULL, 'b' },
|
sl@0
|
96 |
{ "fluoride", required_argument, NULL, 'f' },
|
sl@0
|
97 |
{ "daggerset", no_argument, &daggerset;, 1 },
|
sl@0
|
98 |
{ NULL, 0, NULL, 0 }
|
sl@0
|
99 |
};
|
sl@0
|
100 |
|
sl@0
|
101 |
bflag = 0;
|
sl@0
|
102 |
|
sl@0
|
103 |
while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
|
sl@0
|
104 |
switch (ch) {
|
sl@0
|
105 |
|
sl@0
|
106 |
case 'b':
|
sl@0
|
107 |
printf("option is \"buffy\"
|
sl@0
|
108 |
");
|
sl@0
|
109 |
bflag = 1;
|
sl@0
|
110 |
break;
|
sl@0
|
111 |
|
sl@0
|
112 |
case 'f':
|
sl@0
|
113 |
printf("option is \"fluoride\"
|
sl@0
|
114 |
");
|
sl@0
|
115 |
if ((fd = open(optarg, O_RDONLY, 0)) == -1)
|
sl@0
|
116 |
printf("unable to open %s", optarg);
|
sl@0
|
117 |
break;
|
sl@0
|
118 |
|
sl@0
|
119 |
case 0:
|
sl@0
|
120 |
if (daggerset) {
|
sl@0
|
121 |
fprintf(stderr,"Buffy will use her dagger to apply
|
sl@0
|
122 |
fluoride to dracula's teeth
|
sl@0
|
123 |
");
|
sl@0
|
124 |
}
|
sl@0
|
125 |
break;
|
sl@0
|
126 |
default:
|
sl@0
|
127 |
printf("unknown option
|
sl@0
|
128 |
");
|
sl@0
|
129 |
|
sl@0
|
130 |
}//end of switch
|
sl@0
|
131 |
}//end of while
|
sl@0
|
132 |
|
sl@0
|
133 |
return 0;
|
sl@0
|
134 |
}
|
sl@0
|
135 |
|
sl@0
|
136 |
@endcode
|
sl@0
|
137 |
Output
|
sl@0
|
138 |
@code
|
sl@0
|
139 |
Buffy will use her dagger to apply fluoride to dracula's teeth
|
sl@0
|
140 |
|
sl@0
|
141 |
@endcode
|
sl@0
|
142 |
@see getopt()
|
sl@0
|
143 |
|
sl@0
|
144 |
|
sl@0
|
145 |
|
sl@0
|
146 |
|
sl@0
|
147 |
@publishedAll
|
sl@0
|
148 |
@externallyDefinedApi
|
sl@0
|
149 |
*/
|
sl@0
|
150 |
|
sl@0
|
151 |
/** @fn getopt(int argc, char * const argv[], const char *optstring)
|
sl@0
|
152 |
@param argc
|
sl@0
|
153 |
@param argv[]
|
sl@0
|
154 |
@param optstring
|
sl@0
|
155 |
@return The getopt function returns the next known option character in optstring. If getopt encounters a character not found in optstring or if it detects a missing option argument, it returns ' ? ' (question mark). If optstring has a leading ' : ' then a missing option argument causes ' : ' to be returned instead of ' ?. ' In either case, the variable optopt is set to the character that caused the error. The getopt function returns -1 when the argument list is exhausted.
|
sl@0
|
156 |
|
sl@0
|
157 |
The getopt function incrementally parses a command line argument list argv and returns the next known option character.
|
sl@0
|
158 |
An option character is known if it has been specified in the string of accepted option characters, optstring.
|
sl@0
|
159 |
|
sl@0
|
160 |
The option string optstring may contain the following elements: individual characters
|
sl@0
|
161 |
and characters followed by a colon to indicate an option argument is to follow.
|
sl@0
|
162 |
For example, an option string x recognizes an option "- x ", and an option string x: recognizes an option and argument "- x argument. " It does not matter to getopt if a following argument has leading white space.
|
sl@0
|
163 |
|
sl@0
|
164 |
On return from getopt, optarg points to an option argument, if it is anticipated,
|
sl@0
|
165 |
and the variable optind contains the index to the next argv argument for a subsequent call
|
sl@0
|
166 |
to getopt. The variable optopt saves the last known option character returned by getopt.
|
sl@0
|
167 |
|
sl@0
|
168 |
The variables opterr and optind are both initialized to 1.
|
sl@0
|
169 |
The optind variable may be set to another value before a set of calls to getopt in order to skip over more or less argv entries.
|
sl@0
|
170 |
|
sl@0
|
171 |
In order to use getopt to evaluate multiple sets of arguments, or to evaluate a single set of
|
sl@0
|
172 |
arguments multiple times,
|
sl@0
|
173 |
the variable optreset must be set to 1 before the second and each additional set of calls to getopt, and the variable optind must be reinitialized.
|
sl@0
|
174 |
|
sl@0
|
175 |
The getopt function returns -1 when the argument list is exhausted.
|
sl@0
|
176 |
The interpretation of options in the argument list may be cancelled
|
sl@0
|
177 |
by the option ' -- '
|
sl@0
|
178 |
(double dash) which causes getopt to signal the end of argument processing and return -1.
|
sl@0
|
179 |
When all options have been processed (i.e., up to the first non-option
|
sl@0
|
180 |
argument), getopt returns -1.
|
sl@0
|
181 |
|
sl@0
|
182 |
|
sl@0
|
183 |
|
sl@0
|
184 |
Examples:
|
sl@0
|
185 |
@code
|
sl@0
|
186 |
#include <unistd.h>
|
sl@0
|
187 |
#include <stdio.h>
|
sl@0
|
188 |
#include <fcntl.h>
|
sl@0
|
189 |
#include <errno.h>
|
sl@0
|
190 |
#include <string.h>
|
sl@0
|
191 |
|
sl@0
|
192 |
int main()
|
sl@0
|
193 |
{
|
sl@0
|
194 |
int argc = 3;
|
sl@0
|
195 |
|
sl@0
|
196 |
char *argv[] =
|
sl@0
|
197 |
{
|
sl@0
|
198 |
"getopt","-f","hi"
|
sl@0
|
199 |
};
|
sl@0
|
200 |
|
sl@0
|
201 |
int bflag, ch, fd;
|
sl@0
|
202 |
bflag = 0;
|
sl@0
|
203 |
|
sl@0
|
204 |
while ((ch = getopt(argc, argv, "bf:")) != -1) {
|
sl@0
|
205 |
|
sl@0
|
206 |
switch (ch) {
|
sl@0
|
207 |
case 'b':
|
sl@0
|
208 |
bflag = 1;
|
sl@0
|
209 |
printf("option is 'b' \n");
|
sl@0
|
210 |
break;
|
sl@0
|
211 |
case 'f':
|
sl@0
|
212 |
printf("option is 'f' \n");
|
sl@0
|
213 |
if ((fd = open(optarg, O_RDONLY, 0)) != 0) {
|
sl@0
|
214 |
(void)fprintf(stderr,
|
sl@0
|
215 |
"myname: %s: %s\n", optarg, strerror(errno));
|
sl@0
|
216 |
}
|
sl@0
|
217 |
break;
|
sl@0
|
218 |
case '?':
|
sl@0
|
219 |
printf("missing option!");
|
sl@0
|
220 |
default:
|
sl@0
|
221 |
printf("unknown option!");
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
}
|
sl@0
|
225 |
argc -= optind;
|
sl@0
|
226 |
return 0;
|
sl@0
|
227 |
}
|
sl@0
|
228 |
|
sl@0
|
229 |
|
sl@0
|
230 |
@endcode
|
sl@0
|
231 |
Output
|
sl@0
|
232 |
@code
|
sl@0
|
233 |
option is 'f'
|
sl@0
|
234 |
myname: hi: No such file or directory
|
sl@0
|
235 |
|
sl@0
|
236 |
@endcode
|
sl@0
|
237 |
Diagnostics:
|
sl@0
|
238 |
If the getopt function encounters a character not found in the string optstring or detects
|
sl@0
|
239 |
a missing option argument it writes an error message to the stderr and returns ' ?. '
|
sl@0
|
240 |
Setting opterr to a zero will disable these error messages.
|
sl@0
|
241 |
If optstring has a leading ' : '
|
sl@0
|
242 |
then a missing option argument causes a ' : '
|
sl@0
|
243 |
to be returned in addition to suppressing any error messages. Option arguments are allowed to begin with "-";
|
sl@0
|
244 |
this is reasonable but reduces the amount of error checking possible.
|
sl@0
|
245 |
@see getopt_long()
|
sl@0
|
246 |
|
sl@0
|
247 |
|
sl@0
|
248 |
Bugs:
|
sl@0
|
249 |
|
sl@0
|
250 |
The getopt function was once specified to return EOF instead of -1.
|
sl@0
|
251 |
This was changed by -p1003.2-92 to decouple getopt from \#include \< stdio.h \> A single dash "-" may be specified as a character in optstring, however it should never have an argument associated with it. This allows getopt to be used with programs that expect "-" as an option flag. This practice is wrong and should not be used in any
|
sl@0
|
252 |
current development. It is provided for backward compatibility only. Care should be taken not to use ' - ' as the first character in optstring to avoid a semantic conflict with GNU getopt, which assigns different meaning to an optstring that begins with a ' -. ' By default, a single dash causes getopt to return -1. It is also possible to handle digits as option letters.
|
sl@0
|
253 |
This allows getopt to be used with programs that expect a number ("-3")
|
sl@0
|
254 |
as an option.
|
sl@0
|
255 |
This practice is wrong, and should not be used in any current development.
|
sl@0
|
256 |
It is provided for backward compatibility only. The following code fragment works in most cases.
|
sl@0
|
257 |
@code
|
sl@0
|
258 |
int ch;
|
sl@0
|
259 |
long length;
|
sl@0
|
260 |
char *p, *ep;
|
sl@0
|
261 |
while ((ch = getopt(argc, argv, "0123456789")) != -1)
|
sl@0
|
262 |
switch (ch) {
|
sl@0
|
263 |
case ’0’: case ’1’: case ’2’: case ’3’: case ’4’:
|
sl@0
|
264 |
case ’5’: case ’6’: case ’7’: case ’8’: case ’9’:
|
sl@0
|
265 |
p = argv[optind - 1];
|
sl@0
|
266 |
if (p[0] == ’-’ Am]Am] p[1] == ch Am]Am] !p[2]) {
|
sl@0
|
267 |
length = ch - ’0’;
|
sl@0
|
268 |
ep = "";
|
sl@0
|
269 |
} else if (argv[optind] Am]Am] argv[optind][1] == ch) {
|
sl@0
|
270 |
length = strtol((p = argv[optind] + 1),
|
sl@0
|
271 |
Am]ep, 10);
|
sl@0
|
272 |
optind++;
|
sl@0
|
273 |
optreset = 1;
|
sl@0
|
274 |
} else
|
sl@0
|
275 |
usage();
|
sl@0
|
276 |
if (*ep != ’\0’)
|
sl@0
|
277 |
errx(EX_USAGE, "illegal number -- %s", p);
|
sl@0
|
278 |
break;
|
sl@0
|
279 |
}
|
sl@0
|
280 |
|
sl@0
|
281 |
|
sl@0
|
282 |
@endcode
|
sl@0
|
283 |
|
sl@0
|
284 |
|
sl@0
|
285 |
|
sl@0
|
286 |
@publishedAll
|
sl@0
|
287 |
@externallyDefinedApi
|
sl@0
|
288 |
*/
|
sl@0
|
289 |
|
sl@0
|
290 |
|
sl@0
|
291 |
/** @def optopt
|
sl@0
|
292 |
|
sl@0
|
293 |
getopt(3) external variables. character checked for validity.
|
sl@0
|
294 |
|
sl@0
|
295 |
@publishedAll
|
sl@0
|
296 |
@externallyDefinedApi
|
sl@0
|
297 |
*/
|
sl@0
|
298 |
|
sl@0
|
299 |
/** @def opterr
|
sl@0
|
300 |
|
sl@0
|
301 |
getopt(3) external variables. if error message should be printed.
|
sl@0
|
302 |
|
sl@0
|
303 |
@publishedAll
|
sl@0
|
304 |
@externallyDefinedApi
|
sl@0
|
305 |
*/
|
sl@0
|
306 |
|
sl@0
|
307 |
/** @def optind
|
sl@0
|
308 |
|
sl@0
|
309 |
getopt(3) external variables. index into parent argv vector.
|
sl@0
|
310 |
|
sl@0
|
311 |
@publishedAll
|
sl@0
|
312 |
@externallyDefinedApi
|
sl@0
|
313 |
*/
|
sl@0
|
314 |
|
sl@0
|
315 |
/** @def optarg
|
sl@0
|
316 |
|
sl@0
|
317 |
argument associated with option
|
sl@0
|
318 |
|
sl@0
|
319 |
@publishedAll
|
sl@0
|
320 |
@externallyDefinedApi
|
sl@0
|
321 |
*/
|
sl@0
|
322 |
|
sl@0
|
323 |
/** @def optreset
|
sl@0
|
324 |
|
sl@0
|
325 |
reset getopt
|
sl@0
|
326 |
|
sl@0
|
327 |
@publishedAll
|
sl@0
|
328 |
@externallyDefinedApi
|
sl@0
|
329 |
*/
|
sl@0
|
330 |
|
sl@0
|
331 |
/** @def no_argument
|
sl@0
|
332 |
|
sl@0
|
333 |
no argument required
|
sl@0
|
334 |
|
sl@0
|
335 |
@publishedAll
|
sl@0
|
336 |
@released
|
sl@0
|
337 |
*/
|
sl@0
|
338 |
|
sl@0
|
339 |
/** @def required_argument
|
sl@0
|
340 |
|
sl@0
|
341 |
argument is required
|
sl@0
|
342 |
|
sl@0
|
343 |
@publishedAll
|
sl@0
|
344 |
@released
|
sl@0
|
345 |
*/
|
sl@0
|
346 |
|
sl@0
|
347 |
/** @def optional_argument
|
sl@0
|
348 |
|
sl@0
|
349 |
argument is optional
|
sl@0
|
350 |
|
sl@0
|
351 |
@publishedAll
|
sl@0
|
352 |
@released
|
sl@0
|
353 |
*/
|
sl@0
|
354 |
|
sl@0
|
355 |
/** @struct option
|
sl@0
|
356 |
|
sl@0
|
357 |
Includes following members,
|
sl@0
|
358 |
|
sl@0
|
359 |
@publishedAll
|
sl@0
|
360 |
@released
|
sl@0
|
361 |
*/
|
sl@0
|
362 |
|
sl@0
|
363 |
/** @var option::name
|
sl@0
|
364 |
name of long option
|
sl@0
|
365 |
*/
|
sl@0
|
366 |
|
sl@0
|
367 |
/** @var option::has_arg
|
sl@0
|
368 |
one of no_argument, required_argument, and optional_argument: whether option takes an argument
|
sl@0
|
369 |
*/
|
sl@0
|
370 |
|
sl@0
|
371 |
/** @var option::flag
|
sl@0
|
372 |
if not NULL, set flag to val when option found
|
sl@0
|
373 |
*/
|
sl@0
|
374 |
|
sl@0
|
375 |
/** @var option::val
|
sl@0
|
376 |
if flag not NULL, value to set flag to; else return value
|
sl@0
|
377 |
*/
|