getopt.3 (7b4e5796b921fa69629efd1c167c072d1343b69e) | getopt.3 (662909a7800d5634772b89ca1509765dda837508) |
---|---|
1.\" Copyright (c) 1988, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. --- 15 unchanged lines hidden (view full) --- 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" | 1.\" Copyright (c) 1988, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. --- 15 unchanged lines hidden (view full) --- 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" |
32.\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | 32.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 |
33.\" | 33.\" |
34.Dd April 19, 1994 | 34.Dd April 27, 1995 |
35.Dt GETOPT 3 36.Os BSD 4.3 37.Sh NAME 38.Nm getopt 39.Nd get option character from command line argument list 40.Sh SYNOPSIS | 35.Dt GETOPT 3 36.Os BSD 4.3 37.Sh NAME 38.Nm getopt 39.Nd get option character from command line argument list 40.Sh SYNOPSIS |
41.Fd #include <stdlib.h> | 41.Fd #include <unistd.h> |
42.Vt extern char *optarg; 43.Vt extern int optind; 44.Vt extern int optopt; 45.Vt extern int opterr; 46.Vt extern int optreset; 47.Ft int 48.Fn getopt "int argc" "char * const *argv" "const char *optstring" 49.Sh DESCRIPTION --- 65 unchanged lines hidden (view full) --- 115.Fn getopt , 116and the variable 117.Va optind 118must be reinitialized. 119.Pp 120The 121.Fn getopt 122function | 42.Vt extern char *optarg; 43.Vt extern int optind; 44.Vt extern int optopt; 45.Vt extern int opterr; 46.Vt extern int optreset; 47.Ft int 48.Fn getopt "int argc" "char * const *argv" "const char *optstring" 49.Sh DESCRIPTION --- 65 unchanged lines hidden (view full) --- 115.Fn getopt , 116and the variable 117.Va optind 118must be reinitialized. 119.Pp 120The 121.Fn getopt 122function |
123returns an 124.Dv EOF 125when the argument list is exhausted, or 126.Ql ? 127if a non-recognized | 123returns \-1 124when the argument list is exhausted, or a non-recognized |
128option is encountered. | 125option is encountered. |
129The interpretation of options in the argument list may be canceled | 126The interpretation of options in the argument list may be cancelled |
130by the option 131.Ql -- 132(double dash) which causes 133.Fn getopt | 127by the option 128.Ql -- 129(double dash) which causes 130.Fn getopt |
134to signal the end of argument processing and return an 135.Dv EOF . | 131to signal the end of argument processing and returns \-1. |
136When all options have been processed (i.e., up to the first non-option 137argument), 138.Fn getopt | 132When all options have been processed (i.e., up to the first non-option 133argument), 134.Fn getopt |
139returns 140.Dv EOF . | 135returns \-1. |
141.Sh DIAGNOSTICS 142If the 143.Fn getopt 144function encounters a character not found in the string 145.Va optarg 146or detects | 136.Sh DIAGNOSTICS 137If the 138.Fn getopt 139function encounters a character not found in the string 140.Va optarg 141or detects |
147a missing option argument it writes an error message to the 148.Em stderr 149and returns 150.Ql ? . | 142a missing option argument it writes an error message and returns 143.Ql ? 144to the 145.Em stderr . |
151Setting 152.Va opterr 153to a zero will disable these error messages. 154If 155.Va optstring 156has a leading 157.Ql \&: 158then a missing option argument causes a --- 15 unchanged lines hidden (view full) --- 174specification. 175.Sh EXAMPLE 176.Bd -literal -compact 177extern char *optarg; 178extern int optind; 179int bflag, ch, fd; 180 181bflag = 0; | 146Setting 147.Va opterr 148to a zero will disable these error messages. 149If 150.Va optstring 151has a leading 152.Ql \&: 153then a missing option argument causes a --- 15 unchanged lines hidden (view full) --- 169specification. 170.Sh EXAMPLE 171.Bd -literal -compact 172extern char *optarg; 173extern int optind; 174int bflag, ch, fd; 175 176bflag = 0; |
182while ((ch = getopt(argc, argv, "bf:")) != EOF) | 177while ((ch = getopt(argc, argv, "bf:")) != -1) |
183 switch(ch) { 184 case 'b': 185 bflag = 1; 186 break; 187 case 'f': 188 if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 189 (void)fprintf(stderr, 190 "myname: %s: %s\en", optarg, strerror(errno)); --- 8 unchanged lines hidden (view full) --- 199argv += optind; 200.Ed 201.Sh HISTORY 202The 203.Fn getopt 204function appeared 205.Bx 4.3 . 206.Sh BUGS | 178 switch(ch) { 179 case 'b': 180 bflag = 1; 181 break; 182 case 'f': 183 if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 184 (void)fprintf(stderr, 185 "myname: %s: %s\en", optarg, strerror(errno)); --- 8 unchanged lines hidden (view full) --- 194argv += optind; 195.Ed 196.Sh HISTORY 197The 198.Fn getopt 199function appeared 200.Bx 4.3 . 201.Sh BUGS |
202The 203.Fn getopt 204function was once specified to return 205.Dv EOF 206instead of \-1. 207This was changed by 208.St -p1003.2-92 209to decouple 210.Fn getopt 211from 212.Pa <stdio.h> . 213.Pp |
|
207A single dash 208.Dq Li - 209may be specified as an character in 210.Fa optstring , 211however it should 212.Em never 213have an argument associated with it. 214This allows 215.Fn getopt 216to be used with programs that expect 217.Dq Li - 218as an option flag. 219This practice is wrong, and should not be used in any current development. 220It is provided for backward compatibility 221.Em only . 222By default, a single dash causes 223.Fn getopt | 214A single dash 215.Dq Li - 216may be specified as an character in 217.Fa optstring , 218however it should 219.Em never 220have an argument associated with it. 221This allows 222.Fn getopt 223to be used with programs that expect 224.Dq Li - 225as an option flag. 226This practice is wrong, and should not be used in any current development. 227It is provided for backward compatibility 228.Em only . 229By default, a single dash causes 230.Fn getopt |
224to return 225.Dv EOF . | 231to return \-1. |
226This is, we believe, compatible with System V. 227.Pp 228It is also possible to handle digits as option letters. 229This allows 230.Fn getopt 231to be used with programs that expect a number 232.Pq Dq Li \&-\&3 233as an option. 234This practice is wrong, and should not be used in any current development. 235It is provided for backward compatibility 236.Em only . 237The following code fragment works in most cases. 238.Bd -literal -offset indent 239int length; 240char *p; 241 | 232This is, we believe, compatible with System V. 233.Pp 234It is also possible to handle digits as option letters. 235This allows 236.Fn getopt 237to be used with programs that expect a number 238.Pq Dq Li \&-\&3 239as an option. 240This practice is wrong, and should not be used in any current development. 241It is provided for backward compatibility 242.Em only . 243The following code fragment works in most cases. 244.Bd -literal -offset indent 245int length; 246char *p; 247 |
242while ((c = getopt(argc, argv, "0123456789")) != EOF) | 248while ((c = getopt(argc, argv, "0123456789")) != -1) |
243 switch (c) { 244 case '0': case '1': case '2': case '3': case '4': 245 case '5': case '6': case '7': case '8': case '9': 246 p = argv[optind - 1]; 247 if (p[0] == '-' && p[1] == ch && !p[2]) 248 length = atoi(++p); 249 else 250 length = atoi(argv[optind] + 1); 251 break; 252 } 253} 254.Ed | 249 switch (c) { 250 case '0': case '1': case '2': case '3': case '4': 251 case '5': case '6': case '7': case '8': case '9': 252 p = argv[optind - 1]; 253 if (p[0] == '-' && p[1] == ch && !p[2]) 254 length = atoi(++p); 255 else 256 length = atoi(argv[optind] + 1); 257 break; 258 } 259} 260.Ed |