1GETOPT(3) Library Functions Manual GETOPT(3)
2
3
4
6 getopt - get option character from command line argument list
7
9 #include <stdlib.h>
10
11 extern char *optarg;
12 extern int optind;
13 extern int optopt;
14 extern int opterr;
15 extern int optreset;
16
17 int
18 getopt(argc, argv, optstring)
19 int argc;
20 char **argv;
21 char *optstring;
22
24 The getopt() function incrementally parses a command line argument list
25 argv and returns the next known option character. An option character
26 is known if it has been specified in the string of accepted option
27 characters, optstring.
28
29 The option string optstring may contain the following elements: indi‐
30 vidual characters, and characters followed by a colon to indicate an
31 option argument is to follow. For example, an option string """x""
32 recognizes an option ``-x'', and an option string """x:"" recognizes an
33 option and argument ``-x argument''. It does not matter to getopt() if
34 a following argument has leading white space.
35
36 On return from getopt(), optarg points to an option argument, if it is
37 anticipated, and the variable optind contains the index to the next
38 argv argument for a subsequent call to getopt(). The variable optopt
39 saves the last known option character returned by getopt().
40
41 The variable opterr and optind are both initialized to 1. The optind
42 variable may be set to another value before a set of calls to getopt()
43 in order to skip over more or less argv entries.
44
45 In order to use getopt() to evaluate multiple sets of arguments, or to
46 evaluate a single set of arguments multiple times, the variable optre‐
47 set must be set to 1 before the second and each additional set of calls
48 to getopt(), and the variable optind must be reinitialized.
49
50 The getopt() function returns an EOF when the argument list is
51 exhausted, or a non-recognized option is encountered. The interpreta‐
52 tion of options in the argument list may be cancelled by the option
53 `--' (double dash) which causes getopt() to signal the end of argument
54 processing and return an EOF. When all options have been processed
55 (i.e., up to the first non-option argument), getopt() returns EOF.
56
58 If the getopt() function encounters a character not found in the string
59 optarg or detects a missing option argument it writes an error message
60 and returns `?' to the stderr. Setting opterr to a zero will disable
61 these error messages. If optstring has a leading `:' then a missing
62 option argument causes a `:' to be returned in addition to suppressing
63 any error messages.
64
65 Option arguments are allowed to begin with `-'; this is reasonable but
66 reduces the amount of error checking possible.
67
69 The optreset variable was added to make it possible to call the
70 getopt() function multiple times. This is an extension to the IEEE
71 Std1003.2 (``POSIX'') specification.
72
74 extern char *optarg;
75 extern int optind;
76 int bflag, ch, fd;
77
78 bflag = 0;
79 while ((ch = getopt(argc, argv, "bf:")) != EOF)
80 switch(ch) {
81 case 'b':
82 bflag = 1;
83 break;
84 case 'f':
85 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
86 (void)fprintf(stderr,
87 "myname: %s: %s\n", optarg, strerror(errno));
88 exit(1);
89 }
90 break;
91 case '?':
92 default:
93 usage();
94 }
95 argc -= optind;
96 argv += optind;
97
99 The getopt() function appeared 4.3BSD.
100
102 A single dash ``-'' may be specified as an character in optstring ,
103 however it should never have an argument associated with it. This
104 allows getopt() to be used with programs that expect ``-'' as an option
105 flag. This practice is wrong, and should not be used in any current
106 development. It is provided for backward compatibility only . By
107 default, a single dash causes getopt() to return EOF. This is, we
108 believe, compatible with System V.
109
110 It is also possible to handle digits as option letters. This allows
111 getopt() to be used with programs that expect a number (``-3'') as an
112 option. This practice is wrong, and should not be used in any current
113 development. It is provided for backward compatibility only. The fol‐
114 lowing code fragment works in most cases.
115
116 int length;
117 char *p;
118
119 while ((c = getopt(argc, argv, "0123456789")) != EOF)
120 switch (c) {
121 case '0': case '1': case '2': case '3': case '4':
122 case '5': case '6': case '7': case '8': case '9':
123 p = argv[optind - 1];
124 if (p[0] == '-' && p[1] == ch && !p[2])
125 length = atoi(++p);
126 else
127 length = atoi(argv[optind] + 1);
128 break;
129 }
130 }
131
132
133
1344.3 Berkeley Distribution January 12, 1996 GETOPT(3)