getopt() — Command option parsing

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE 
#include <stdio.h>
int getopt(int argc, char *const argv[], const char *optsting);

extern char *optarg; 
extern int optind, opterr, optopt;
SUSV3:
#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;

General description

The getopt() function is a command-line parser that can be used by applications that follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9 and 10 in X/Open CAE Specification, System Interface Definitions, Issue 4, Version 2 topic, Utility Syntax Guidelines. The getopt() function provides the identical functionality described in the X/Open CAE Specification System Interfaces and Headers, Issue 4, Version 2 for the getopt() function with the following extensions:
  • If the external variable optind is set to zero, the getopt() function treats this as an indication to restart the scan at the first byte of argv[1].

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.

Returned value

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.

The following functions defined in <stdio.h> should be used by multithreaded applications when attempting to reference or change the optind, optopt, optarg and opterr external variables:
 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.

Related information