1GETOPT(3P)                 POSIX Programmer's Manual                GETOPT(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       getopt, optarg, opterr, optind, optopt - command option parsing
13

SYNOPSIS

15       #include <unistd.h>
16
17       int getopt(int argc, char * const argv[], const char *optstring);
18       extern char *optarg;
19       extern int optind, opterr, optopt;
20
21

DESCRIPTION

23       The getopt() function is a command-line parser that shall follow  Util‐
24       ity  Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in the Base Definitions
25       volume of IEEE Std 1003.1-2001, Section  12.2,  Utility  Syntax  Guide‐
26       lines.
27
28       The  parameters argc and argv are the argument count and argument array
29       as passed to main() (see exec()). The argument optstring is a string of
30       recognized  option  characters;  if a character is followed by a colon,
31       the option takes an argument. All option characters allowed by  Utility
32       Syntax  Guideline  3  are  allowed in optstring. The implementation may
33       accept other characters as an extension.
34
35       The variable optind is the index of the next element of the argv[] vec‐
36       tor  to  be  processed. It shall be initialized to 1 by the system, and
37       getopt() shall update it when it finishes with each element of  argv[].
38       When  an  element  of argv[] contains multiple option characters, it is
39       unspecified how getopt() determines which  options  have  already  been
40       processed.
41
42       The getopt() function shall return the next option character (if one is
43       found) from argv that matches a character in optstring, if there is one
44       that  matches.  If the option takes an argument, getopt() shall set the
45       variable optarg to point to the option-argument as follows:
46
47        1. If the option was the last character in the string pointed to by an
48           element  of  argv,  then  optarg  shall contain the next element of
49           argv, and optind shall be incremented by 2. If the resulting  value
50           of  optind  is  greater than argc, this indicates a missing option-
51           argument, and getopt() shall return an error indication.
52
53        2. Otherwise, optarg shall point to the string  following  the  option
54           character  in that element of argv, and optind shall be incremented
55           by 1.
56
57       If, when getopt() is called:
58
59
60              argv[optind]  is a null pointer*
61              argv[optind]  is not the character -
62              argv[optind]  points to the string "-"
63
64       getopt() shall return -1 without changing optind. If:
65
66
67              argv[optind]   points to the string "--"
68
69       getopt() shall return -1 after incrementing optind.
70
71       If getopt() encounters an option character that  is  not  contained  in
72       optstring,  it  shall return the question-mark ( '?' ) character. If it
73       detects a missing option-argument, it shall return the colon  character
74       ( ':' ) if the first character of optstring was a colon, or a question-
75       mark character ( '?' ) otherwise. In either case,  getopt()  shall  set
76       the  variable  optopt to the option character that caused the error. If
77       the application has not set the variable opterr  to  0  and  the  first
78       character  of  optstring  is  not  a colon, getopt() shall also print a
79       diagnostic message to stderr in the format specified  for  the  getopts
80       utility.
81
82       The  getopt()  function  need  not be reentrant. A function that is not
83       required to be reentrant is not required to be thread-safe.
84

RETURN VALUE

86       The getopt() function shall return the next option character  specified
87       on the command line.
88
89       A  colon  ( ':' ) shall be returned if getopt() detects a missing argu‐
90       ment and the first character of optstring was a colon ( ':' ).
91
92       A question mark ( '?' ) shall be returned  if  getopt()  encounters  an
93       option character not in optstring or detects a missing argument and the
94       first character of optstring was not a colon ( ':' ).
95
96       Otherwise, getopt() shall return -1 when all command line  options  are
97       parsed.
98

ERRORS

100       No errors are defined.
101
102       The following sections are informative.
103

EXAMPLES

