1 .\" Copyright (c) 1985 Regents of the University of California.
2 .\" All rights reserved. The Berkeley software License Agreement
3 .\" specifies the terms and conditions for redistribution.
5 .\" @(#)getopt.3 6.4 (Berkeley) 5/27/86
7 .TH GETOPT 3 "May 27, 1986"
10 getopt \- get option letter from argv
13 int getopt(argc, argv, optstring)
27 returns the next option letter in
29 that matches a letter in
32 is a string of recognized option letters;
33 if a letter is followed by a colon, the option is expected to have
34 an argument that may or may not be separated from it by white space.
36 is set to point to the start of the option argument on return from
44 index of the next argument to be processed.
47 is external, it is normally initialized to zero automatically
48 before the first call to
51 When all options have been processed (i.e., up to the first
58 may be used to delimit the end of the options;
65 prints an error message on
67 and returns a question mark
69 when it encounters an option letter not included in
72 The following code fragment shows how one might process the arguments
73 for a command that can take the mutually exclusive options
81 both of which require arguments:
95 while ((c = getopt(argc, argv, "abf:o:")) != EOF)
121 fprintf(stderr, "Usage: ...");
124 for (; optind < argc; optind++) {
135 Written by Henry Spencer, working from a Bell Labs manual page.
136 Modified by Keith Bostic to behave more like the System V version.
138 It is not obvious how
140 standing alone should be treated; this version treats it as
141 a non-option argument, which is not always right.
143 Option arguments are allowed to begin with `\-';
144 this is reasonable but reduces the amount of error checking possible.
147 is quite flexible but the obvious price must be paid: there is much
148 it could do that it doesn't, like
149 checking mutually exclusive options, checking type of
150 option arguments, etc.