Annotation of 43BSD/contrib/xns/examples/print/getopt.3, revision 1.1

1.1     ! root        1: .TH GETOPT 3 local
        !             2: .DA 25 March 1982
        !             3: .SH NAME
        !             4: getopt \- get option letter from argv
        !             5: .SH SYNOPSIS
        !             6: .ft B
        !             7: int getopt(argc, argv, optstring)
        !             8: .br
        !             9: int argc;
        !            10: .br
        !            11: char **argv;
        !            12: .br
        !            13: char *optstring;
        !            14: .sp
        !            15: extern char *optarg;
        !            16: .br
        !            17: extern int optind;
        !            18: .ft
        !            19: .SH DESCRIPTION
        !            20: .I Getopt
        !            21: returns the next option letter in
        !            22: .I argv
        !            23: that matches a letter in
        !            24: .IR optstring .
        !            25: .I Optstring
        !            26: is a string of recognized option letters;
        !            27: if a letter is followed by a colon, the option is expected to have
        !            28: an argument that may or may not be separated from it by white space.
        !            29: .I Optarg
        !            30: is set to point to the start of the option argument on return from
        !            31: .IR getopt .
        !            32: .PP
        !            33: .I Getopt
        !            34: places in
        !            35: .I optind
        !            36: the
        !            37: .I argv
        !            38: index of the next argument to be processed.
        !            39: Because
        !            40: .I optind
        !            41: is external, it is normally initialized to zero automatically
        !            42: before the first call to 
        !            43: .IR getopt .
        !            44: .PP
        !            45: When all options have been processed (i.e., up to the first
        !            46: non-option argument),
        !            47: .I getopt
        !            48: returns
        !            49: .BR EOF .
        !            50: The special option
        !            51: .B \-\-
        !            52: may be used to delimit the end of the options;
        !            53: .B EOF
        !            54: will be returned, and
        !            55: .B \-\-
        !            56: will be skipped.
        !            57: .SH SEE ALSO
        !            58: getopt(1)
        !            59: .SH DIAGNOSTICS
        !            60: .I Getopt
        !            61: prints an error message on
        !            62: .I stderr
        !            63: and returns a question mark
        !            64: .RB ( ? )
        !            65: when it encounters an option letter not included in
        !            66: .IR optstring .
        !            67: .SH EXAMPLE
        !            68: The following code fragment shows how one might process the arguments
        !            69: for a command that can take the mutually exclusive options
        !            70: .B a
        !            71: and
        !            72: .BR b ,
        !            73: and the options
        !            74: .B f
        !            75: and
        !            76: .BR o ,
        !            77: both of which require arguments:
        !            78: .PP
        !            79: .RS
        !            80: .nf
        !            81: main(argc, argv)
        !            82: int argc;
        !            83: char **argv;
        !            84: {
        !            85:        int c;
        !            86:        extern int optind;
        !            87:        extern char *optarg;
        !            88:        \&.
        !            89:        \&.
        !            90:        \&.
        !            91:        while ((c = getopt(argc, argv, "abf:o:")) != EOF)
        !            92:                switch (c) {
        !            93:                case 'a':
        !            94:                        if (bflg)
        !            95:                                errflg++;
        !            96:                        else
        !            97:                                aflg++;
        !            98:                        break;
        !            99:                case 'b':
        !           100:                        if (aflg)
        !           101:                                errflg++;
        !           102:                        else
        !           103:                                bproc();
        !           104:                        break;
        !           105:                case 'f':
        !           106:                        ifile = optarg;
        !           107:                        break;
        !           108:                case 'o':
        !           109:                        ofile = optarg;
        !           110:                        break;
        !           111:                case '?':
        !           112:                default:
        !           113:                        errflg++;
        !           114:                        break;
        !           115:                }
        !           116:        if (errflg) {
        !           117:                fprintf(stderr, "Usage: ...");
        !           118:                exit(2);
        !           119:        }
        !           120:        for (; optind < argc; optind++) {
        !           121:                \&.
        !           122:                \&.
        !           123:                \&.
        !           124:        }
        !           125:        \&.
        !           126:        \&.
        !           127:        \&.
        !           128: }
        !           129: .RE
        !           130: .PP
        !           131: A template similar to this can be found in
        !           132: .IR /usr/pub/template.c .
        !           133: .SH HISTORY
        !           134: Written by Henry Spencer, working from a Bell Labs manual page.
        !           135: Behavior believed identical to the Bell version.
        !           136: .SH BUGS
        !           137: It is not obvious how
        !           138: `\-'
        !           139: standing alone should be treated;  this version treats it as
        !           140: a non-option argument, which is not always right.
        !           141: .PP
        !           142: Option arguments are allowed to begin with `\-';
        !           143: this is reasonable but reduces the amount of error checking possible.
        !           144: .PP
        !           145: .I Getopt
        !           146: is quite flexible but the obvious price must be paid:  there is much
        !           147: it could do that it doesn't, like
        !           148: checking mutually exclusive options, checking type of
        !           149: option arguments, etc.

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.