xref: /freebsd/usr.bin/getopt/getopt.1 (revision eec64f7486b83d0692714786084cbc7c9e1a402e)
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
8eec64f74SMartin Cracauer.Nm args=\`getopt Ar optstring $*\`
9eec64f74SMartin Cracauer; errcode=$?; set \-\- $args
10b37b9a6dSNate Williams.Sh DESCRIPTION
11b37b9a6dSNate Williams.Nm Getopt
12b37b9a6dSNate Williamsis used to break up options in command lines for easy parsing by
13b37b9a6dSNate Williamsshell procedures, and to check for legal options.
140b6471c9SJoerg Wunsch.Ar Optstring
15b37b9a6dSNate Williamsis a string of recognized option letters (see
16b37b9a6dSNate Williams.Xr getopt 3
17b37b9a6dSNate Williams);
18b37b9a6dSNate Williamsif a letter is followed by a colon, the option
19b37b9a6dSNate Williamsis expected to have an argument which may or may not be
20b37b9a6dSNate Williamsseparated from it by white space.
21b37b9a6dSNate WilliamsThe special option
220b6471c9SJoerg Wunsch.Ql \-\-
23b37b9a6dSNate Williamsis used to delimit the end of the options.
24b37b9a6dSNate Williams.Nm Getopt
25b37b9a6dSNate Williamswill place
260b6471c9SJoerg Wunsch.Ql \-\-
27b37b9a6dSNate Williamsin the arguments at the end of the options,
28b37b9a6dSNate Williamsor recognize it if used explicitly.
29b37b9a6dSNate WilliamsThe shell arguments
30b37b9a6dSNate Williams(\fB$1 $2\fR ...) are reset so that each option is
31b37b9a6dSNate Williamspreceded by a
320b6471c9SJoerg Wunsch.Ql \-
33b37b9a6dSNate Williamsand in its own shell argument;
34b37b9a6dSNate Williamseach option argument is also in its own shell argument.
35b37b9a6dSNate Williams.Sh EXAMPLE
36b37b9a6dSNate WilliamsThe following code fragment shows how one might process the arguments
37b37b9a6dSNate Williamsfor a command that can take the options
380b6471c9SJoerg Wunsch.Fl a
39b37b9a6dSNate Williamsand
400b6471c9SJoerg Wunsch.Fl b ,
41b37b9a6dSNate Williamsand the option
420b6471c9SJoerg Wunsch.Fl o ,
43b37b9a6dSNate Williamswhich requires an argument.
44b37b9a6dSNate Williams.Pp
45b37b9a6dSNate Williams.Bd -literal -offset indent
46eec64f74SMartin Cracauerargs=\`getopt abo: $*\`
47eec64f74SMartin Cracauer# you should not use \`getopt abo: "$@"\` since that would parse
48eec64f74SMartin Cracauer# the arguments differently from what the set command below does.
4918b3ba26SMartin Cracauerif [ $? != 0 ]
50b37b9a6dSNate Williamsthen
51b37b9a6dSNate Williams	echo 'Usage: ...'
52b37b9a6dSNate Williams	exit 2
53b37b9a6dSNate Williamsfi
54eec64f74SMartin Cracauerset \-\- $args
55eec64f74SMartin Cracauer# You cannot use the set command with a backquoted getopt directly,
56eec64f74SMartin Cracauer# since the exit code from getopt would be shadowed by those of set,
57eec64f74SMartin Cracauer# which is zero by definition.
58b37b9a6dSNate Williamsfor i
59b37b9a6dSNate Williamsdo
60b37b9a6dSNate Williams	case "$i"
61b37b9a6dSNate Williams	in
62b37b9a6dSNate Williams		\-a|\-b)
630ab2a7aeSMartin Cracauer			echo flag $i set; sflags="${i#-}$sflags";
640ab2a7aeSMartin Cracauer			shift;;
65b37b9a6dSNate Williams		\-o)
660ab2a7aeSMartin Cracauer			echo oarg is "'"$2"'"; oarg="$2"; shift;
670ab2a7aeSMartin Cracauer			shift;;
68b37b9a6dSNate Williams		\-\-)
69b37b9a6dSNate Williams			shift; break;;
70b37b9a6dSNate Williams	esac
71b37b9a6dSNate Williamsdone
72eec64f74SMartin Cracauerecho single-char flags: "'"$sflags"'"
7318b3ba26SMartin Cracauerecho oarg is "'"$oarg"'"
74b37b9a6dSNate Williams.Ed
75b37b9a6dSNate Williams.Pp
76b37b9a6dSNate WilliamsThis code will accept any of the following as equivalent:
77b37b9a6dSNate Williams.Pp
78b37b9a6dSNate Williams.Bd -literal -offset indent
79b37b9a6dSNate Williamscmd \-aoarg file file
80b37b9a6dSNate Williamscmd \-a \-o arg file file
81b37b9a6dSNate Williamscmd \-oarg -a file file
82b37b9a6dSNate Williamscmd \-a \-oarg \-\- file file
8318b3ba26SMartin Cracauer.Pp
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.
95eec64f74SMartin CracauerBehavior believed identical to the Bell version. Example changed 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
1030ab2a7aeSMartin CracauerArguments containing white space or embedded shell metacharacters
104eec64f74SMartin Cracauergenerally will not survive intact;  this looks easy to fix but
105eec64f74SMartin Cracauerisn't. People trying to fix
106eec64f74SMartin Cracauer.Nm getopt
107eec64f74SMartin Cracaueror the example in this manpage should check the history of this file
108eec64f74SMartin Cracauerin
109eec64f74SMartin Cracauer.Fx .
110b37b9a6dSNate Williams.Pp
111b37b9a6dSNate WilliamsThe error message for an invalid option is identified as coming
112b37b9a6dSNate Williamsfrom
113b37b9a6dSNate Williams.Nm getopt
114b37b9a6dSNate Williamsrather than from the shell procedure containing the invocation
115b37b9a6dSNate Williamsof
116b37b9a6dSNate Williams.Nm getopt ;
1170ab2a7aeSMartin Cracauerthis again is hard to fix.
1180ab2a7aeSMartin Cracauer.Pp
1190ab2a7aeSMartin CracauerThe precise best way to use the
1200ab2a7aeSMartin Cracauer.Nm set
1210ab2a7aeSMartin Cracauercommand to set the arguments without disrupting the value(s) of
1220ab2a7aeSMartin Cracauershell options varies from one shell version to another.
123eec64f74SMartin Cracauer.Pp
124eec64f74SMartin CracauerEach shellscript has to carry complex code to parse arguments halfway
125eec64f74SMartin Cracauercorrecty (like the example presented here). A better getopt-like tool
126eec64f74SMartin Cracauerwould move much of the complexity into the tool and keep the client
127eec64f74SMartin Cracauershell scripts simpler.
128