|
|
coherent
getopt() General Function getopt()
Get option letter from argv
iinntt ggeettoopptt(_a_r_g_c, _a_r_g_v, _o_p_t_s_t_r_i_n_g)
iinntt _a_r_g_c;
cchhaarr **_a_r_g_v;
cchhaarr *_o_p_t_s_t_r_i_n_g;
eexxtteerrnn cchhaarr *_o_p_t_a_r_g;
eexxtteerrnn iinntt _o_p_t_i_n_d;
getopt returns the next option letter in argv that matches a let-
ter in optstring. optstring is a string of recognized option
letters. If a letter is followed by a colon, the option is ex-
pected to have an argument, which may or may not be separated
from it by white space. optarg is set to point to the start of
the option argument on return from getopt.
getopt places into optind the argv index of the next argument to
be processed. Because optind is external, it is normally in-
itialized to zero automatically before the first call to getopt.
When all options have been processed (i.e., up to the first non-
option argument), getopt returns EOF. The special option -- may
be used to delimit the end of the options: EOF will be returned,
and -- will be skipped.
***** Example *****
The following code fragment shows how one might process the ar-
guments for a command that can take the mutually exclusive op-
tions a and b, and the options f and o, both of which require ar-
guments:
main(argc, argv)
int argc;
char **argv;
{
int c;
extern int optind;
extern char *optarg;
.
.
.
while ((c = getopt(argc, argv, "abf:o:")) != EOF)
switch (c) {
COHERENT Lexicon Page 1
getopt() General Function getopt()
case 'a':
if (bflg)
errflg++;
else
aflg++;
break;
case 'b':
if (aflg)
errflg++;
else
bflg++;
break;
case 'f':
ifile = optarg;
break;
case 'o':
ofile = optarg;
break;
case '?':
default:
errflg++;
break;
}
if (errflg) {
fprintf(stderr, "Usage: ...");
exit(2);
}
for (; optind < argc; optind++) {
.
.
.
}
.
.
.
}
COHERENT Lexicon Page 2
getopt() General Function getopt()
***** See Also *****
general functions
***** Diagnostics *****
getopt prints an error message on stderr and returns a question
mark when it encounters an option letter not included in
optstring.
***** Notes *****
It is not obvious how `-' standing alone should be treated. This
version treats it as a non-option argument, which is not always
right.
Option arguments are allowed to begin with `-'. This is reason-
able, but reduces the amount of error checking possible.
COHERENT Lexicon Page 3
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.