[m-rev.] for review: Add float argument support to getopt module.

Paul Bone pbone at csse.unimelb.edu.au
Tue Sep 30 11:07:10 AEST 2008


For review by anyone.

Estimated hours taken: 1
Branches: main

Add two new argument types to the getopt module: float and maybe_float.  These
accept floating point arguments using string.to_float/2

library/getopt.m:
	Add support for float and maybe_float arguments.

NEWS:
	Announce change.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.497
diff -u -p -b -r1.497 NEWS
--- NEWS	23 Sep 2008 06:42:24 -0000	1.497
+++ NEWS	30 Sep 2008 01:04:44 -0000
@@ -225,6 +225,8 @@ Changes to the Mercury standard library:
 	construct.find_functor/5
 	type_desc.same_type/2 
 
+* The getopt module now supports options with floating point arguments.
+
 Changes to the Mercury compiler:
 
 * We have added support for trail segments, which allow programs to grow
Index: library/getopt.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/getopt.m,v
retrieving revision 1.44
diff -u -p -b -r1.44 getopt.m
--- library/getopt.m	21 Feb 2008 03:49:32 -0000	1.44
+++ library/getopt.m	29 Sep 2008 12:23:29 -0000
@@ -36,6 +36,8 @@
 %   - maybe_int (which have a value of `no' or `yes(int)')
 %   - string
 %   - maybe_string (which have a value of `no' or `yes(string)')
+%   - float
+%   - maybe_float (which have a value of 'no' or 'yes(float)')
 %
 % We also support one "accumulating" option type:
 %
@@ -68,7 +70,7 @@
 % It is an error to use a "special" option for which there is no handler, or
 % for which the handler fails.
 %
-% Boolean (i.e. bool or bool_special), maybe_int, maybe_string
+% Boolean (i.e. bool or bool_special), maybe_int, maybe_string, maybe_float
 % and accumulating options can be negated. Negating an accumulating
 % option empties the accumulated list of strings.
 % Single-character options can be negated by following them
@@ -92,6 +94,7 @@
 
 :- import_module bool.
 :- import_module char.
+:- import_module float.
 :- import_module list.
 :- import_module map.
 :- import_module maybe.
@@ -233,8 +236,10 @@
     --->    bool(bool)
     ;       int(int)
     ;       string(string)
+    ;       float(float)
     ;       maybe_int(maybe(int))
     ;       maybe_string(maybe(string))
+    ;       maybe_float(maybe(float))
     ;       accumulating(list(string))
     ;       special
     ;       bool_special
@@ -271,6 +276,10 @@
     string::out) is det.
 :- func getopt.lookup_string_option(option_table(Option), Option) = string.
 
+:- pred getopt.lookup_float_option(option_table(Option)::in, Option::in,
+    float::out) is det.
+:- func getopt.lookup_float_option(option_table(Option), Option) = float.
+
 :- pred getopt.lookup_maybe_int_option(option_table(Option)::in, Option::in,
     maybe(int)::out) is det.
 :- func getopt.lookup_maybe_int_option(option_table(Option), Option) =
@@ -281,6 +290,11 @@
 :- func getopt.lookup_maybe_string_option(option_table(Option), Option) =
     maybe(string).
 
+:- pred getopt.lookup_maybe_float_option(option_table(Option)::in,
+    Option::in, maybe(float)::out) is det.
+:- func getopt.lookup_maybe_float_option(option_table(Option), Option) =
+    maybe(float).
+
 :- pred getopt.lookup_accumulating_option(option_table(Option)::in,
     Option::in, list(string)::out) is det.
 :- func getopt.lookup_accumulating_option(option_table(Option), Option) =
@@ -676,7 +690,7 @@ getopt.process_option(int(_), Option, Fl
             map.set(OptionTable0, Flag, int(IntArg), OptionTable),
             Result = ok(OptionTable)
         ;
-            getopt.numeric_argument(Option, Arg, Result)
+            getopt.int_argument(Option, Arg, Result)
         )
     ;
         MaybeArg = no,
@@ -693,6 +707,21 @@ getopt.process_option(string(_), _Option
         MaybeArg = no,
         error("string argument expected in getopt.process_option")
     ).
+getopt.process_option(float(_), Option, Flag, MaybeArg, _OptionOps,
+        OptionTable0, Result, !OptionsSet) :-
+    svset.insert(Flag, !OptionsSet),
+    (
+        MaybeArg = yes(Arg),
+        ( string.to_float(Arg, FloatArg) ->
+            map.set(OptionTable0, Flag, float(FloatArg), OptionTable),
+            Result = ok(OptionTable)
+        ;
+            getopt.float_argument(Option, Arg, Result)
+        )
+    ;
+        MaybeArg = no,
+        error("float argument expected in getopt.process_option")
+    ).
 getopt.process_option(maybe_int(_), Option, Flag, MaybeArg, _OptionOps,
         OptionTable0, Result, !OptionsSet) :-
     svset.insert(Flag, !OptionsSet),
@@ -702,7 +731,7 @@ getopt.process_option(maybe_int(_), Opti
             map.set(OptionTable0, Flag, maybe_int(yes(IntArg)), OptionTable),
             Result = ok(OptionTable)
         ;
-            getopt.numeric_argument(Option, Arg, Result)
+            getopt.int_argument(Option, Arg, Result)
         )
     ;
         MaybeArg = no,
@@ -719,6 +748,22 @@ getopt.process_option(maybe_string(_), _
         MaybeArg = no,
         error("string argument expected in getopt.process_option")
     ).
+getopt.process_option(maybe_float(_), Option, Flag, MaybeArg, _OptionOps,
+        OptionTable0, Result, !OptionsSet) :-
+    svset.insert(Flag, !OptionsSet),
+    (
+        MaybeArg = yes(Arg),
+        ( string.to_float(Arg, FloatArg) ->
+            map.set(OptionTable0, Flag, maybe_float(yes(FloatArg)),
+                OptionTable),
+            Result = ok(OptionTable)
+        ;
+            getopt.float_argument(Option, Arg, Result)
+        )
+    ;
+        MaybeArg = no,
+        error("float argument expected in getopt.process_option")
+    ).
 getopt.process_option(accumulating(List0), _Option, Flag, MaybeArg, _OptionOps,
         OptionTable0, Result, !OptionsSet) :-
     svset.insert(Flag, !OptionsSet),
@@ -763,7 +808,7 @@ getopt.process_option(int_special, Optio
             getopt.process_special(Option, Flag, int(IntArg),
                 OptionOps, OptionTable0, Result, !OptionsSet)
         ;
-            getopt.numeric_argument(Option, Arg, Result)
+            getopt.int_argument(Option, Arg, Result)
         )
     ;
         MaybeArg = no,
@@ -816,6 +861,11 @@ process_negated_option(Option, Flag, Opt
             map.set(OptionTable0, Flag, maybe_string(no), OptionTable),
             Result = ok(OptionTable)
         ;
+            OptionData = maybe_float(_),
+            svset.insert(Flag, !OptionsSet),
+            map.set(OptionTable0, Flag, maybe_float(no), OptionTable),
+            Result = ok(OptionTable)
+        ;
             OptionData = accumulating(_),
             svset.insert(Flag, !OptionsSet),
             map.set(OptionTable0, Flag, accumulating([]), OptionTable),
@@ -831,31 +881,13 @@ process_negated_option(Option, Flag, Opt
             getopt.process_special(Option, Flag, maybe_string(no),
                 OptionOps, OptionTable0, Result, !OptionsSet)
         ;
-            OptionData = int_special,
-            string.append_list(["cannot negate option `", Option, "' --",
-                "only boolean, maybe and accumulating options can be negated"],
-                ErrorMsg),
-            Result = error(ErrorMsg)
-        ;
-            OptionData = string_special,
-            string.append_list(["cannot negate option `", Option, "' --",
-                "only boolean, maybe and accumulating options can be negated"],
-                ErrorMsg),
-            Result = error(ErrorMsg)
-        ;
-            OptionData = int(_),
-            string.append_list(["cannot negate option `", Option, "' --",
-                "only boolean, maybe and accumulating options can be negated"],
-                ErrorMsg),
-            Result = error(ErrorMsg)
-        ;
-            OptionData = string(_),
-            string.append_list(["cannot negate option `", Option, "' --",
-                "only boolean, maybe and accumulating options can be negated"],
-                ErrorMsg),
-            Result = error(ErrorMsg)
-        ;
-            OptionData = special,
+            ( OptionData = int_special
+            ; OptionData = string_special
+            ; OptionData = int(_)
+            ; OptionData = string(_)
+            ; OptionData = float(_)
+            ; OptionData = special
+            ),
             string.append_list(["cannot negate option `", Option, "' --",
                 "only boolean, maybe and accumulating options can be negated"],
                 ErrorMsg),
@@ -913,8 +945,10 @@ getopt.process_special(Option, Flag, Opt
 getopt.need_arg(bool(_), no).
 getopt.need_arg(int(_), yes).
 getopt.need_arg(string(_), yes).
+getopt.need_arg(float(_), yes).
 getopt.need_arg(maybe_int(_), yes).
 getopt.need_arg(maybe_string(_), yes).
+getopt.need_arg(maybe_float(_), yes).
 getopt.need_arg(accumulating(_), yes).
 getopt.need_arg(special, no).
 getopt.need_arg(bool_special, no).
@@ -922,12 +956,24 @@ getopt.need_arg(int_special, yes).
 getopt.need_arg(string_special, yes).
 getopt.need_arg(maybe_string_special, yes).
 
-:- pred getopt.numeric_argument(string::in, string::in,
+:- pred getopt.int_argument(string::in, string::in,
     maybe_option_table(OptionType)::out) is det.
 
-getopt.numeric_argument(Option, Arg, Result) :-
+getopt.int_argument(Option, Arg, Result) :-
+    invalid_type_argument(Option, Arg, "integer", Result).
+
+:- pred getopt.float_argument(string::in, string::in,
+    maybe_option_table(OptionType)::out) is det.
+
+getopt.float_argument(Option, Arg, Result) :-
+    invalid_type_argument(Option, Arg, "float", Result).
+
+:- pred getopt.invalid_type_argument(string::in, string::in, string::in,
+    maybe_option_table(OptionsType)::out) is det.
+
+getopt.invalid_type_argument(Option, Arg, Type, Result) :-
     ErrorMsg = "option `" ++ Option ++ "'" ++
-        "requires a numeric argument; `" ++ Arg ++ "' is not numeric",
+        "requires an " ++ Type ++ "; `" ++ Arg ++ "' is not a " ++ Type,
     Result = error(ErrorMsg).
 
 %-----------------------------------------------------------------------------%
@@ -953,6 +999,13 @@ getopt.lookup_string_option(OptionTable,
         error("Expected string option and didn't get one.")
     ).
 
+getopt.lookup_float_option(OptionTable, Opt, Val) :-
+    ( map.lookup(OptionTable, Opt, float(Val0)) ->
+        Val = Val0
+    ;
+        error("Expected float option and didn't get one.")
+    ).
+
 getopt.lookup_maybe_int_option(OptionTable, Opt, Val) :-
     ( map.lookup(OptionTable, Opt, maybe_int(Val0)) ->
         Val = Val0
@@ -967,6 +1020,13 @@ getopt.lookup_maybe_string_option(Option
         error("Expected maybe_string option and didn't get one.")
     ).
 
+getopt.lookup_maybe_float_option(OptionTable, Opt, Val) :-
+    ( map.lookup(OptionTable, Opt, maybe_float(Val0)) ->
+        Val = Val0
+    ;
+        error("Expected maybe_float option and didn't get one.")
+    ).
+
 getopt.lookup_accumulating_option(OptionTable, Opt, Val) :-
     ( map.lookup(OptionTable, Opt, accumulating(Val0)) ->
         Val = Val0
@@ -988,11 +1048,17 @@ getopt.lookup_int_option(OT, Opt) = N :-
 getopt.lookup_string_option(OT, Opt) = S :-
     getopt.lookup_string_option(OT, Opt, S).
 
+getopt.lookup_float_option(OT, Opt) = S :-
+    getopt.lookup_float_option(OT, Opt, S).
+
 getopt.lookup_maybe_int_option(OT, Opt) = MN :-
     getopt.lookup_maybe_int_option(OT, Opt, MN).
 
 getopt.lookup_maybe_string_option(OT, Opt) =MS :-
     getopt.lookup_maybe_string_option(OT, Opt, MS).
 
+getopt.lookup_maybe_float_option(OT, Opt) =MS :-
+    getopt.lookup_maybe_float_option(OT, Opt, MS).
+
 getopt.lookup_accumulating_option(OT, Opt) =Ss :-
     getopt.lookup_accumulating_option(OT, Opt, Ss).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20080930/e81da712/attachment.sig>


More information about the reviews mailing list