?? optind.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>getopt</title></head><body bgcolor="white"><basefont size="3"> <a name="getopt"></a> <a name="tag_03_234"></a><!-- getopt --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_234_01"></a>NAME</h4><blockquote>getopt, optarg, opterr, optind, optopt - command option parsing</blockquote><h4><a name="tag_03_234_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br><br> int getopt(int</tt> <i>argc</i><tt>, char * const</tt> <i>argv</i><tt>[], const char *</tt><i>optstring</i><tt>);<br> extern char *optarg;<br> extern int optind, opterr, optopt;<br></tt></code></p></blockquote><h4><a name="tag_03_234_03"></a>DESCRIPTION</h4><blockquote><p>The <i>getopt</i>() function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 inthe Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2,Utility Syntax Guidelines</a>.</p><p>The parameters <i>argc</i> and <i>argv</i> are the argument count and argument array as passed to <i>main</i>() (see <a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> ). The argument <i>optstring</i> is a string of recognizedoption characters; if a character is followed by a colon, the option takes an argument. All option characters allowed by UtilitySyntax Guideline 3 are allowed in <i>optstring</i>. The implementation may accept other characters as an extension.</p><p>The variable <i>optind</i> is the index of the next element of the <i>argv</i>[] vector to be processed. It shall be initializedto 1 by the system, and <i>getopt</i>() shall update it when it finishes with each element of <i>argv</i>[]. When an element of<i>argv</i>[] contains multiple option characters, it is unspecified how <i>getopt</i>() determines which options have already beenprocessed.</p><p>The <i>getopt</i>() function shall return the next option character (if one is found) from <i>argv</i> that matches a characterin <i>optstring</i>, if there is one that matches. If the option takes an argument, <i>getopt</i>() shall set the variable<i>optarg</i> to point to the option-argument as follows:</p><ol><li><p>If the option was the last character in the string pointed to by an element of <i>argv</i>, then <i>optarg</i> shall contain thenext element of <i>argv</i>, and <i>optind</i> shall be incremented by 2. If the resulting value of <i>optind</i> is greater than<i>argc</i>, this indicates a missing option-argument, and <i>getopt</i>() shall return an error indication.</p></li><li><p>Otherwise, <i>optarg</i> shall point to the string following the option character in that element of <i>argv</i>, and<i>optind</i> shall be incremented by 1.</p></li></ol><p>If, when <i>getopt</i>() is called:</p><pre><i>argv</i><tt>[optind]</tt> is a null pointer<tt>*</tt>*<i>argv</i><tt>[optind]</tt> is not the character <tt>- </tt> <i>argv</i><tt>[optind]</tt> points to the string <tt>"-"</tt></pre><p><i>getopt</i>() shall return -1 without changing <i>optind</i>. If:</p><pre><i>argv</i><tt>[optind]</tt> points to the string <tt>"--"</tt></pre><p><i>getopt</i>() shall return -1 after incrementing <i>optind</i>.</p><p>If <i>getopt</i>() encounters an option character that is not contained in <i>optstring</i>, it shall return the question-mark (<tt>'?'</tt> ) character. If it detects a missing option-argument, it shall return the colon character ( <tt>':'</tt> ) if thefirst character of <i>optstring</i> was a colon, or a question-mark character ( <tt>'?'</tt> ) otherwise. In either case,<i>getopt</i>() shall set the variable <i>optopt</i> to the option character that caused the error. If the application has not setthe variable <i>opterr</i> to 0 and the first character of <i>optstring</i> is not a colon, <i>getopt</i>() shall also print adiagnostic message to <i>stderr</i> in the format specified for the <a href="../utilities/getopts.html"><i>getopts</i></a>utility.</p><p>The <i>getopt</i>() function need not be reentrant. A function that is not required to be reentrant is not required to bethread-safe.</p></blockquote><h4><a name="tag_03_234_04"></a>RETURN VALUE</h4><blockquote><p>The <i>getopt</i>() function shall return the next option character specified on the command line.</p><p>A colon ( <tt>':'</tt> ) shall be returned if <i>getopt</i>() detects a missing argument and the first character of<i>optstring</i> was a colon ( <tt>':'</tt> ).</p><p>A question mark ( <tt>'?'</tt> ) shall be returned if <i>getopt</i>() encounters an option character not in <i>optstring</i> ordetects a missing argument and the first character of <i>optstring</i> was not a colon ( <tt>':'</tt> ).</p><p>Otherwise, <i>getopt</i>() shall return -1 when all command line options are parsed.</p></blockquote><h4><a name="tag_03_234_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_234_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_234_06_01"></a>Parsing Command Line Options</h5><p>The following code fragment shows how you might process the arguments for a utility that can take the mutually-exclusive options<i>a</i> and <i>b</i> and the options <i>f</i> and <i>o</i>, both of which require arguments:</p><pre><tt>#include <unistd.h><br>intmain(int argc, char *argv[ ]){ int c; int bflg, aflg, errflg; char *ifile; char *ofile; extern char *optarg; extern int optind, optopt; . . . while ((c = getopt(argc, argv, ":abf:o:")) != -1) { switch(c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else { bflg++; bproc(); } break; case 'f': ifile = optarg; break; case 'o': ofile = optarg; break; case ':': /* -f or -o without operand */ fprintf(stderr, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflg++; } } if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } for ( ; optind < argc; optind++) { if (access(argv[optind], R_OK)) { . . .}</tt></pre><p>This code accepts any of the following as equivalent:</p><pre><tt>cmd -ao arg path pathcmd -a -o arg path pathcmd -o arg -a path pathcmd -a -o arg -- path pathcmd -a -oarg path pathcmd -aoarg path path</tt></pre><h5><a name="tag_03_234_06_02"></a>Checking Options and Arguments</h5><p>The following example parses a set of command line options and prints messages to standard output for each option and argumentthat it encounters.</p><pre><tt>#include <unistd.h>#include <stdio.h>...int c;char *filename;extern char *optarg;extern int optind, optopt, opterr;...while ((c = getopt(argc, argv, ":abf:")) != -1) { switch(c) { case 'a': printf("a is set\n"); break; case 'b': printf("b is set\n"); break; case 'f': filename = optarg; printf("filename is %s\n", filename); break; case ':': printf("-%c without filename\n", optopt); break; case '?': printf("unknown arg %c\n", optopt); break; }}</tt></pre><h5><a name="tag_03_234_06_03"></a>Selecting Options from the Command Line</h5><p>The following example selects the type of database routines the user wants to use based on the <i>Options</i> argument.</p><pre><tt>#include <unistd.h>#include <string.h>...char *Options = "hdbtl";...int dbtype, i;char c;char *st;...dbtype = 0;while ((c = getopt(argc, argv, Options)) != -1) { if ((st = strchr(Options, c)) != NULL) { dbtype = st - Options; break; }}</tt></pre></blockquote><h4><a name="tag_03_234_07"></a>APPLICATION USAGE</h4><blockquote><p>The <i>getopt</i>() function is only required to support option characters included in Utility Syntax Guideline 3. Manyhistorical implementations of <i>getopt</i>() support other characters as options. This is an allowed extension, but applicationsthat use extensions are not maximally portable. Note that support for multi-byte option characters is only possible when suchcharacters can be represented as type <b>int</b>.</p></blockquote><h4><a name="tag_03_234_08"></a>RATIONALE</h4><blockquote><p>The <i>optopt</i> variable represents historical practice and allows the application to obtain the identity of the invalidoption.</p><p>The description has been written to make it clear that <i>getopt</i>(), like the <a href="../utilities/getopts.html"><i>getopts</i></a> utility, deals with option-arguments whether separated from the option by<blank>s or not. Note that the requirements on <i>getopt</i>() and <a href="../utilities/getopts.html"><i>getopts</i></a> aremore stringent than the Utility Syntax Guidelines.</p><p>The <i>getopt</i>() function shall return -1, rather than EOF, so that <a href="../basedefs/stdio.h.html"><i><stdio.h></i></a> is not required.</p><p>The special significance of a colon as the first character of <i>optstring</i> makes <i>getopt</i>() consistent with the <ahref="../utilities/getopts.html"><i>getopts</i></a> utility. It allows an application to make a distinction between a missingargument and an incorrect option letter without having to examine the option letter. It is true that a missing argument can only bedetected in one case, but that is a case that has to be considered.</p></blockquote><h4><a name="tag_03_234_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_234_10"></a>SEE ALSO</h4><blockquote><p><a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a>, the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a>, the Shell and Utilities volume ofIEEE Std 1003.1-2001</p></blockquote><h4><a name="tag_03_234_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_234_12"></a>Issue 5</h4><blockquote><p>A note indicating that the <i>getopt</i>() function need not be reentrant is added to the DESCRIPTION.</p></blockquote><h4><a name="tag_03_234_13"></a>Issue 6</h4><blockquote><p>IEEE PASC Interpretation 1003.2 #150 is applied.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -