summaryrefslogtreecommitdiffstats
path: root/usr.bin/getopt
diff options
context:
space:
mode:
authorcracauer <cracauer@FreeBSD.org>1999-04-03 22:24:36 +0000
committercracauer <cracauer@FreeBSD.org>1999-04-03 22:24:36 +0000
commitcfa91c2e4045fec89ba0346224694d4029ee7697 (patch)
tree6f4966894f03e36c7db02fffdd48d51010c8bc9b /usr.bin/getopt
parent60ad9560502beabab095c9441389f5ad42ab0d86 (diff)
downloadFreeBSD-src-cfa91c2e4045fec89ba0346224694d4029ee7697.zip
FreeBSD-src-cfa91c2e4045fec89ba0346224694d4029ee7697.tar.gz
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
Diffstat (limited to 'usr.bin/getopt')
-rw-r--r--usr.bin/getopt/getopt.155
-rw-r--r--usr.bin/getopt/getopt.c2
2 files changed, 39 insertions, 18 deletions
diff --git a/usr.bin/getopt/getopt.1 b/usr.bin/getopt/getopt.1
index ebf1c65..39c79b1 100644
--- a/usr.bin/getopt/getopt.1
+++ b/usr.bin/getopt/getopt.1
@@ -1,11 +1,13 @@
-.Dd June 21, 1993
+.Dd April 3, 1999
.Dt GETOPT 1
.Os
.Sh NAME
.Nm getopt
.Nd parse command options
.Sh SYNOPSIS
-.Nm set \-\- \`getopt Ar optstring $*\`
+.Ic set \-\- \`getopt Ar optstring
+.Qq $@
+\`
.Sh DESCRIPTION
.Nm Getopt
is used to break up options in command lines for easy parsing by
@@ -42,24 +44,27 @@ and the option
which requires an argument.
.Pp
.Bd -literal -offset indent
-set \-\- \`getopt abo: $*\`
-if test $? != 0
+tmp=$(getopt abo: "$@")
+if [ $? != 0 ]
then
echo 'Usage: ...'
exit 2
fi
+eval set \-\- $tmp
for i
do
case "$i"
in
\-a|\-b)
- flag=$i; shift;;
+ echo flag $i set; sflags="${i#-}$sflags"; shift;;
\-o)
- oarg=$2; shift; shift;;
+ echo oarg is "'"$2"'"; oarg="$2"; shift; shift;;
\-\-)
shift; break;;
esac
done
+echo single-char flags: $sflags
+echo oarg is "'"$oarg"'"
.Ed
.Pp
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 \-oarg -a 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
.Sh SEE ALSO
.Xr sh 1 ,
.Xr getopt 3
.Sh DIAGNOSTICS
.Nm Getopt
-prints an error message on the standard error output when it
-encounters an option letter not included in
+prints an error message on the standard error output and exits with
+status > 0 when it encounters an option letter not included in
.Ar optstring .
.Sh HISTORY
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
Whatever
.Xr getopt 3
has.
.Pp
-Arguments containing white space or embedded shell metacharacters
-generally will not survive intact; this looks easy to fix but isn't.
+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.
.Pp
The error message for an invalid option is identified as coming
from
@@ -95,9 +121,4 @@ from
rather than from the shell procedure containing the invocation
of
.Nm getopt ;
-this again 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.
+this is hard to fix.
diff --git a/usr.bin/getopt/getopt.c b/usr.bin/getopt/getopt.c
index 060e2c9..19c21f8 100644
--- a/usr.bin/getopt/getopt.c
+++ b/usr.bin/getopt/getopt.c
@@ -17,7 +17,7 @@ char *argv[];
break;
default:
if (optarg != NULL)
- printf(" -%c %s", c, optarg);
+ printf(" -%c '%s'", c, optarg);
else
printf(" -%c", c);
break;
OpenPOWER on IntegriCloud