

getopt()                 General Function                getopt()




Get option letter from argv

iinntt ggeettoopptt(_a_r_g_c, _a_r_g_v, _o_p_t_s_t_r_i_n_g)
iinntt _a_r_g_c;
cchhaarr **_a_r_g_v;
cchhaarr *_o_p_t_s_t_r_i_n_g;
eexxtteerrnn cchhaarr *_o_p_t_a_r_g;
eexxtteerrnn iinntt _o_p_t_i_n_d;

getopt returns the next option letter in argv that matches a let-
ter  in optstring.   optstring is a  string of  recognized option
letters.  If a  letter is followed by a colon,  the option is ex-
pected to  have an  argument, which may  or may not  be separated
from it by  white space.  optarg is set to  point to the start of
the option argument on return from getopt.

getopt places into optind the  argv index of the next argument to
be  processed.  Because  optind is external,  it is  normally in-
itialized to zero automatically before the first call to getopt.

When all options have been  processed (i.e., up to the first non-
option argument), getopt  returns EOF.  The special option -- may
be used to delimit the end  of the options: EOF will be returned,
and -- will be skipped.

***** Example *****

The following  code fragment shows how one  might process the ar-
guments for  a command that  can take the  mutually exclusive op-
tions a and b, and the options f and o, both of which require ar-
guments:


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) {





COHERENT Lexicon                                           Page 1




getopt()                 General Function                getopt()




                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 '?':
                default:
                        errflg++;
                        break;
                }



        if (errflg) {
                fprintf(stderr, "Usage: ...");
                exit(2);
        }



        for (; optind < argc; optind++) {
                .
                .
                .
        }
        .
        .
        .
}


COHERENT Lexicon                                           Page 2




getopt()                 General Function                getopt()





***** See Also *****

general functions

***** Diagnostics *****

getopt prints  an error message on stderr  and returns a question
mark  when  it  encounters  an  option  letter  not  included  in
optstring.

***** Notes *****

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 reason-
able, but reduces the amount of error checking possible.





































COHERENT Lexicon                                           Page 3


