1) Fix the case where a shellscript using getopt is called with a
parameter that has space in it, both in getopt.c and in the manpage example. 2) Fix the example in the manpage. The set(1) command is required to return 0 (POSIX 1003.2, section 3.14.11), so you can't test for getopt's exit status like the example did: #! /bin/sh set -- `getopt abo: $*` if test $? != 0 # wrong, tests for set's exit status, which is # always zero, no for getopt(1)'s. Fixes PR bin/5845, which thought it was getopt's fault, but in fact the manpage was wrong. I also updated the example to be more useful and updated the BUGS section. PR: bin/5845
This commit is contained in:
parent
4fe88fe637
commit
18b3ba267c
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=45271
@ -1,11 +1,13 @@
|
|||||||
.Dd June 21, 1993
|
.Dd April 3, 1999
|
||||||
.Dt GETOPT 1
|
.Dt GETOPT 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
.Nm getopt
|
.Nm getopt
|
||||||
.Nd parse command options
|
.Nd parse command options
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm set \-\- \`getopt Ar optstring $*\`
|
.Ic set \-\- \`getopt Ar optstring
|
||||||
|
.Qq $@
|
||||||
|
\`
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
.Nm Getopt
|
.Nm Getopt
|
||||||
is used to break up options in command lines for easy parsing by
|
is used to break up options in command lines for easy parsing by
|
||||||
@ -42,24 +44,27 @@ and the option
|
|||||||
which requires an argument.
|
which requires an argument.
|
||||||
.Pp
|
.Pp
|
||||||
.Bd -literal -offset indent
|
.Bd -literal -offset indent
|
||||||
set \-\- \`getopt abo: $*\`
|
tmp=$(getopt abo: "$@")
|
||||||
if test $? != 0
|
if [ $? != 0 ]
|
||||||
then
|
then
|
||||||
echo 'Usage: ...'
|
echo 'Usage: ...'
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
eval set \-\- $tmp
|
||||||
for i
|
for i
|
||||||
do
|
do
|
||||||
case "$i"
|
case "$i"
|
||||||
in
|
in
|
||||||
\-a|\-b)
|
\-a|\-b)
|
||||||
flag=$i; shift;;
|
echo flag $i set; sflags="${i#-}$sflags"; shift;;
|
||||||
\-o)
|
\-o)
|
||||||
oarg=$2; shift; shift;;
|
echo oarg is "'"$2"'"; oarg="$2"; shift; shift;;
|
||||||
\-\-)
|
\-\-)
|
||||||
shift; break;;
|
shift; break;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
echo single-char flags: $sflags
|
||||||
|
echo oarg is "'"$oarg"'"
|
||||||
.Ed
|
.Ed
|
||||||
.Pp
|
.Pp
|
||||||
This code will accept any of the following as equivalent:
|
This code will accept any of the following as equivalent:
|
||||||
@ -69,25 +74,46 @@ cmd \-aoarg file file
|
|||||||
cmd \-a \-o arg file file
|
cmd \-a \-o arg file file
|
||||||
cmd \-oarg -a file file
|
cmd \-oarg -a file file
|
||||||
cmd \-a \-oarg \-\- file file
|
cmd \-a \-oarg \-\- file file
|
||||||
|
.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.
|
||||||
.Ed
|
.Ed
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr sh 1 ,
|
.Xr sh 1 ,
|
||||||
.Xr getopt 3
|
.Xr getopt 3
|
||||||
.Sh DIAGNOSTICS
|
.Sh DIAGNOSTICS
|
||||||
.Nm Getopt
|
.Nm Getopt
|
||||||
prints an error message on the standard error output when it
|
prints an error message on the standard error output and exits with
|
||||||
encounters an option letter not included in
|
status > 0 when it encounters an option letter not included in
|
||||||
.Ar optstring .
|
.Ar optstring .
|
||||||
.Sh HISTORY
|
.Sh HISTORY
|
||||||
Written by Henry Spencer, working from a Bell Labs manual page.
|
Written by Henry Spencer, working from a Bell Labs manual page.
|
||||||
Behavior believed identical to the Bell version.
|
Behavior believed identical to the Bell version. Example replaced in
|
||||||
|
.Fx
|
||||||
|
version 3.2 and 4.0.
|
||||||
.Sh BUGS
|
.Sh BUGS
|
||||||
Whatever
|
Whatever
|
||||||
.Xr getopt 3
|
.Xr getopt 3
|
||||||
has.
|
has.
|
||||||
.Pp
|
.Pp
|
||||||
Arguments containing white space or embedded shell metacharacters
|
It is hard to get command switch parsing right in shell scripts,
|
||||||
generally will not survive intact; this looks easy to fix but isn't.
|
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.
|
||||||
.Pp
|
.Pp
|
||||||
The error message for an invalid option is identified as coming
|
The error message for an invalid option is identified as coming
|
||||||
from
|
from
|
||||||
@ -95,9 +121,4 @@ from
|
|||||||
rather than from the shell procedure containing the invocation
|
rather than from the shell procedure containing the invocation
|
||||||
of
|
of
|
||||||
.Nm getopt ;
|
.Nm getopt ;
|
||||||
this again is hard to fix.
|
this is hard to fix.
|
||||||
.Pp
|
|
||||||
The precise best way to use the
|
|
||||||
.Nm set
|
|
||||||
command to set the arguments without disrupting the value(s) of
|
|
||||||
shell options varies from one shell version to another.
|
|
||||||
|
@ -17,7 +17,7 @@ char *argv[];
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (optarg != NULL)
|
if (optarg != NULL)
|
||||||
printf(" -%c %s", c, optarg);
|
printf(" -%c '%s'", c, optarg);
|
||||||
else
|
else
|
||||||
printf(" -%c", c);
|
printf(" -%c", c);
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user