Bug 133118

Summary: [patch] Error in getopt (1) manual EXAMPLES section
Product: Documentation Reporter: oleg
Component: Books & ArticlesAssignee: Benedict Reuschling <bcr>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   

Description oleg 2009-03-27 09:30:02 UTC
The man page for getopt(1) contains incorrect example.

for i
do
    case "$i"
    in
       ...
done

Variable $@ expanded to list of options and their values (if any).
Loop variable ($i) will contain both (but should contain only options).

This invalid example was imported from NetBSD cvs in Revision 1.1:
http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/getopt/getopt.1?rev=1.1;content-type=text%2Fx-cvsweb-markup

Later this was fixed in NetBSD:
http://cvsweb.netbsd.org/bsdweb.cgi/src/usr.bin/getopt/getopt.1?rev=1.10&content-type=text/x-cvsweb-markup&only_with_tag=MAIN

Fix: 

This piece of code should look like this:
while true;
do
    case "$1"
    in
       ...
done
Comment 1 dfilter service freebsd_committer freebsd_triage 2010-07-07 18:44:22 UTC
Author: bcr (doc committer)
Date: Wed Jul  7 17:44:09 2010
New Revision: 209772
URL: http://svn.freebsd.org/changeset/base/209772

Log:
  Fix an error in the EXAMPLES section of getopt(1), which is based on
  the same fix present in NetBSD.
  
  Note: the getopt man page contains more antique information like this.
  An overhaul of the man page and/or sync with NetBSD would be the right
  thing to do. But since this is out of the scope of the PR, I'll leave
  it as it is for now.
  
  PR:             docs/133118
  Submitted by:   Oleg A. Mamontov (oleg at mamontov dot net)
  Discussed with: jilles@
  MFC after:      2 weeks

Modified:
  head/usr.bin/getopt/getopt.1

Modified: head/usr.bin/getopt/getopt.1
==============================================================================
--- head/usr.bin/getopt/getopt.1	Wed Jul  7 17:20:16 2010	(r209771)
+++ head/usr.bin/getopt/getopt.1	Wed Jul  7 17:44:09 2010	(r209772)
@@ -1,6 +1,6 @@
 .\" $FreeBSD$
 .\"
-.Dd April 3, 1999
+.Dd July 7, 2010
 .Dt GETOPT 1
 .Os
 .Sh NAME
@@ -64,9 +64,9 @@ 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
+while true;
 do
-	case "$i"
+	case "$1"
 	in
 		\-a|\-b)
 			echo flag $i set; sflags="${i#-}$sflags";
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 2 Benedict Reuschling freebsd_committer freebsd_triage 2010-07-07 18:49:12 UTC
State Changed
From-To: open->patched

A patch based on your suggestion was committed to HEAD (r209772). 
MFC pending, so I'll leave it in this state until then. 


Comment 3 Benedict Reuschling freebsd_committer freebsd_triage 2010-07-07 18:49:12 UTC
Responsible Changed
From-To: freebsd-doc->bcr

A patch based on your suggestion was committed to HEAD (r209772). 
MFC pending, so I'll leave it in this state until then.
Comment 4 dfilter service freebsd_committer freebsd_triage 2010-07-21 18:11:36 UTC
Author: bcr (doc committer)
Date: Wed Jul 21 17:11:26 2010
New Revision: 210354
URL: http://svn.freebsd.org/changeset/base/210354

Log:
  MFC r209772:
  
  Fix an error in the EXAMPLES section of getopt(1), which is based on
  the same fix present in NetBSD.
  
  Note: the getopt man page contains more antique information like this.
  An overhaul of the man page and/or sync with NetBSD would be the right
  thing to do. But since this is out of the scope of the PR, I'll leave
  it as it is for now.
  
  PR:             docs/133118
  Submitted by:   Oleg A. Mamontov (oleg at mamontov dot net)
  Discussed with: jilles@

Modified:
  stable/8/usr.bin/getopt/getopt.1
Directory Properties:
  stable/8/usr.bin/getopt/   (props changed)

Modified: stable/8/usr.bin/getopt/getopt.1
==============================================================================
--- stable/8/usr.bin/getopt/getopt.1	Wed Jul 21 13:29:00 2010	(r210353)
+++ stable/8/usr.bin/getopt/getopt.1	Wed Jul 21 17:11:26 2010	(r210354)
@@ -1,6 +1,6 @@
 .\" $FreeBSD$
 .\"
-.Dd April 3, 1999
+.Dd July 7, 2010
 .Dt GETOPT 1
 .Os
 .Sh NAME
@@ -64,9 +64,9 @@ 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
+while true;
 do
-	case "$i"
+	case "$1"
 	in
 		\-a|\-b)
 			echo flag $i set; sflags="${i#-}$sflags";
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 5 Benedict Reuschling freebsd_committer freebsd_triage 2010-07-21 18:18:03 UTC
State Changed
From-To: patched->closed

MFC is done, PR closed. Thanks!
Comment 6 dfilter service freebsd_committer freebsd_triage 2011-02-13 19:07:25 UTC
Author: uqs
Date: Sun Feb 13 19:07:17 2011
New Revision: 218657
URL: http://svn.freebsd.org/changeset/base/218657

Log:
  MFH r209772,217890
  
  Fix an error in the EXAMPLES section of getopt(1), which is based on
  the same fix present in NetBSD.
  
  Fix typo in example getopt(1) script: $i vs $1 [1]
  While here apply style hammer.
  
  PR:		docs/133118, docs/154289 [1]
  Submitted by:	Oleg A. Mamontov, Jamie Landeg Jones

Modified:
  stable/7/usr.bin/getopt/getopt.1
Directory Properties:
  stable/7/usr.bin/getopt/   (props changed)

Modified: stable/7/usr.bin/getopt/getopt.1
==============================================================================
--- stable/7/usr.bin/getopt/getopt.1	Sun Feb 13 19:02:26 2011	(r218656)
+++ stable/7/usr.bin/getopt/getopt.1	Sun Feb 13 19:07:17 2011	(r218657)
@@ -1,6 +1,6 @@
 .\" $FreeBSD$
 .\"
-.Dd April 3, 1999
+.Dd January 26, 2011
 .Dt GETOPT 1
 .Os
 .Sh NAME
@@ -55,8 +55,7 @@ which requires an argument.
 args=\`getopt abo: $*\`
 # you should not use \`getopt abo: "$@"\` since that would parse
 # the arguments differently from what the set command below does.
-if [ $? -ne 0 ]
-then
+if [ $? -ne 0 ]; then
 	echo 'Usage: ...'
 	exit 2
 fi
@@ -64,22 +63,23 @@ 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
-do
-	case "$i"
-	in
-		\-a|\-b)
-			echo flag $i set; sflags="${i#-}$sflags";
-			shift;;
-		\-o)
-			echo oarg is "'"$2"'"; oarg="$2"; shift;
-			shift;;
-		\-\-)
-			shift; break;;
+while true; do
+	case "$1" in
+	\-a|\-b)
+		echo "flag $1 set"; sflags="${1#-}$sflags"
+		shift
+		;;
+	\-o)
+		echo "oarg is '$2'"; oarg="$2"
+		shift; shift
+		;;
+	\-\-)
+		shift; break
+		;;
 	esac
 done
-echo single-char flags: "'"$sflags"'"
-echo oarg is "'"$oarg"'"
+echo "single-char flags: '$sflags'"
+echo "oarg is '$oarg'"
 .Ed
 .Pp
 This code will accept any of the following as equivalent:
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"