Manual Page Result
0
Command: getopt | Section: 3 | Source: UNIX v10 | File: getopt.3
GETOPT(3) Library Functions Manual GETOPT(3)
NAME
getopt - get option letter from argv
SYNOPSIS
int getopt (argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind;
extern int opterr;
DESCRIPTION
Getopt returns the next option letter in argv that matches a letter in
optstring. Optstring is a string of recognized option letters; if a
letter is followed by a colon, the option is expected to have an argu-
ment, which may or may not be separated from it by white space. Optarg
is set to point to the start of the option argument, if any.
Optind, initially 1, holds the index in argv of the next argument to be
processed. When opterr is nonzero (the default state), errors cause
diagnostic messages.
Option letters appear in nonempty clusters preceded by -. The special
option may be used to mark the end of the options.
EXAMPLES
This fragment processes arguments for a command that can take option a
and option f, which requires an argument.
main (argc, argv) char **argv;
{
int c, errflg = 0;
extern int optind;
extern char *optarg, *ifile;
while((c = getopt(argc, argv, "af:")) != -1)
switch (c){
case 'a': aflg=1; break;
case 'f': ifile = optarg; break;
case '?': errflg=1; break;
}
if(errflg){
fprintf(stderr, "usage: . . . ");
exit(2);
}
for( ; optind < argc; optind++){
if(access(argv[optind], 4)){
...
}
}
...
}
SEE ALSO
getflags(3)
DIAGNOSTICS
When all options have been processed, -1 is returned; optind refers to
the first non-option argument.
When getopt encounters an option letter not included in optstring or
finds an option argument missing, it prints a diagnostic on stderr un-
der control of opterr and returns a question mark
GETOPT(3)