getopt [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
getopt
Gets ASCII characters from an argument vector.
Syntax
int getopt (int argc, char *argv, char *optstring);
extern char *optarg;
extern int optind, opterr;
Parameters
argc An integer giving the length of the array argv.
argv A pointer to the command line.
optstring A string of recognized option letters.
Return Values
'?' An option letter is not included in optstring. This error
message can be disabled by setting opterr to zero.
EOF All options have been processed.
n The next option letter in argv, starting from argv[1] that
matches a letter in optstring.
optarg An external character pointer that is set to the start of
the option argument, if any, on return from getopt.
optind An external integer that should be initialized to one
before the first call to getopt. The optind value is set
to the argv index of the next argument to be processed on
return from getopt.
Description
The getopt function returns the next option letter in argv that matches a
letter in optstring. The optstring argument is a string of recognized
option letters. If a letter in optstring 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.
On return from getopt, optarg points to the start of the option argument.
The getopt function places in optind the argv index of the next argument
to be processed. The external variable optind is initialized to 1 before
the first call to getopt().
The external integer opterr enables or disables printing error messages
on the standard error device.
When all options have been processed (for example, up to the first
non-option argument), getopt returns EOF. The special option -- can be
used to delimit the end of the options. When this option is processed,
EOF is returned and optind is incremented to the argv index beyond --.
Options can be any ASCII characters except colon (:), question mark (?),
or null (\0). It is impossible to distinguish between a ? used as a
legal option, and the character that getopt returns when it encounters an
invalid option character in the input.
Set opterr to 0 to disable getopt from printing error messages on the
standard error device. Otherwise, getopt prints an error message on the
stderr and returns a question mark (?) when it encounters an option
letter not included in optstring.
Example
The following code fragment shows how you can 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;
extern char *optarg;
extern int optind;
:
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;
break;
case '?':
errflg++;
}
if (errflg) {
(void) fprintf(stderr, "usage: . . . ");
exit (2);
}
for ( ; optind< argc; optind++) {
if (access(argv[optind], 4)) {
:
}
MPE/iX 5.0 Documentation