getopt()

parse command line 

Function


SYNOPSIS

#include <unistd.h>

extern char *optarg;

extern int optind, opterr, optopt

int getopt(int argc, char * const argv[], const char *optstring);


DESCRIPTION

The getopt() function is a command line parser. The parameters argc and argv are the argument count and argument array as passed to main() (see the exec() functions). The argument optstring is a string of allowed option characters; if a character is followed by a colon, the option takes an argument.

The variable optind is the index of the next element of argv to be processed. It is initialized to 1, and getopt() updates it as it processes each element of argv[].

The getopt() function returns the next option character (if one is found) from argv that matches a character in optstring, if any. If the option takes an argument (followed by a colon in optstring), getopt() sets the variable optarg to point to the option-argument as follows:


PARAMETERS

argc 

Is the number of arguments in command line.

argv 

Is an array of strings that holds the command line arguments.

optstring 

Points to a string of allowable option letters.


RETURN VALUES

The getopt() function returns the next option character specified on the command line.

When getopt() is called, if argv[optind] is a null pointer, *argv[optind] is not the character -, or argv[optind] points to the string "-", getopt() returns -1 without changing optind. If argv[optind] points to the string "--", getopt() returns -1 after incrementing optind.

If getopt() encounters an option character that is not contained in optstring, it returns the question-mark (?) character. If it detects a missing option-argument, it returns the colon character (:) if the first character of optstring was a colon, or a question-mark character (?) otherwise. In either case, getopt() sets the variable optopt to the option character that caused the error.

Otherwise getopt() returns -1 when all command line options are parsed.


CONFORMANCE

POSIX.2 (1992).


MULTITHREAD SAFETY LEVEL

Unsafe.


PORTING ISSUES

Do not declare any of the global variables in your code. Always include the appropriate PTC MKS Toolkit UNIX APIs header file.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
exec()


PTC MKS Toolkit 10.3 Documentation Build 39.