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