Commit | Line | Data |
---|---|---|
006fcfe1 KB |
1 | .\" Copyright (c) 1988 Regents of the University of California. |
2 | .\" All rights reserved. | |
53e9b2ee | 3 | .\" |
91cff1e1 | 4 | .\" %sccs.include.redist.man% |
006fcfe1 | 5 | .\" |
91cff1e1 | 6 | .\" @(#)getopt.3 6.15 (Berkeley) %G% |
53e9b2ee KM |
7 | .\" |
8 | .TH GETOPT 3 "" | |
9 | .UC 6 | |
10 | .SH NAME | |
11 | getopt \- get option letter from argv | |
12 | .SH SYNOPSIS | |
13 | .ft B | |
09885840 | 14 | .nf |
53e9b2ee | 15 | int getopt(argc, argv, optstring) |
53e9b2ee | 16 | int argc; |
53e9b2ee | 17 | char **argv; |
53e9b2ee KM |
18 | char *optstring; |
19 | .sp | |
20 | extern char *optarg; | |
53e9b2ee | 21 | extern int optind; |
0b64d222 | 22 | extern int opterr; |
53e9b2ee KM |
23 | .ft |
24 | .SH DESCRIPTION | |
25 | .I Getopt | |
26 | returns the next option letter in | |
27 | .I argv | |
28 | that matches a letter in | |
29 | .IR optstring . | |
30 | .I Optstring | |
09885840 KB |
31 | is a string of recognized option letters; if a letter is followed by a |
32 | colon, the option is expected to have an argument that may or may not | |
33 | be separated from it by white space. | |
53e9b2ee | 34 | .PP |
09885840 KB |
35 | On return from |
36 | .IR getopt , | |
37 | optarg is set to point to the start of any option argument. | |
38 | .I Optind | |
39 | contains the | |
53e9b2ee KM |
40 | .I argv |
41 | index of the next argument to be processed. | |
09885840 KB |
42 | .PP |
43 | .I Opterr | |
44 | and | |
53e9b2ee | 45 | .I optind |
09885840 | 46 | are both initialized to 1. |
8fa9b88f KB |
47 | In order to use |
48 | .I getopt | |
49 | to evaluate multiple sets of arguments, or to evaluate a single set of | |
50 | arguments multiple times, | |
51 | .I optind | |
52 | must be initialized to the number of argv entries to be skipped in each | |
53 | evaluation. | |
53e9b2ee | 54 | .PP |
09885840 KB |
55 | When all options have been processed (i.e., up to the first non-option |
56 | argument), | |
53e9b2ee | 57 | .I getopt |
09885840 KB |
58 | returns EOF. |
59 | The special option ``\-\-'' may be used to delimit the end of the options; | |
60 | EOF will be returned, and the ``\-\-'' will be skipped. | |
53e9b2ee KM |
61 | .SH DIAGNOSTICS |
62 | .I Getopt | |
63 | prints an error message on | |
64 | .I stderr | |
09885840 KB |
65 | and returns a question mark (``?'') when it encounters an option |
66 | letter not included in | |
67 | .IR optstring , | |
68 | or it encounters an option that requires an argument which is not | |
69 | supplied. | |
70 | Setting | |
71 | .I opterr | |
72 | to a zero will disable these error messages. | |
53e9b2ee | 73 | .SH EXAMPLE |
53e9b2ee | 74 | .nf |
09885840 KB |
75 | .in +5 |
76 | extern char *optarg; | |
77 | extern int optind; | |
78 | int bflag, ch, fd; | |
79 | ||
80 | bflag = 0; | |
81 | while ((ch = getopt(argc, argv, "bf:")) != EOF) | |
82 | switch(ch) { | |
83 | case 'b': | |
84 | bflag = 1; | |
85 | break; | |
86 | case 'f': | |
87 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | |
88 | (void)fprintf(stderr, | |
89 | "myname: unable to read file %s.\en", optarg); | |
90 | exit(1); | |
53e9b2ee | 91 | } |
09885840 KB |
92 | break; |
93 | case '?': | |
94 | default: | |
95 | usage(); | |
53e9b2ee | 96 | } |
09885840 KB |
97 | argc -= optind; |
98 | argv += optind; | |
99 | .fi | |
53e9b2ee | 100 | .SH BUGS |
09885840 KB |
101 | Option arguments are allowed to begin with ``\-''; this is reasonable but |
102 | reduces the amount of error checking possible. | |
53e9b2ee | 103 | .PP |
09885840 KB |
104 | A single dash (``-'') may be specified as an character in |
105 | .IR optstring , | |
106 | however it should | |
107 | .B never | |
108 | have an argument associated with it. | |
109 | This allows | |
110 | .I getopt | |
111 | to be used with programs that expect ``-'' as an option flag. | |
112 | This practice is wrong, and should not be used in any current development. | |
113 | It is provided for backward compatibility | |
114 | .BR only . | |
dd71e7d8 KB |
115 | By default, a single dash causes |
116 | .I getopt | |
117 | to return EOF. | |
118 | This is, we believe, compatible with System V. | |
09885840 KB |
119 | .PP |
120 | It is also possible to handle digits as option letters. | |
121 | This allows | |
122 | .I getopt | |
123 | to be used with programs that expect a number (``-3'') as an option. | |
124 | This practice is wrong, and should not be used in any current development. | |
125 | It is provided for backward compatibility | |
126 | .BR only . | |
127 | The following code fragment works fairly well. | |
128 | .sp | |
7edf3dad | 129 | .nf |
09885840 KB |
130 | .in +5 |
131 | int length; | |
132 | char *p; | |
7edf3dad | 133 | |
09885840 KB |
134 | while ((c = getopt(argc, argv, "0123456789")) != EOF) |
135 | switch (c) { | |
136 | case '0': case '1': case '2': case '3': case '4': | |
137 | case '5': case '6': case '7': case '8': case '9': | |
138 | p = argv[optind - 1]; | |
139 | if (p[0] == '-' && p[1] == ch && !p[2]) | |
140 | length = atoi(++p); | |
141 | else | |
142 | length = atoi(argv[optind] + 1); | |
143 | break; | |
7edf3dad | 144 | } |
09885840 | 145 | } |
7edf3dad | 146 | .fi |