Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both |
#define _XOPEN_SOURCE
#include <stdio.h>
int getopt(int argc, char *const argv[], const char *optsting);
extern char *optarg;
extern int optind, opterr, optopt;
#define _XOPEN_SOURCE 600
#include <unistd.h>
int getopt(int argc, char *const argv[], const char *optsting);
extern char *optarg;
extern int optind, opterr, optopt;
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. If the application has not set the variable opterr to 0 and the first character of optstring is not a colon, getopt() also prints a diagnostic message to stderr in the format specified for the getopts utility.
Because the getopt() function returns thread-specific data the getopt() function can be used safely from a multithreaded application.
If successful, getopt() returns the value of the next option character from argv that matches a character in optstring.
A colon (:) is returned if getopt() detects a missing argument and the first character of optstring was a colon (:).
A question-mark (?) is returned if getopt() encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon (:).
Otherwise getopt() returns -1 when all command line arguments have been parsed or an unexpected error is encountered in the command line.
getopt() sets the external variables optind, optarg and optopt as described in the X/Open CAE Specification System Interfaces and Headers, Issue 4, Version 2 for the getopt() function.
int *__opindf(void);
int *__opoptf(void);
char **__opargf(void);
int *__operrf(void);
Also use these functions when you invoke getopt() in a DLL. These functions return a pointer to a thread-specific value for each variable.
getopt() does not return any errno values.
If getopt() detects a missing argument or an option character not in optstring it will write an error message to stderr describing the option character in error and the invoking program.