xref: /freebsd/usr.bin/getopt/getopt.1 (revision b37b9a6d)
1b37b9a6dSNate Williams.Dd June 21, 1993
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
8b37b9a6dSNate Williams.Nm set \-\- \`getopt optstring $*\`
9b37b9a6dSNate Williams.Sh DESCRIPTION
10b37b9a6dSNate Williams.Nm Getopt
11b37b9a6dSNate Williamsis used to break up options in command lines for easy parsing by
12b37b9a6dSNate Williamsshell procedures, and to check for legal options.
13b37b9a6dSNate Williams.Op Optstring
14b37b9a6dSNate Williamsis a string of recognized option letters (see
15b37b9a6dSNate Williams.Xr getopt 3
16b37b9a6dSNate Williams);
17b37b9a6dSNate Williamsif a letter is followed by a colon, the option
18b37b9a6dSNate Williamsis expected to have an argument which may or may not be
19b37b9a6dSNate Williamsseparated from it by white space.
20b37b9a6dSNate WilliamsThe special option
21b37b9a6dSNate Williams.B \-\-
22b37b9a6dSNate Williamsis used to delimit the end of the options.
23b37b9a6dSNate Williams.Nm Getopt
24b37b9a6dSNate Williamswill place
25b37b9a6dSNate Williams.B \-\-
26b37b9a6dSNate Williamsin the arguments at the end of the options,
27b37b9a6dSNate Williamsor recognize it if used explicitly.
28b37b9a6dSNate WilliamsThe shell arguments
29b37b9a6dSNate Williams(\fB$1 $2\fR ...) are reset so that each option is
30b37b9a6dSNate Williamspreceded by a
31b37b9a6dSNate Williams.B \-
32b37b9a6dSNate Williamsand in its own shell argument;
33b37b9a6dSNate Williamseach option argument is also in its own shell argument.
34b37b9a6dSNate Williams.Sh EXAMPLE
35b37b9a6dSNate WilliamsThe following code fragment shows how one might process the arguments
36b37b9a6dSNate Williamsfor a command that can take the options
37b37b9a6dSNate Williams.Op a
38b37b9a6dSNate Williamsand
39b37b9a6dSNate Williams.Op b ,
40b37b9a6dSNate Williamsand the option
41b37b9a6dSNate Williams.Op o ,
42b37b9a6dSNate Williamswhich requires an argument.
43b37b9a6dSNate Williams.Pp
44b37b9a6dSNate Williams.Bd -literal -offset indent
45b37b9a6dSNate Williamsset \-\- \`getopt abo: $*\`
46b37b9a6dSNate Williamsif test $? != 0
47b37b9a6dSNate Williamsthen
48b37b9a6dSNate Williams	echo 'Usage: ...'
49b37b9a6dSNate Williams	exit 2
50b37b9a6dSNate Williamsfi
51b37b9a6dSNate Williamsfor i
52b37b9a6dSNate Williamsdo
53b37b9a6dSNate Williams	case "$i"
54b37b9a6dSNate Williams	in
55b37b9a6dSNate Williams		\-a|\-b)
56b37b9a6dSNate Williams			flag=$i; shift;;
57b37b9a6dSNate Williams		\-o)
58b37b9a6dSNate Williams			oarg=$2; shift; shift;;
59b37b9a6dSNate Williams		\-\-)
60b37b9a6dSNate Williams			shift; break;;
61b37b9a6dSNate Williams	esac
62b37b9a6dSNate Williamsdone
63b37b9a6dSNate Williams.Ed
64b37b9a6dSNate Williams.Pp
65b37b9a6dSNate WilliamsThis code will accept any of the following as equivalent:
66b37b9a6dSNate Williams.Pp
67b37b9a6dSNate Williams.Bd -literal -offset indent
68b37b9a6dSNate Williamscmd \-aoarg file file
69b37b9a6dSNate Williamscmd \-a \-o arg file file
70b37b9a6dSNate Williamscmd \-oarg -a file file
71b37b9a6dSNate Williamscmd \-a \-oarg \-\- file file
72b37b9a6dSNate Williams.Ed
73b37b9a6dSNate Williams.Sh SEE ALSO
74b37b9a6dSNate Williams.Xr sh 1 ,
75b37b9a6dSNate Williams.Xr getopt 3
76b37b9a6dSNate Williams.Sh DIAGNOSTICS
77b37b9a6dSNate Williams.Nm Getopt
78b37b9a6dSNate Williamsprints an error message on the standard error output when it
79b37b9a6dSNate Williamsencounters an option letter not included in
80b37b9a6dSNate Williams.Op optstring .
81b37b9a6dSNate Williams.Sh HISTORY
82b37b9a6dSNate WilliamsWritten by Henry Spencer, working from a Bell Labs manual page.
83b37b9a6dSNate WilliamsBehavior believed identical to the Bell version.
84b37b9a6dSNate Williams.Sh BUGS
85b37b9a6dSNate WilliamsWhatever
86b37b9a6dSNate Williams.Xr getopt 3
87b37b9a6dSNate Williamshas.
88b37b9a6dSNate Williams.Pp
89b37b9a6dSNate WilliamsArguments containing white space or imbedded shell metacharacters
90b37b9a6dSNate Williamsgenerally will not survive intact;  this looks easy to fix but isn't.
91b37b9a6dSNate Williams.Pp
92b37b9a6dSNate WilliamsThe error message for an invalid option is identified as coming
93b37b9a6dSNate Williamsfrom
94b37b9a6dSNate Williams.Nm getopt
95b37b9a6dSNate Williamsrather than from the shell procedure containing the invocation
96b37b9a6dSNate Williamsof
97b37b9a6dSNate Williams.Nm getopt ;
98b37b9a6dSNate Williamsthis again is hard to fix.
99b37b9a6dSNate Williams.Pp
100b37b9a6dSNate WilliamsThe precise best way to use the
101b37b9a6dSNate Williams.Nm set
102b37b9a6dSNate Williamscommand to set the arguments without disrupting the value(s) of
103b37b9a6dSNate Williamsshell options varies from one shell version to another.
104b37b9a6dSNate Williamsvaries from one shell version to another.
105