105   Parsing Command Line Options
106       The  following  code fragment shows how you might process the arguments
107       for a utility that can take the mutually-exclusive options a and b  and
108       the options f and o, both of which require arguments:
109
110
111              #include <unistd.h>
112
113
114              int
115              main(int argc, char *argv[ ])
116              {
117                  int c;
118                  int bflg, aflg, errflg;
119                  char *ifile;
120                  char *ofile;
121                  extern char *optarg;
122                  extern int optind, optopt;
123                  . . .
124                  while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
125                      switch(c) {
126                      case 'a':
127                          if (bflg)
128                              errflg++;
129                          else
130                              aflg++;
131                          break;
132                      case 'b':
133                          if (aflg)
134                              errflg++;
135                          else {
136                              bflg++;
137                              bproc();
138                          }
139                          break;
140                      case 'f':
141                          ifile = optarg;
142                          break;
143                      case 'o':
144                          ofile = optarg;
145                          break;
146                          case ':':       /* -f or -o without operand */
147                                  fprintf(stderr,
148                                          "Option -%c requires an operand\n", optopt);
149                                  errflg++;
150                                  break;
151                      case '?':
152                                  fprintf(stderr,
153                                          "Unrecognized option: -%c\n", optopt);
154                          errflg++;
155                      }
156                  }
157                  if (errflg) {
158                      fprintf(stderr, "usage: . . . ");
159                      exit(2);
160                  }
161                  for ( ; optind < argc; optind++) {
162                      if (access(argv[optind], R_OK)) {
163                  . . .
164              }
165
166       This code accepts any of the following as equivalent:
167
168
169              cmd -ao arg path path
170              cmd -a -o arg path path
171              cmd -o arg -a path path
172              cmd -a -o arg -- path path
173              cmd -a -oarg path path
174              cmd -aoarg path path
175
176   Checking Options and Arguments
177       The  following  example parses a set of command line options and prints
178       messages to standard output  for  each  option  and  argument  that  it
179       encounters.
180
181
182              #include <unistd.h>
183              #include <stdio.h>
184              ...
185              int c;
186              char *filename;
187              extern char *optarg;
188              extern int optind, optopt, opterr;
189              ...
190              while ((c = getopt(argc, argv, ":abf:")) != -1) {
191                  switch(c) {
192                  case 'a':
193                      printf("a is set\n");
194                      break;
195                  case 'b':
196                      printf("b is set\n");
197                      break;
198                  case 'f':
199                      filename = optarg;
200                      printf("filename is %s\n", filename);
201                      break;
202                  case ':':
203                      printf("-%c without filename\n", optopt);
204                      break;
205                  case '?':
206                      printf("unknown arg %c\n", optopt);
207                      break;
208                  }
209              }
210
211   Selecting Options from the Command Line
212       The  following  example  selects the type of database routines the user
213       wants to use based on the Options argument.
214
215
216              #include <unistd.h>
217              #include <string.h>
218              ...
219              char *Options = "hdbtl";
220              ...
221              int dbtype, i;
222              char c;
223              char *st;
224              ...
225              dbtype = 0;
226              while ((c = getopt(argc, argv, Options)) != -1) {
227                  if ((st = strchr(Options, c)) != NULL) {
228                      dbtype = st - Options;
229                      break;
230                  }
231              }
232

APPLICATION USAGE

234       The getopt() function is only required  to  support  option  characters
235       included in Utility Syntax Guideline 3. Many historical implementations
236       of getopt() support other characters as options.  This  is  an  allowed
237       extension,  but applications that use extensions are not maximally por‐
238       table. Note that support for multi-byte option characters is only  pos‐
239       sible when such characters can be represented as type int.
240

RATIONALE

242       The  optopt  variable  represents  historical  practice  and allows the
243       application to obtain the identity of the invalid option.
244
245       The description has been written to make it clear that  getopt(),  like
246       the getopts utility, deals with option-arguments whether separated from
247       the option by <blank>s or not. Note that the requirements  on  getopt()
248       and getopts are more stringent than the Utility Syntax Guidelines.
249
250       The  getopt()  function  shall  return  -1,  rather  than  EOF, so that
251       <stdio.h> is not required.
252
253       The special significance of a colon as the first character of optstring
254       makes getopt() consistent with the getopts utility. It allows an appli‐
255       cation to make a distinction between a missing argument and  an  incor‐
256       rect  option  letter without having to examine the option letter. It is
257       true that a missing argument can only be detected in one case, but that
258       is a case that has to be considered.
259

FUTURE DIRECTIONS

261       None.
262

SEE ALSO

264       exec(),   the   Base   Definitions   volume   of  IEEE Std 1003.1-2001,
265       <unistd.h>, the Shell and Utilities volume of IEEE Std 1003.1-2001
266
268       Portions of this text are reprinted and reproduced in  electronic  form
269       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
270       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
271       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
272       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
273       event of any discrepancy between this version and the original IEEE and
274       The Open Group Standard, the original IEEE and The Open Group  Standard
275       is  the  referee document. The original Standard can be obtained online
276       at http://www.opengroup.org/unix/online.html .
277
278
279
280IEEE/The Open Group                  2003                           GETOPT(3P)
Impressum