.TH getopts "" "" Command
.PC "Parse command-line options"
\fBgetopts \fIoptstring name [ opt ]\fR
.PP
The command
.B getopts
parses a command's options and check their legality.
.I optstring
must contain the options letters that the command using
.B getopts
will recognize.
If a letter is followed by a colon `:', that option must
have an argument that is separated from it by whitespace.
.PP
.II OPTIND
.II OPTARG
Each time it is invoked,
.B getopts
places the next option into the shell variable
.I name
and the index of the next argument to be processed into the shell variable
.BR OPTIND ,
which is initialized by default to one.
When an option requires an argument,
.B getopts
copies it into the shell variable
.BR OPTARG .
If
.B getopts
encounters an error, it initializes variable
.I name
to \fB?\fR.
.PP
When it encounters the end of the options,
.B getopts
exits with non-zero status.
The special option ``--'' can be used to delineate the end of options.
.SH Example
The following example processes a command that takes options
.BR a ,
.BR b ,
and
.BR o ;
the last option requires an argument:
.DM
	while getopts abo: c
	do
		case $c in
			a|b)	FLAGS=$FLAGS$c;;
			o)	OARG=$OPTARG;;
			\e?)	echo $USAGE 1>&2
				exit 2;;
		esac
	done
	shift OPTIND-1
.DE
.PP
This code will accept any of the following as equivalent:
.DM
	cmd -a -b -o"xxx z yy" file
	cmd -a -b -o"xxx z yy" -- file
	cmd -ab -o"xxx z yy" file
	cmd -ab -o"xxx z yy" -- file
.DE
.PP
Note that no space is required between
.B \-o
and its argument.
.SH "See Also"
.Xr "commands," commands
.Xr "getopt()," getopt
.Xr "ksh" ksh
