Further backouts and changes to the example.
getopt in bourne shell is in fact hard. Maybe perl isn't *that* bad after all...
This commit is contained in:
parent
67022433f8
commit
eec64f7486
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=45298
@ -5,9 +5,8 @@
|
|||||||
.Nm getopt
|
.Nm getopt
|
||||||
.Nd parse command options
|
.Nd parse command options
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Ic var=\`getopt Ar optstring
|
.Nm args=\`getopt Ar optstring $*\`
|
||||||
.Qq $@
|
; errcode=$?; set \-\- $args
|
||||||
\` ; set \-\- $var
|
|
||||||
.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
|
||||||
@ -44,13 +43,18 @@ and the option
|
|||||||
which requires an argument.
|
which requires an argument.
|
||||||
.Pp
|
.Pp
|
||||||
.Bd -literal -offset indent
|
.Bd -literal -offset indent
|
||||||
tmp=$(getopt abo: "$@")
|
args=\`getopt abo: $*\`
|
||||||
|
# you should not use \`getopt abo: "$@"\` since that would parse
|
||||||
|
# the arguments differently from what the set command below does.
|
||||||
if [ $? != 0 ]
|
if [ $? != 0 ]
|
||||||
then
|
then
|
||||||
echo 'Usage: ...'
|
echo 'Usage: ...'
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
set \-\- $tmp
|
set \-\- $args
|
||||||
|
# You cannot use the set command with a backquoted getopt directly,
|
||||||
|
# since the exit code from getopt would be shadowed by those of set,
|
||||||
|
# which is zero by definition.
|
||||||
for i
|
for i
|
||||||
do
|
do
|
||||||
case "$i"
|
case "$i"
|
||||||
@ -65,7 +69,7 @@ do
|
|||||||
shift; break;;
|
shift; break;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
echo single-char flags: $sflags
|
echo single-char flags: "'"$sflags"'"
|
||||||
echo oarg is "'"$oarg"'"
|
echo oarg is "'"$oarg"'"
|
||||||
.Ed
|
.Ed
|
||||||
.Pp
|
.Pp
|
||||||
@ -88,7 +92,7 @@ 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. Example replaced in
|
Behavior believed identical to the Bell version. Example changed in
|
||||||
.Fx
|
.Fx
|
||||||
version 3.2 and 4.0.
|
version 3.2 and 4.0.
|
||||||
.Sh BUGS
|
.Sh BUGS
|
||||||
@ -97,7 +101,12 @@ Whatever
|
|||||||
has.
|
has.
|
||||||
.Pp
|
.Pp
|
||||||
Arguments containing white space or embedded shell metacharacters
|
Arguments containing white space or embedded shell metacharacters
|
||||||
generally will not survive intact; this looks easy to fix but isn't.
|
generally will not survive intact; this looks easy to fix but
|
||||||
|
isn't. People trying to fix
|
||||||
|
.Nm getopt
|
||||||
|
or the example in this manpage should check the history of this file
|
||||||
|
in
|
||||||
|
.Fx .
|
||||||
.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
|
||||||
@ -111,3 +120,8 @@ The precise best way to use the
|
|||||||
.Nm set
|
.Nm set
|
||||||
command to set the arguments without disrupting the value(s) of
|
command to set the arguments without disrupting the value(s) of
|
||||||
shell options varies from one shell version to another.
|
shell options varies from one shell version to another.
|
||||||
|
.Pp
|
||||||
|
Each shellscript has to carry complex code to parse arguments halfway
|
||||||
|
correcty (like the example presented here). A better getopt-like tool
|
||||||
|
would move much of the complexity into the tool and keep the client
|
||||||
|
shell scripts simpler.
|
||||||
|
Loading…
Reference in New Issue
Block a user