1999-04-04 00:24:36 +02:00
|
|
|
.Dd April 3, 1999
|
1993-07-27 00:22:37 +02:00
|
|
|
.Dt GETOPT 1
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm getopt
|
|
|
|
.Nd parse command options
|
|
|
|
.Sh SYNOPSIS
|
1999-04-04 00:24:36 +02:00
|
|
|
.Ic set \-\- \`getopt Ar optstring
|
|
|
|
.Qq $@
|
|
|
|
\`
|
1993-07-27 00:22:37 +02:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Nm Getopt
|
|
|
|
is used to break up options in command lines for easy parsing by
|
|
|
|
shell procedures, and to check for legal options.
|
1996-10-22 23:56:11 +02:00
|
|
|
.Ar Optstring
|
1993-07-27 00:22:37 +02:00
|
|
|
is a string of recognized option letters (see
|
|
|
|
.Xr getopt 3
|
|
|
|
);
|
|
|
|
if a letter is followed by a colon, the option
|
|
|
|
is expected to have an argument which may or may not be
|
|
|
|
separated from it by white space.
|
|
|
|
The special option
|
1996-10-22 23:56:11 +02:00
|
|
|
.Ql \-\-
|
1993-07-27 00:22:37 +02:00
|
|
|
is used to delimit the end of the options.
|
|
|
|
.Nm Getopt
|
|
|
|
will place
|
1996-10-22 23:56:11 +02:00
|
|
|
.Ql \-\-
|
1993-07-27 00:22:37 +02:00
|
|
|
in the arguments at the end of the options,
|
|
|
|
or recognize it if used explicitly.
|
|
|
|
The shell arguments
|
|
|
|
(\fB$1 $2\fR ...) are reset so that each option is
|
|
|
|
preceded by a
|
1996-10-22 23:56:11 +02:00
|
|
|
.Ql \-
|
1993-07-27 00:22:37 +02:00
|
|
|
and in its own shell argument;
|
|
|
|
each option argument is also in its own shell argument.
|
|
|
|
.Sh EXAMPLE
|
|
|
|
The following code fragment shows how one might process the arguments
|
|
|
|
for a command that can take the options
|
1996-10-22 23:56:11 +02:00
|
|
|
.Fl a
|
1993-07-27 00:22:37 +02:00
|
|
|
and
|
1996-10-22 23:56:11 +02:00
|
|
|
.Fl b ,
|
1993-07-27 00:22:37 +02:00
|
|
|
and the option
|
1996-10-22 23:56:11 +02:00
|
|
|
.Fl o ,
|
1993-07-27 00:22:37 +02:00
|
|
|
which requires an argument.
|
|
|
|
.Pp
|
|
|
|
.Bd -literal -offset indent
|
1999-04-04 00:24:36 +02:00
|
|
|
tmp=$(getopt abo: "$@")
|
|
|
|
if [ $? != 0 ]
|
1993-07-27 00:22:37 +02:00
|
|
|
then
|
|
|
|
echo 'Usage: ...'
|
|
|
|
exit 2
|
|
|
|
fi
|
1999-04-04 00:24:36 +02:00
|
|
|
eval set \-\- $tmp
|
1993-07-27 00:22:37 +02:00
|
|
|
for i
|
|
|
|
do
|
|
|
|
case "$i"
|
|
|
|
in
|
|
|
|
\-a|\-b)
|
1999-04-04 00:24:36 +02:00
|
|
|
echo flag $i set; sflags="${i#-}$sflags"; shift;;
|
1993-07-27 00:22:37 +02:00
|
|
|
\-o)
|
1999-04-04 00:24:36 +02:00
|
|
|
echo oarg is "'"$2"'"; oarg="$2"; shift; shift;;
|
1993-07-27 00:22:37 +02:00
|
|
|
\-\-)
|
|
|
|
shift; break;;
|
|
|
|
esac
|
|
|
|
done
|
1999-04-04 00:24:36 +02:00
|
|
|
echo single-char flags: $sflags
|
|
|
|
echo oarg is "'"$oarg"'"
|
1993-07-27 00:22:37 +02:00
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
This code will accept any of the following as equivalent:
|
|
|
|
.Pp
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
cmd \-aoarg file file
|
|
|
|
cmd \-a \-o arg file file
|
|
|
|
cmd \-oarg -a file file
|
|
|
|
cmd \-a \-oarg \-\- file file
|
1999-04-04 00:24:36 +02:00
|
|
|
.Pp
|
|
|
|
Test your scripts with calls like this
|
|
|
|
.Pp
|
|
|
|
cmd \-ab \-o 'f \-z'
|
|
|
|
.Pp
|
|
|
|
to verify that they work with parameters/filenames that have
|
|
|
|
whitespace in them.
|
1993-07-27 00:22:37 +02:00
|
|
|
.Ed
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr sh 1 ,
|
|
|
|
.Xr getopt 3
|
|
|
|
.Sh DIAGNOSTICS
|
|
|
|
.Nm Getopt
|
1999-04-04 00:24:36 +02:00
|
|
|
prints an error message on the standard error output and exits with
|
|
|
|
status > 0 when it encounters an option letter not included in
|
1996-10-22 23:56:11 +02:00
|
|
|
.Ar optstring .
|
1993-07-27 00:22:37 +02:00
|
|
|
.Sh HISTORY
|
|
|
|
Written by Henry Spencer, working from a Bell Labs manual page.
|
1999-04-04 00:24:36 +02:00
|
|
|
Behavior believed identical to the Bell version. Example replaced in
|
|
|
|
.Fx
|
|
|
|
version 3.2 and 4.0.
|
1993-07-27 00:22:37 +02:00
|
|
|
.Sh BUGS
|
|
|
|
Whatever
|
|
|
|
.Xr getopt 3
|
|
|
|
has.
|
|
|
|
.Pp
|
1999-04-04 00:24:36 +02:00
|
|
|
It is hard to get command switch parsing right in shell scripts,
|
|
|
|
especially with arguments containing whitespace or embedded shell
|
|
|
|
metacharacters. This version of
|
|
|
|
.Nm getopt
|
|
|
|
and the example in this manpage have been fixed to avoid traditional
|
|
|
|
problems. They have been tested with
|
|
|
|
.Fx
|
|
|
|
.Xr sh 1
|
|
|
|
and with GNU bash. Note that bash has a builtin
|
|
|
|
.Nm getopt .
|
|
|
|
In shells with builtin
|
|
|
|
.Nm getopt
|
|
|
|
you need to call getopt with a full path to get the external version
|
|
|
|
described here.
|
1993-07-27 00:22:37 +02:00
|
|
|
.Pp
|
|
|
|
The error message for an invalid option is identified as coming
|
|
|
|
from
|
|
|
|
.Nm getopt
|
|
|
|
rather than from the shell procedure containing the invocation
|
|
|
|
of
|
|
|
|
.Nm getopt ;
|
1999-04-04 00:24:36 +02:00
|
|
|
this is hard to fix.
|