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