[m-rev.] fix getopt bug

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Feb 25 16:02:27 AEDT 2003


Estimated hours taken: 0.5
Branches: main

library/getopt.m:
	Fix a bug: it was not reporting an error when an argument value
	was supplied for an option which should not take an argument.

	Also slightly improve some of the other error messages, e.g. for
	`--intval=foo' we now report "option `--intval' requires a numeric
	argument; `foo' is not numeric" instead of "option `--intval=foo'
	requires a numeric argument; `foo' is not numeric".

Workspace: /home/ceres/fjh/mercury
Index: library/getopt.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/getopt.m,v
retrieving revision 1.27
diff -u -d -r1.27 getopt.m
--- library/getopt.m	24 Jan 2003 07:17:10 -0000	1.27
+++ library/getopt.m	25 Feb 2003 04:07:52 -0000
@@ -291,9 +291,10 @@
 			LongOption = LongOptionStr,
 			MaybeArg = no
 		),
+		OptionName = "--" ++ LongOption,
 	  	( call(LongOptionPred, LongOption, Flag) ->
 			( map__search(OptionTable0, Flag, OptionData) ->
-				getopt__handle_long_option(Option, Flag,
+				getopt__handle_long_option(OptionName, Flag,
 					OptionData, MaybeArg, Args0, Args,
 					OptionOps, [Option | OptionArgs0],
 					OptionArgs, OptionTable0, Result)
@@ -305,7 +306,8 @@
 				Args = Args0
 			)
 		;
-			string__append("unrecognized option `", Option, Tmp),
+			string__append("unrecognized option `", OptionName,
+				Tmp),
 			string__append(Tmp, "'", ErrorMsg),
 			Result = error(ErrorMsg),
 			OptionArgs = OptionArgs0,
@@ -403,6 +405,16 @@
 		OptionArgs = OptionArgs1,
 		string__append_list(["option `", Option,
 			"' needs an argument"],
+			ErrorMsg),
+		Result = error(ErrorMsg)
+	;
+		getopt__need_arg(OptionData, no),
+		MaybeOptionArg = yes(_)
+	->
+		Args = Args0,
+		OptionArgs = OptionArgs1,
+		string__append_list(["option `", Option,
+			"' does not allow an argument"],
 			ErrorMsg),
 		Result = error(ErrorMsg)
 	;

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list