xref: /minix3/lib/libc/stdlib/getopt.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: getopt.3,v 1.34 2014/06/05 22:09:50 wiz Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1988, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
312fe8fb19SBen Gras.\"
32*0a6a1f1dSLionel Sambuc.Dd June 5, 2014
332fe8fb19SBen Gras.Dt GETOPT 3
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm getopt
372fe8fb19SBen Gras.Nd get option character from command line argument list
382fe8fb19SBen Gras.Sh LIBRARY
392fe8fb19SBen Gras.Lb libc
402fe8fb19SBen Gras.Sh SYNOPSIS
412fe8fb19SBen Gras.In unistd.h
422fe8fb19SBen Gras.Vt extern char *optarg;
432fe8fb19SBen Gras.Vt extern int   optind;
442fe8fb19SBen Gras.Vt extern int   optopt;
452fe8fb19SBen Gras.Vt extern int   opterr;
462fe8fb19SBen Gras.Vt extern int   optreset;
472fe8fb19SBen Gras.Ft int
482fe8fb19SBen Gras.Fn getopt "int argc" "char * const argv[]" "const char *optstring"
492fe8fb19SBen Gras.Sh DESCRIPTION
502fe8fb19SBen GrasThe
512fe8fb19SBen Gras.Fn getopt
522fe8fb19SBen Grasfunction incrementally parses a command line argument list
532fe8fb19SBen Gras.Fa argv
542fe8fb19SBen Grasand returns the next
552fe8fb19SBen Gras.Em known
562fe8fb19SBen Grasoption character.
572fe8fb19SBen GrasAn option character is
582fe8fb19SBen Gras.Em known
592fe8fb19SBen Grasif it has been specified in the string of accepted option characters,
602fe8fb19SBen Gras.Fa optstring .
612fe8fb19SBen Gras.Pp
622fe8fb19SBen GrasThe option string
632fe8fb19SBen Gras.Fa optstring
642fe8fb19SBen Grasmay contain the following elements: individual characters, and
652fe8fb19SBen Grascharacters followed by a colon to indicate an option argument
662fe8fb19SBen Grasis to follow.
67*0a6a1f1dSLionel SambucIf an individual character is followed by two colons, then the
68*0a6a1f1dSLionel Sambucoption argument is optional;
69*0a6a1f1dSLionel Sambuc.Va optarg
70*0a6a1f1dSLionel Sambucis set to the rest of the current
71*0a6a1f1dSLionel Sambuc.Va argv
72*0a6a1f1dSLionel Sambucword, or
73*0a6a1f1dSLionel Sambuc.Dv NULL
74*0a6a1f1dSLionel Sambucif there were no more characters in the current word.
75*0a6a1f1dSLionel SambucThis is a
76*0a6a1f1dSLionel Sambuc.Nx
77*0a6a1f1dSLionel Sambucextension.
782fe8fb19SBen GrasFor example, an option string
792fe8fb19SBen Gras.Qq x
802fe8fb19SBen Grasrecognizes an option
812fe8fb19SBen Gras.Dq Fl x ,
822fe8fb19SBen Grasand an option string
832fe8fb19SBen Gras.Qq x:
842fe8fb19SBen Grasrecognizes an option and argument
852fe8fb19SBen Gras.Dq Fl x Ar argument .
862fe8fb19SBen GrasIt does not matter to
872fe8fb19SBen Gras.Fn getopt
882fe8fb19SBen Grasif a following argument has leading whitespace.
892fe8fb19SBen Gras.Pp
902fe8fb19SBen GrasOn return from
912fe8fb19SBen Gras.Fn getopt ,
922fe8fb19SBen Gras.Va optarg
932fe8fb19SBen Graspoints to an option argument, if it is anticipated,
942fe8fb19SBen Grasand the variable
952fe8fb19SBen Gras.Va optind
962fe8fb19SBen Grascontains the index to the next
972fe8fb19SBen Gras.Fa argv
982fe8fb19SBen Grasargument for a subsequent call
992fe8fb19SBen Grasto
1002fe8fb19SBen Gras.Fn getopt .
1012fe8fb19SBen GrasThe variable
1022fe8fb19SBen Gras.Va optopt
1032fe8fb19SBen Grassaves the last
1042fe8fb19SBen Gras.Em known
1052fe8fb19SBen Grasoption character returned by
1062fe8fb19SBen Gras.Fn getopt .
1072fe8fb19SBen Gras.Pp
1082fe8fb19SBen GrasThe variables
1092fe8fb19SBen Gras.Va opterr
1102fe8fb19SBen Grasand
1112fe8fb19SBen Gras.Va optind
1122fe8fb19SBen Grasare both initialized to 1.
1132fe8fb19SBen GrasThe
1142fe8fb19SBen Gras.Va optind
1152fe8fb19SBen Grasvariable may be set to another value before a set of calls to
1162fe8fb19SBen Gras.Fn getopt
1172fe8fb19SBen Grasin order to skip over more or less argv entries.
1182fe8fb19SBen Gras.Pp
1192fe8fb19SBen GrasIn order to use
1202fe8fb19SBen Gras.Fn getopt
1212fe8fb19SBen Grasto evaluate multiple sets of arguments, or to evaluate a single set of
1222fe8fb19SBen Grasarguments multiple times,
1232fe8fb19SBen Grasthe variable
1242fe8fb19SBen Gras.Va optreset
1252fe8fb19SBen Grasmust be set to 1 before the second and each additional set of calls to
1262fe8fb19SBen Gras.Fn getopt ,
1272fe8fb19SBen Grasand the variable
1282fe8fb19SBen Gras.Va optind
1292fe8fb19SBen Grasmust be reinitialized.
1302fe8fb19SBen Gras.Pp
1312fe8fb19SBen GrasThe
1322fe8fb19SBen Gras.Fn getopt
1332fe8fb19SBen Grasfunction returns \-1 when the argument list is exhausted.
1342fe8fb19SBen GrasThe interpretation of options in the argument list may be cancelled
1352fe8fb19SBen Grasby the option
1362fe8fb19SBen Gras.Dq --
1372fe8fb19SBen Gras(double dash) which causes
1382fe8fb19SBen Gras.Fn getopt
1392fe8fb19SBen Grasto signal the end of argument processing and return \-1.
1402fe8fb19SBen GrasWhen all options have been processed (i.e., up to the first non-option
1412fe8fb19SBen Grasargument),
1422fe8fb19SBen Gras.Fn getopt
1432fe8fb19SBen Grasreturns \-1.
1442fe8fb19SBen Gras.Sh RETURN VALUES
1452fe8fb19SBen GrasThe
1462fe8fb19SBen Gras.Fn getopt
1472fe8fb19SBen Grasfunction returns the next known option character in
1482fe8fb19SBen Gras.Fa optstring .
1492fe8fb19SBen GrasIf
1502fe8fb19SBen Gras.Fn getopt
1512fe8fb19SBen Grasencounters a character not found in
1522fe8fb19SBen Gras.Fa optstring
1532fe8fb19SBen Grasor if it detects a missing option argument,
1542fe8fb19SBen Grasit returns
1552fe8fb19SBen Gras.Sq \&?
1562fe8fb19SBen Gras(question mark).
1572fe8fb19SBen GrasIf
1582fe8fb19SBen Gras.Fa optstring
1592fe8fb19SBen Grashas a leading
1602fe8fb19SBen Gras.Sq \&:
1612fe8fb19SBen Grasthen a missing option argument causes
1622fe8fb19SBen Gras.Sq \&:
1632fe8fb19SBen Grasto be returned instead of
1642fe8fb19SBen Gras.Sq \&? .
1652fe8fb19SBen GrasIn either case, the variable
1662fe8fb19SBen Gras.Va optopt
1672fe8fb19SBen Grasis set to the character that caused the error.
1682fe8fb19SBen GrasThe
1692fe8fb19SBen Gras.Fn getopt
1702fe8fb19SBen Grasfunction returns \-1 when the argument list is exhausted.
1712fe8fb19SBen Gras.Sh EXAMPLES
1722fe8fb19SBen Gras.Bd -literal -compact
1732fe8fb19SBen Grasextern char *optarg;
1742fe8fb19SBen Grasextern int optind;
1752fe8fb19SBen Grasint bflag, ch, fd;
1762fe8fb19SBen Gras
1772fe8fb19SBen Grasbflag = 0;
1782fe8fb19SBen Graswhile ((ch = getopt(argc, argv, "bf:")) != -1) {
1792fe8fb19SBen Gras	switch (ch) {
1802fe8fb19SBen Gras	case 'b':
1812fe8fb19SBen Gras		bflag = 1;
1822fe8fb19SBen Gras		break;
1832fe8fb19SBen Gras	case 'f':
1842fe8fb19SBen Gras		if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
1852fe8fb19SBen Gras			(void)fprintf(stderr,
1862fe8fb19SBen Gras			    "myname: %s: %s\en", optarg, strerror(errno));
1872fe8fb19SBen Gras			exit(1);
1882fe8fb19SBen Gras		}
1892fe8fb19SBen Gras		break;
1902fe8fb19SBen Gras	case '?':
1912fe8fb19SBen Gras	default:
1922fe8fb19SBen Gras		usage();
1932fe8fb19SBen Gras	}
1942fe8fb19SBen Gras}
1952fe8fb19SBen Grasargc -= optind;
1962fe8fb19SBen Grasargv += optind;
1972fe8fb19SBen Gras.Ed
1982fe8fb19SBen Gras.Sh DIAGNOSTICS
1992fe8fb19SBen GrasIf the
2002fe8fb19SBen Gras.Fn getopt
2012fe8fb19SBen Grasfunction encounters a character not found in the string
2022fe8fb19SBen Gras.Fa optstring
2032fe8fb19SBen Grasor detects
2042fe8fb19SBen Grasa missing option argument it writes an error message to
2052fe8fb19SBen Gras.Em stderr
2062fe8fb19SBen Grasand returns
2072fe8fb19SBen Gras.Sq \&? .
2082fe8fb19SBen GrasSetting
2092fe8fb19SBen Gras.Va opterr
2102fe8fb19SBen Grasto a zero will disable these error messages.
2112fe8fb19SBen GrasIf
2122fe8fb19SBen Gras.Fa optstring
2132fe8fb19SBen Grashas a leading
2142fe8fb19SBen Gras.Sq \&:
2152fe8fb19SBen Grasthen a missing option argument causes a
2162fe8fb19SBen Gras.Sq \&:
2172fe8fb19SBen Grasto be returned in addition to suppressing any error messages.
2182fe8fb19SBen Gras.Pp
2192fe8fb19SBen GrasOption arguments are allowed to begin with
2202fe8fb19SBen Gras.Sq - ;
2212fe8fb19SBen Grasthis is reasonable but reduces the amount of error checking possible.
2222fe8fb19SBen Gras.Sh SEE ALSO
2232fe8fb19SBen Gras.Xr getopt 1 ,
2242fe8fb19SBen Gras.Xr getopt_long 3 ,
2252fe8fb19SBen Gras.Xr getsubopt 3
2262fe8fb19SBen Gras.Sh STANDARDS
2272fe8fb19SBen GrasThe
2282fe8fb19SBen Gras.Va optreset
2292fe8fb19SBen Grasvariable was added to make it possible to call the
2302fe8fb19SBen Gras.Fn getopt
2312fe8fb19SBen Grasfunction multiple times.
2322fe8fb19SBen GrasThis is an extension to the
2332fe8fb19SBen Gras.St -p1003.2
2342fe8fb19SBen Grasspecification.
2352fe8fb19SBen Gras.Sh HISTORY
2362fe8fb19SBen GrasThe
2372fe8fb19SBen Gras.Fn getopt
2382fe8fb19SBen Grasfunction appeared in
2392fe8fb19SBen Gras.Bx 4.3 .
2402fe8fb19SBen Gras.Sh BUGS
2412fe8fb19SBen GrasThe
2422fe8fb19SBen Gras.Fn getopt
2432fe8fb19SBen Grasfunction was once specified to return
2442fe8fb19SBen Gras.Dv EOF
2452fe8fb19SBen Grasinstead of \-1.
2462fe8fb19SBen GrasThis was changed by
2472fe8fb19SBen Gras.St -p1003.2-92
2482fe8fb19SBen Grasto decouple
2492fe8fb19SBen Gras.Fn getopt
2502fe8fb19SBen Grasfrom
2512fe8fb19SBen Gras.In stdio.h .
2522fe8fb19SBen Gras.Pp
2532fe8fb19SBen GrasA single dash
2542fe8fb19SBen Gras.Pq Sq -
2552fe8fb19SBen Grasmay be specified as a character in
2562fe8fb19SBen Gras.Fa optstring ,
2572fe8fb19SBen Grashowever it should
2582fe8fb19SBen Gras.Em never
2592fe8fb19SBen Grashave an argument associated with it.
2602fe8fb19SBen GrasThis allows
2612fe8fb19SBen Gras.Fn getopt
2622fe8fb19SBen Grasto be used with programs that expect
2632fe8fb19SBen Gras.Sq -
2642fe8fb19SBen Grasas an option flag.
2652fe8fb19SBen GrasThis practice is wrong, and should not be used in any current development.
2662fe8fb19SBen GrasIt is provided for backward compatibility
2672fe8fb19SBen Gras.Em only .
2682fe8fb19SBen GrasCare should be taken not to use
2692fe8fb19SBen Gras.Sq -
2702fe8fb19SBen Grasas the first character in
2712fe8fb19SBen Gras.Fa optstring
2722fe8fb19SBen Grasto avoid a semantic conflict with
2732fe8fb19SBen Gras.Tn GNU
2742fe8fb19SBen Gras.Fn getopt ,
2752fe8fb19SBen Graswhich assigns different meaning to an
2762fe8fb19SBen Gras.Fa optstring
2772fe8fb19SBen Grasthat begins with a
2782fe8fb19SBen Gras.Sq - .
2792fe8fb19SBen GrasBy default, a single dash causes
2802fe8fb19SBen Gras.Fn getopt
2812fe8fb19SBen Grasto return \-1.
2822fe8fb19SBen Gras.Pp
2832fe8fb19SBen GrasIt is also possible to handle digits as option letters.
2842fe8fb19SBen GrasThis allows
2852fe8fb19SBen Gras.Fn getopt
2862fe8fb19SBen Grasto be used with programs that expect a number
2872fe8fb19SBen Gras.Pq Dq Li \-3
2882fe8fb19SBen Grasas an option.
2892fe8fb19SBen GrasThis practice is wrong, and should not be used in any current development.
2902fe8fb19SBen GrasIt is provided for backward compatibility
2912fe8fb19SBen Gras.Em only .
2922fe8fb19SBen GrasThe following code fragment works in most cases.
2932fe8fb19SBen Gras.Bd -literal -offset indent
2942fe8fb19SBen Grasint ch;
2952fe8fb19SBen Graslong length;
2962fe8fb19SBen Graschar *p;
2972fe8fb19SBen Gras
2982fe8fb19SBen Graswhile ((ch = getopt(argc, argv, "0123456789")) != -1) {
2992fe8fb19SBen Gras	switch (ch) {
3002fe8fb19SBen Gras	case '0': case '1': case '2': case '3': case '4':
3012fe8fb19SBen Gras	case '5': case '6': case '7': case '8': case '9':
3022fe8fb19SBen Gras		p = argv[optind - 1];
3032fe8fb19SBen Gras		if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2])
3042fe8fb19SBen Gras			length = ch - '0';
3052fe8fb19SBen Gras		else
3062fe8fb19SBen Gras			length = strtol(argv[optind] + 1, NULL, 10);
3072fe8fb19SBen Gras		break;
3082fe8fb19SBen Gras	}
3092fe8fb19SBen Gras}
3102fe8fb19SBen Gras.Ed
311