Returns the next flag letter specified on the command line.
#include <unistd.h>
int getopt (ArgumentC, ArgumentV, OptionString) int ArgumentC; char *const ArgumentV [ ]; const char *OptionString;
extern int optind; extern int optopt; extern int opterr; extern char *optarg;
The optind parameter indexes the next element of the ArgumentV parameter to be processed. It is initialized to 1 and the getopt subroutine updates it after calling each element of the ArgumentV parameter.
The getopt subroutine returns the next flag letter in the ArgumentV parameter list that matches a letter in the OptionString parameter. If the flag takes an argument, the getopt subroutine sets the optarg parameter to point to the argument as follows:
The getopt subroutine returns the next flag letter specified on the command line. A value of -1 is returned when all command line flags have been parsed. When the value of the ArgumentV [optind] parameter is null, *ArgumentV [optind] is not the - (minus) character, or ArgumentV [optind] points to the "-" (minus) string, the getopt subroutine returns a value of -1 without changing the value. If ArgumentV [optind] points to the "- -" (double minus) string, the getopt subroutine returns a value of -1 after incrementing the value of the optind parameter.
If the getopt subroutine encounters an option character that is not specified by the OptionString parameter, a ? (question mark) character is returned. If it detects a missing option argument and the first character of OptionString is a : (colon), then a : (colon) character is returned. If this subroutine detects a missing option argument and the first character of OptionString is not a colon, it returns a ? (question mark). In either case, the getopt subroutine sets the optopt parameter to the option character that caused the error. If the application has not set the opterr parameter to 0 and the first character of OptionString is not a : (colon), the getopt subroutine also prints a diagnostic message to standard error.
The following code fragment processes the flags for a command that can take the mutually exclusive flags a and b, and the flags f and o, both of which require parameters.
#include <unistd.h> /*Needed for access subroutine constants*/ 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) { 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 '?': errflg++; } /* case */
if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } } /* while */
for ( ; optind < argc; optind++) { if (access(argv[optind], R_OK)) { . . . } } /* for */ } /* main */
This subroutine is part of Base Operating System (BOS) Runtime.
The getopt command.
List of Executable Program Creation Subroutines, Subroutines Overview, and List of Multithread Subroutines in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.