xref: /freebsd/usr.bin/getopt/getopt.1 (revision 18b3ba267cff8d71364cbabb5429eccf1e79bba4)
118b3ba26SMartin Cracauer.Dd April 3, 1999
2b37b9a6dSNate Williams.Dt GETOPT 1
3b37b9a6dSNate Williams.Os
4b37b9a6dSNate Williams.Sh NAME
5b37b9a6dSNate Williams.Nm getopt
6b37b9a6dSNate Williams.Nd parse command options
7b37b9a6dSNate Williams.Sh SYNOPSIS
818b3ba26SMartin Cracauer.Ic set \-\- \`getopt Ar optstring
918b3ba26SMartin Cracauer.Qq $@
1018b3ba26SMartin Cracauer\`
11b37b9a6dSNate Williams.Sh DESCRIPTION
12b37b9a6dSNate Williams.Nm Getopt
13b37b9a6dSNate Williamsis used to break up options in command lines for easy parsing by
14b37b9a6dSNate Williamsshell procedures, and to check for legal options.
150b6471c9SJoerg Wunsch.Ar Optstring
16b37b9a6dSNate Williamsis a string of recognized option letters (see
17b37b9a6dSNate Williams.Xr getopt 3
18b37b9a6dSNate Williams);
19b37b9a6dSNate Williamsif a letter is followed by a colon, the option
20b37b9a6dSNate Williamsis expected to have an argument which may or may not be
21b37b9a6dSNate Williamsseparated from it by white space.
22b37b9a6dSNate WilliamsThe special option
230b6471c9SJoerg Wunsch.Ql \-\-
24b37b9a6dSNate Williamsis used to delimit the end of the options.
25b37b9a6dSNate Williams.Nm Getopt
26b37b9a6dSNate Williamswill place
270b6471c9SJoerg Wunsch.Ql \-\-
28b37b9a6dSNate Williamsin the arguments at the end of the options,
29b37b9a6dSNate Williamsor recognize it if used explicitly.
30b37b9a6dSNate WilliamsThe shell arguments
31b37b9a6dSNate Williams(\fB$1 $2\fR ...) are reset so that each option is
32b37b9a6dSNate Williamspreceded by a
330b6471c9SJoerg Wunsch.Ql \-
34b37b9a6dSNate Williamsand in its own shell argument;
35b37b9a6dSNate Williamseach option argument is also in its own shell argument.
36b37b9a6dSNate Williams.Sh EXAMPLE
37b37b9a6dSNate WilliamsThe following code fragment shows how one might process the arguments
38b37b9a6dSNate Williamsfor a command that can take the options
390b6471c9SJoerg Wunsch.Fl a
40b37b9a6dSNate Williamsand
410b6471c9SJoerg Wunsch.Fl b ,
42b37b9a6dSNate Williamsand the option
430b6471c9SJoerg Wunsch.Fl o ,
44b37b9a6dSNate Williamswhich requires an argument.
45b37b9a6dSNate Williams.Pp
46b37b9a6dSNate Williams.Bd -literal -offset indent
4718b3ba26SMartin Cracauertmp=$(getopt abo: "$@")
4818b3ba26SMartin Cracauerif [ $? != 0 ]
49b37b9a6dSNate Williamsthen
50b37b9a6dSNate Williams	echo 'Usage: ...'
51b37b9a6dSNate Williams	exit 2
52b37b9a6dSNate Williamsfi
5318b3ba26SMartin Cracauereval set \-\- $tmp
54b37b9a6dSNate Williamsfor i
55b37b9a6dSNate Williamsdo
56b37b9a6dSNate Williams	case "$i"
57b37b9a6dSNate Williams	in
58b37b9a6dSNate Williams		\-a|\-b)
5918b3ba26SMartin Cracauer			echo flag $i set; sflags="${i#-}$sflags"; shift;;
60b37b9a6dSNate Williams		\-o)
6118b3ba26SMartin Cracauer			echo oarg is "'"$2"'"; oarg="$2"; shift; shift;;
62b37b9a6dSNate Williams		\-\-)
63b37b9a6dSNate Williams			shift; break;;
64b37b9a6dSNate Williams	esac
65b37b9a6dSNate Williamsdone
6618b3ba26SMartin Cracauerecho single-char flags: $sflags
6718b3ba26SMartin Cracauerecho oarg is "'"$oarg"'"
68b37b9a6dSNate Williams.Ed
69b37b9a6dSNate Williams.Pp
70b37b9a6dSNate WilliamsThis code will accept any of the following as equivalent:
71b37b9a6dSNate Williams.Pp
72b37b9a6dSNate Williams.Bd -literal -offset indent
73b37b9a6dSNate Williamscmd \-aoarg file file
74b37b9a6dSNate Williamscmd \-a \-o arg file file
75b37b9a6dSNate Williamscmd \-oarg -a file file
76b37b9a6dSNate Williamscmd \-a \-oarg \-\- file file
7718b3ba26SMartin Cracauer.Pp
7818b3ba26SMartin CracauerTest your scripts with calls like this
7918b3ba26SMartin Cracauer.Pp
8018b3ba26SMartin Cracauercmd \-ab \-o 'f \-z'
8118b3ba26SMartin Cracauer.Pp
8218b3ba26SMartin Cracauerto verify that they work with parameters/filenames that have
8318b3ba26SMartin Cracauerwhitespace in them.
84b37b9a6dSNate Williams.Ed
85b37b9a6dSNate Williams.Sh SEE ALSO
86b37b9a6dSNate Williams.Xr sh 1 ,
87b37b9a6dSNate Williams.Xr getopt 3
88b37b9a6dSNate Williams.Sh DIAGNOSTICS
89b37b9a6dSNate Williams.Nm Getopt
9018b3ba26SMartin Cracauerprints an error message on the standard error output and exits with
9118b3ba26SMartin Cracauerstatus > 0 when it encounters an option letter not included in
920b6471c9SJoerg Wunsch.Ar optstring .
93b37b9a6dSNate Williams.Sh HISTORY
94b37b9a6dSNate WilliamsWritten by Henry Spencer, working from a Bell Labs manual page.
9518b3ba26SMartin CracauerBehavior believed identical to the Bell version. Example replaced in
9618b3ba26SMartin Cracauer.Fx
9718b3ba26SMartin Cracauerversion 3.2 and 4.0.
98b37b9a6dSNate Williams.Sh BUGS
99b37b9a6dSNate WilliamsWhatever
100b37b9a6dSNate Williams.Xr getopt 3
101b37b9a6dSNate Williamshas.
102b37b9a6dSNate Williams.Pp
10318b3ba26SMartin CracauerIt is hard to get command switch parsing right in shell scripts,
10418b3ba26SMartin Cracauerespecially with arguments containing whitespace or embedded shell
10518b3ba26SMartin Cracauermetacharacters. This version of
10618b3ba26SMartin Cracauer.Nm getopt
10718b3ba26SMartin Cracauerand the example in this manpage have been fixed to avoid traditional
10818b3ba26SMartin Cracauerproblems. They have been tested with
10918b3ba26SMartin Cracauer.Fx
11018b3ba26SMartin Cracauer.Xr sh 1
11118b3ba26SMartin Cracauerand with GNU bash. Note that bash has a builtin
11218b3ba26SMartin Cracauer.Nm getopt .
11318b3ba26SMartin CracauerIn shells with builtin
11418b3ba26SMartin Cracauer.Nm getopt
11518b3ba26SMartin Cracaueryou need to call getopt with a full path to get the external version
11618b3ba26SMartin Cracauerdescribed here.
117b37b9a6dSNate Williams.Pp
118b37b9a6dSNate WilliamsThe error message for an invalid option is identified as coming
119b37b9a6dSNate Williamsfrom
120b37b9a6dSNate Williams.Nm getopt
121b37b9a6dSNate Williamsrather than from the shell procedure containing the invocation
122b37b9a6dSNate Williamsof
123b37b9a6dSNate Williams.Nm getopt ;
12418b3ba26SMartin Cracauerthis is hard to fix.
125