xref: /freebsd/usr.bin/getopt/getopt.1 (revision 6ff6d951ade3f3379932df7f878ef3ea272cfc59)
1.\" $FreeBSD$
2.\"
3.Dd April 3, 1999
4.Dt GETOPT 1
5.Os
6.Sh NAME
7.Nm getopt
8.Nd parse command options
9.Sh SYNOPSIS
10.Nm args=\`getopt Ar optstring $*\`
11; errcode=$?; set \-\- $args
12.Sh DESCRIPTION
13The
14.Nm
15utility is used to break up options in command lines for easy parsing by
16shell procedures, and to check for legal options.
17.Ar Optstring
18is a string of recognized option letters (see
19.Xr getopt 3 ) ;
20if a letter is followed by a colon, the option
21is expected to have an argument which may or may not be
22separated from it by white space.
23The special option
24.Ql \-\-
25is used to delimit the end of the options.
26The
27.Nm
28utility will place
29.Ql \-\-
30in the arguments at the end of the options,
31or recognize it if used explicitly.
32The shell arguments
33(\fB$1 $2\fR ...) are reset so that each option is
34preceded by a
35.Ql \-
36and in its own shell argument;
37each option argument is also in its own shell argument.
38.Sh EXIT STATUS
39The
40.Nm
41utility prints an error message on the standard error output and exits with
42status > 0 when it encounters an option letter not included in
43.Ar optstring .
44.Sh EXAMPLES
45The following code fragment shows how one might process the arguments
46for a command that can take the options
47.Fl a
48and
49.Fl b ,
50and the option
51.Fl o ,
52which requires an argument.
53.Pp
54.Bd -literal -offset indent
55args=\`getopt abo: $*\`
56# you should not use \`getopt abo: "$@"\` since that would parse
57# the arguments differently from what the set command below does.
58if [ $? -ne 0 ]
59then
60	echo 'Usage: ...'
61	exit 2
62fi
63set \-\- $args
64# You cannot use the set command with a backquoted getopt directly,
65# since the exit code from getopt would be shadowed by those of set,
66# which is zero by definition.
67for i
68do
69	case "$i"
70	in
71		\-a|\-b)
72			echo flag $i set; sflags="${i#-}$sflags";
73			shift;;
74		\-o)
75			echo oarg is "'"$2"'"; oarg="$2"; shift;
76			shift;;
77		\-\-)
78			shift; break;;
79	esac
80done
81echo single-char flags: "'"$sflags"'"
82echo oarg is "'"$oarg"'"
83.Ed
84.Pp
85This code will accept any of the following as equivalent:
86.Pp
87.Bd -literal -offset indent
88cmd \-aoarg file file
89cmd \-a \-o arg file file
90cmd \-oarg -a file file
91cmd \-a \-oarg \-\- file file
92.Ed
93.Sh SEE ALSO
94.Xr getopts 1 ,
95.Xr sh 1 ,
96.Xr getopt 3
97.Sh HISTORY
98Written by
99.An Henry Spencer ,
100working from a Bell Labs manual page.
101Behavior believed identical to the Bell version.
102Example changed in
103.Fx
104version 3.2 and 4.0.
105.Sh BUGS
106Whatever
107.Xr getopt 3
108has.
109.Pp
110Arguments containing white space or embedded shell metacharacters
111generally will not survive intact; this looks easy to fix but
112is not.
113People trying to fix
114.Nm
115or the example in this manpage should check the history of this file
116in
117.Fx .
118.Pp
119The error message for an invalid option is identified as coming
120from
121.Nm
122rather than from the shell procedure containing the invocation
123of
124.Nm ;
125this again is hard to fix.
126.Pp
127The precise best way to use the
128.Nm set
129command to set the arguments without disrupting the value(s) of
130shell options varies from one shell version to another.
131.Pp
132Each shellscript has to carry complex code to parse arguments halfway
133correctly (like the example presented here).
134A better getopt-like tool
135would move much of the complexity into the tool and keep the client
136shell scripts simpler.
137