126 lines
4.6 KiB
HTML
126 lines
4.6 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>getopt(3)</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<H1>getopt(3)</H1>
|
|
<HR>
|
|
<PRE>
|
|
|
|
</PRE>
|
|
<H2>NAME</H2><PRE>
|
|
getopt - get option letter from argv
|
|
|
|
|
|
</PRE>
|
|
<H2>SYNOPSIS</H2><PRE>
|
|
<STRONG>int</STRONG> <STRONG>getopt(argc,</STRONG> <STRONG>argv,</STRONG> <STRONG>optstring)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>argc;</STRONG>
|
|
<STRONG>char</STRONG> <STRONG>**argv;</STRONG>
|
|
<STRONG>char</STRONG> <STRONG>*optstring;</STRONG>
|
|
|
|
<STRONG>extern</STRONG> <STRONG>char</STRONG> <STRONG>*optarg;</STRONG>
|
|
<STRONG>extern</STRONG> <STRONG>int</STRONG> <STRONG>optind;</STRONG>
|
|
|
|
|
|
</PRE>
|
|
<H2>DESCRIPTION</H2><PRE>
|
|
<EM>Getopt</EM> returns the next option letter in <EM>argv</EM> that matches a letter in
|
|
<EM>optstring</EM>. <EM>Optstring</EM> is a string of recognized option letters; if a
|
|
letter is followed by a colon, the option is expected to have an argument
|
|
that may or may not be separated from it by white space. <EM>Optarg</EM> is set
|
|
to point to the start of the option argument on return from <EM>getopt</EM>.
|
|
|
|
<EM>Getopt</EM> places in <EM>optind</EM> the <EM>argv</EM> index of the next argument to be
|
|
processed. Because <EM>optind</EM> is external, it is normally initialized to
|
|
zero automatically before the first call to <EM>getopt</EM>.
|
|
|
|
When all options have been processed (i.e., up to the first non-option
|
|
argument), <EM>getopt</EM> returns <STRONG>EOF</STRONG>. The special option <STRONG>--</STRONG> may be used to
|
|
delimit the end of the options; <STRONG>EOF</STRONG> will be returned, and <STRONG>--</STRONG> will be
|
|
skipped.
|
|
|
|
|
|
</PRE>
|
|
<H2>DIAGNOSTICS</H2><PRE>
|
|
<EM>Getopt</EM> prints an error message on <EM>stderr</EM> and returns a question mark (<STRONG>?</STRONG>)
|
|
when it encounters an option letter not included in <EM>optstring</EM>.
|
|
|
|
|
|
</PRE>
|
|
<H2>EXAMPLE</H2><PRE>
|
|
The following code fragment shows how one might process the arguments for
|
|
a command that can take the mutually exclusive options <STRONG>a</STRONG> and <STRONG>b</STRONG>, and the
|
|
options <STRONG>f</STRONG> and <STRONG>o</STRONG>, both of which require arguments:
|
|
|
|
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) {
|
|
case `a':
|
|
if (bflg)
|
|
errflg++;
|
|
else
|
|
aflg++;
|
|
break;
|
|
case `b':
|
|
if (aflg)
|
|
errflg++;
|
|
else
|
|
bproc();
|
|
break;
|
|
case `f':
|
|
ifile = optarg;
|
|
break;
|
|
case `o':
|
|
ofile = optarg;
|
|
break;
|
|
case `?':
|
|
default:
|
|
errflg++;
|
|
break;
|
|
}
|
|
if (errflg) {
|
|
fprintf(stderr, "Usage: ...");
|
|
<STRONG><A HREF="../man2/exit.2.html">exit(2)</A></STRONG>;
|
|
}
|
|
for (; optind < argc; optind++) {
|
|
.
|
|
.
|
|
.
|
|
}
|
|
.
|
|
.
|
|
.
|
|
}
|
|
|
|
|
|
</PRE>
|
|
<H2>HISTORY</H2><PRE>
|
|
Written by Henry Spencer, working from a Bell Labs manual page. Modified
|
|
by Keith Bostic to behave more like the System V version.
|
|
|
|
|
|
</PRE>
|
|
<H2>BUGS</H2><PRE>
|
|
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 reasonable but
|
|
reduces the amount of error checking possible.
|
|
|
|
<EM>Getopt</EM> is quite flexible but the obvious price must be paid: there is
|
|
much it could do that it doesn't, like checking mutually exclusive
|
|
options, checking type of option arguments, etc.
|
|
</PRE>
|
|
</BODY>
|
|
</HTML>
|