:man| Alphabetical   Categories   About us 
 
GETOPT (3) | C library functions | Unix Manual Pages | :man

NAME

getopt - get option character from command line argument list

CONTENTS

Library
Synopsis
Description
Return Values
Examples
Diagnostics
See Also
Standards
History
Bugs

LIBRARY


.Lb libc

SYNOPSIS


.In unistd.h
.Vt extern char *optarg ;
.Vt extern int optind ;
.Vt extern int optopt ;
.Vt extern int opterr ;
.Vt extern int optreset ; int getopt "int argc" "char * const argv[]" "const char *optstring"

DESCRIPTION

The getopt function incrementally parses a command line argument list argv and returns the next known option character. An option character is known if it has been specified in the string of accepted option characters, optstring.

The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. For example, an option string "" "x" " recognizes an option "-x", and an option string "" "x:" " recognizes an option and argument "-x argument". It does not matter to getopt if a following argument has leading white space.

On return from getopt, optarg points to an option argument, if it is anticipated, and the variable optind contains the index to the next argv argument for a subsequent call to getopt. The variable optopt saves the last known option character returned by getopt.

The variables opterr and optind are both initialized to 1. The optind variable may be set to another value before a set of calls to getopt in order to skip over more or less argv entries.

In order to use getopt to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, the variable optreset must be set to 1 before the second and each additional set of calls to getopt, and the variable optind must be reinitialized.

The getopt function returns -1 when the argument list is exhausted. The interpretation of options in the argument list may be cancelled by the option ‘--’ (double dash) which causes getopt to signal the end of argument processing and return -1. When all options have been processed (i.e., up to the first non-option argument), getopt returns -1.

RETURN VALUES

The getopt function returns the next known option character in optstring. If getopt encounters a character not found in optstring or if it detects a missing option argument, it returns ‘?’ (question mark). If optstring has a leading ‘:’ then a missing option argument causes ‘:’ to be returned instead of ‘?’. In either case, the variable optopt is set to the character that caused the error. The getopt function returns -1 when the argument list is exhausted.

EXAMPLES


#include <unistd.h>
int bflag, ch, fd;


bflag = 0;
while ((ch = getopt(argc, argv, "bf:")) != -1) {
switch (ch) {
case ’b’:
bflag = 1;
break;
case ’f’:
if ((fd = open(optarg, O_RDONLY, 0)) Lt] 0) {
(void)fprintf(stderr,
"myname: %s: %s\n", optarg, strerror(errno));
exit(1);
}
break;
case ’?’:
default:
usage();
}
}
argc -= optind;
argv += optind;

DIAGNOSTICS

If the getopt function encounters a character not found in the string optstring or detects a missing option argument it writes an error message to the stderr and returns ‘?’. Setting opterr to a zero will disable these error messages. If optstring has a leading ‘:’ then a missing option argument causes a ‘:’ to be returned in addition to suppressing any error messages.

Option arguments are allowed to begin with "-"; this is reasonable but reduces the amount of error checking possible.

SEE ALSO

getopt(1), getopt_long(3), getsubopt(3)

STANDARDS

HISTORY

BUGS

 
Created by Blin Media, 2008-2013