209 lines
5.3 KiB
HTML
209 lines
5.3 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
|
<title>getopt()--Get Flag Letters from Argument Vector</title>
|
|
<!-- Begin Header Records ========================================== -->
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<!-- Change History: -->
|
|
<!-- UNIX11 SCRIPT A converted by B2H R4.1 (346) (CMS) by V2DCIJB at -->
|
|
<!-- RCHVMW2 on 1 Jun 1999 at 16:14:12 -->
|
|
<!-- Edited by Kersten Feb 02 -->
|
|
<!-- This file has undergone html cleanup July 2002 by JET -->
|
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</head>
|
|
<body>
|
|
<!--End Header Records -->
|
|
<!-- Java sync-link -->
|
|
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
|
|
</script>
|
|
|
|
<a name="Top_Of_Page"></a>
|
|
|
|
<h2>getopt()--Get Flag Letters from Argument Vector</h2>
|
|
|
|
<div class="box" style="width: 70%;">
|
|
<br>
|
|
Syntax<br>
|
|
<pre>
|
|
#include <unistd.h>
|
|
|
|
int getopt(int <em>argc</em>, char * const <em>argv</em>[],
|
|
const char *<em>optionstring</em>);
|
|
|
|
</pre>
|
|
|
|
<br>
|
|
Service Program Name: QP0ZCPA<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Default Public Authority: *USE<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Threadsafe: No<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
</div>
|
|
|
|
<p>The <strong>getopt()</strong> function returns the next flag letter in the
|
|
<em>argv</em> list that matches a letter in <em>optionstring</em>. The
|
|
<em>optarg</em> external variable is set to point to the start of the flag's
|
|
parameter on return from <strong>getopt()</strong></p>
|
|
|
|
<p><strong>getopt()</strong> places the <em>argv</em> index of the next
|
|
argument to be processed in <em>optind</em>. The <em>optind</em> variable
|
|
is external. It is initialized to 1 before the first call to
|
|
<strong>getopt()</strong>.</p>
|
|
|
|
<p><strong>getopt()</strong> can be used to help a program interpret command
|
|
line flags that are passed to it.</p>
|
|
|
|
<br>
|
|
<h3>Parameters</h3>
|
|
|
|
<dl>
|
|
<dt><strong><em>argc</em></strong></dt>
|
|
|
|
<dd>(Input) The number of parameters passed by the function.<br>
|
|
<br>
|
|
</dd>
|
|
|
|
<dt><strong><em>argv</em></strong></dt>
|
|
|
|
<dd>(Input) The list of parameters passed to the function.<br>
|
|
<br>
|
|
</dd>
|
|
|
|
<dt><strong><em>optionstring</em></strong></dt>
|
|
|
|
<dd>(Input) A string of flag letters. The string must contain the flag letters
|
|
that the program using <strong>getopt()</strong> recognizes. If a letter is
|
|
followed by a colon, the flag is expected to have an argument or group of
|
|
arguments, which can be separated from it by blank spaces.
|
|
|
|
<p>The special flag "--" (two hyphens) can be used to delimit the end of the
|
|
options. When this flag is encountered, the "--" is skipped and EOF is
|
|
returned. This flag is useful in delimiting arguments beginning with a hyphen
|
|
that are not options.</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
<br>
|
|
<h3>Authorities</h3>
|
|
|
|
<p>None.</p>
|
|
|
|
<br>
|
|
<h3>Return Value</h3>
|
|
|
|
<table cellpadding="5">
|
|
<!-- cols="5 95" -->
|
|
<tr>
|
|
<td align="left" valign="top">EOF</td>
|
|
<td align="left" valign="top"><strong>getopt()</strong> processed all flags
|
|
(that is, up to the first argument that is not a flag).</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">'?'</td>
|
|
<td align="left" valign="top"><strong>getopt()</strong> encountered a flag
|
|
letter that was not included in <em>optionstring</em>. The variable
|
|
<em>optopt</em> is set to the real option found in <em>argv</em> regardless
|
|
of whether the flag is in <em>optionstring</em> of not. An error message is
|
|
printed to <samp>stderr</samp>. The generation of error messages can be suppressed
|
|
by setting <em>opterr</em> to 0.</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br>
|
|
<br>
|
|
|
|
<h3>Error Conditions</h3>
|
|
|
|
<p>The <strong>getopt()</strong> function does not return an error.</p>
|
|
|
|
<br>
|
|
<h3>Example</h3>
|
|
|
|
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
|
|
for information pertaining to code examples.</p>
|
|
|
|
<p>The following example processes the flags for a command that can take the
|
|
mutually exclusive flags a and b, and the flags f and o, both of which require
|
|
parameters.</p>
|
|
|
|
<pre>
|
|
#include <unistd.h>
|
|
|
|
int main( 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
|
|
bflg++;
|
|
break;
|
|
case 'f':
|
|
ifile = optarg;
|
|
break;
|
|
case 'o':
|
|
ofile = optarg;
|
|
break;
|
|
case '?':
|
|
errflg++;
|
|
} /* case */
|
|
if (errflg)
|
|
{
|
|
fprintf(stderr, "usage: . . . ");
|
|
exit(2);
|
|
}
|
|
} /* while */
|
|
for ( ; optind < argc; optind++)
|
|
{
|
|
if (access(argv[optind], R_OK))
|
|
{
|
|
.
|
|
.
|
|
.
|
|
}
|
|
} /* for */
|
|
} /* main */
|
|
</pre>
|
|
|
|
<br>
|
|
<hr>
|
|
API introduced: V3R6
|
|
|
|
<hr>
|
|
<center>
|
|
<table cellpadding="2" cellspacing="2">
|
|
<tr align="center">
|
|
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
|
|
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
</body>
|
|
</html>
|
|
|