getopt.3

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 3 代码 · 共 136 行

3
136
字号
.\" SCCSID: @(#)getopt.3	8.1	9/11/90.TH getopt 3.SH Namegetopt \- get option letter from argument vector.SH Syntax.B #include <stdio.h>.br.B int getopt (argc, argv, optstring).br.B int argc;.br.B char \(**\(**argv;.br.B char \(**optstring;.PP.B extern char \(**optarg;.br.B extern int optind, opterr;.br.SH Description.NXR "getopt subroutine".NXR "argument vector" "getting option letter"The.PN getoptsubroutinereturns the next option letter in.I argv\^that matchesa letter in.IR optstring .The.I optstringis a string of recognized option letters;if a letter is followed by a colon, the optionis expected to have an argument that may ormay not be separated from it by white space.The.I optarg\^is set to point to the start of the option argumenton return from.PN getopt ..PPThe function.PN getopt\^places in.I optind\^the.I argv\^index of the next argument to be processed.The external variable optind is automatically initialized to 1 beforethe first call to .PN getopt ..PPWhen all options have been processed(that is, up to the first non-option argument),.PN getoptreturns EOF.The special option.B \-\-may be used to delimit the end of the options; EOFwill be returned, and.B \-\-will be skipped..SH DiagnosticsThe function.PN getopt\^prints an error message on.I stderr\^and returns aquestion mark.RB ( ? )when it encounters an option letter that is not included in.IR optstring .Setting opterr to 0 disables this error message..SH ExamplesThe following code fragment shows how one might process the argumentsfor a command that can take the mutually exclusive options.B aand.BR b ,and the options.B fand.BR o ,both of which require arguments:.EX 0#include <stdio.h>main (argc, argv)int argc;char \(**\(**argv;{       int c;       extern int optind, opterr;       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;                        bufsiza = 512;                        break;                case '?':                        errflg++;                }        if (errflg) {                fprintf (stderr, "usage: . . . ");                exit (2);        }        for ( ; optind < argc; optind++) {               if (access (argv[optind], 4)) {        .        .        .}.EE.SH See Alsogetopt(1)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?