1GETOPT(3)                  Linux Programmer's Manual                 GETOPT(3)
2
3
4

NAME

6       getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt -
7       Parse command-line options
8

SYNOPSIS

10       #include <unistd.h>
11
12       int getopt(int argc, char *const argv[],
13                  const char *optstring);
14
15       extern char *optarg;
16       extern int optind, opterr, optopt;
17
18       #include <getopt.h>
19
20       int getopt_long(int argc, char *const argv[],
21                  const char *optstring,
22                  const struct option *longopts, int *longindex);
23       int getopt_long_only(int argc, char *const argv[],
24                  const char *optstring,
25                  const struct option *longopts, int *longindex);
26
27   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
28
29       getopt():
30           _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE
31
32       getopt_long(), getopt_long_only():
33           _GNU_SOURCE
34

DESCRIPTION

36       The getopt() function parses the command-line arguments.  Its arguments
37       argc  and argv are the argument count and array as passed to the main()
38       function on program invocation.  An element of argv  that  starts  with
39       '-' (and is not exactly "-" or "--") is an option element.  The charac‐
40       ters of this element (aside from the initial '-')  are  option  charac‐
41       ters.   If  getopt() is called repeatedly, it returns successively each
42       of the option characters from each of the option elements.
43
44       The variable optind is the index of the next element to be processed in
45       argv.  The system initializes this value to 1.  The caller can reset it
46       to 1 to restart scanning of the same argv, or when scanning a new argu‐
47       ment vector.
48
49       If  getopt() finds another option character, it returns that character,
50       updating the external variable optind and a static variable nextchar so
51       that  the  next call to getopt() can resume the scan with the following
52       option character or argv-element.
53
54       If there are no more option  characters,  getopt()  returns  -1.   Then
55       optind  is  the  index in argv of the first argv-element that is not an
56       option.
57
58       optstring is a string containing the legitimate option  characters.   A
59       legitimate  option character is any visible one byte ascii(7) character
60       (for which isgraph(3) would return nonzero) that is not '-' or ':'.  If
61       such  a  character is followed by a colon, the option requires an argu‐
62       ment, so getopt() places a pointer to the following text  in  the  same
63       argv-element,  or  the  text  of the following argv-element, in optarg.
64       Two colons mean an option takes an optional arg; if there  is  text  in
65       the current argv-element (i.e., in the same word as the option name it‐
66       self, for example, "-oarg"), then it is returned in  optarg,  otherwise
67       optarg is set to zero.  This is a GNU extension.  If optstring contains
68       W followed by a semicolon, then -W foo is treated as  the  long  option
6