GETOPT(3C) COMMAND REFERENCE GETOPT(3C) NAME getopt - get option letter from argument vector SYNOPSIS int getopt (argc, argv, optstring) int argc; char **argv; char *optstring; extern char *optarg; extern int optind; extern int opterr; extern int optopt; DESCRIPTION Getopt returns the next option letter in argv that matches a letter in optstring. Optstring is a string of recognized option letters; if a letter is followed by a colon, the option is expected to have an argument that 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 in optind the argv index of the next argument to be processed. Because optind is external, it is normally initialized to one automatically before the first call to getopt. When all options have been processed (for example, up to the first nonoption argument), getopt returns EOF. The special option -- (dash, dash) may be used to delimit the end of the options; EOF will be returned, and -- (dash, dash) will be skipped. The variable opterr determines whether or not getopt will print error messages itself. If set to 0, no messages are printed. Otherwise, getopt will print an error message for any unknown option or missing option argument. The variable optopt is set to the current option letter, which is the same value that getopt returns. EXAMPLES The following code fragment shows how you might process the arguments for a command that can take the mutually exclusive options a and b, and the options f and o, both of which require arguments: main (argc, argv) int argc; char **argv; { int c; Printed 5/12/88 1
GETOPT(3C) COMMAND REFERENCE GETOPT(3C) 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 bproc( ); break; case 'f': ifilename = optarg; break; case 'o': ofilename = optarg; bufsiza = 512; break; case '?': errflg++; } if (errflg) { fprintf (stderr, "usage: . . . "); exit (2); } for ( ; optind < argc; optind++) { if (access (argv[optind], 4)) { . . . } DIAGNOSTICS Getopt prints an error message on stderr and returns a question mark (?) when it encounters an option letter not included in optstring. CAVEATS The above routine uses <stdio.h>, which causes it to increase the size of programs, not otherwise using standard I/O, more than might be expected. Printed 5/12/88 2
GETOPT(3C) COMMAND REFERENCE GETOPT(3C) SEE ALSO getopt(1). Printed 5/12/88 3
%%index%% na:312,99; sy:411,1584; de:1995,2041; ex:4036,451;4871,1331; di:6202,422; ca:6624,332; se:7340,159; %%index%%000000000133