bdf7af
From ee670da51f4d7763f607e456186c52e72a09a929 Mon Sep 17 00:00:00 2001
f231f6
From: "Richard W.M. Jones" <rjones@redhat.com>
0db310
Date: Tue, 1 Apr 2014 11:17:07 +0100
c5cb96
Subject: [PATCH 11/16] arg: Add no_arg and get_arg helper functions.
f231f6
0db310
The no_arg function in this patch is a no-op.  It will do something
0db310
useful in the followups.
0db310
e4cc04
The get_arg function simply checks the next position on the command
0db310
line exists and returns that argument or raises a Arg.Missing.
0db310
0db310
This patch should introduce no functional change, it is simply code
0db310
refactoring.
0db310
0db310
In particular, this should not change the treatment of Arg.current
0db310
(see: http://caml.inria.fr/mantis/view.php?id=5197#c11147)
f231f6
---
0db310
 stdlib/arg.ml | 47 ++++++++++++++++++++++++++---------------------
0db310
 1 file changed, 26 insertions(+), 21 deletions(-)
f231f6
f231f6
diff --git a/stdlib/arg.ml b/stdlib/arg.ml
fa72b7
index d7b8ac0..a8f3964 100644
f231f6
--- a/stdlib/arg.ml
f231f6
+++ b/stdlib/arg.ml
0db310
@@ -134,56 +134,62 @@ let parse_argv_dynamic ?(current=current) argv speclist anonfun errmsg =
0db310
         try assoc3 s !speclist
0db310
         with Not_found -> stop (Unknown s)
f231f6
       in
0db310
+      let no_arg () = () in
0db310
+      let get_arg () =
0db310
+        if !current + 1 < l then argv.(!current + 1)
0db310
+        else stop (Missing s)
0db310
+      in
f231f6
       begin try
f231f6
         let rec treat_action = function
f231f6
-        | Unit f -> f ();
f231f6
-        | Bool f when !current + 1 < l ->
f231f6
-            let arg = argv.(!current + 1) in
f231f6
+        | Unit f -> no_arg (); f ();
f231f6
+        | Bool f ->
f231f6
+            let arg = get_arg () in
f231f6
             begin try f (bool_of_string arg)
f231f6
             with Invalid_argument "bool_of_string" ->
f231f6
                    raise (Stop (Wrong (s, arg, "a boolean")))
f231f6
             end;
0db310
             incr current;
f231f6
-        | Set r -> r := true;
f231f6
-        | Clear r -> r := false;
f231f6
-        | String f when !current + 1 < l ->
f231f6
-            f argv.(!current + 1);
f231f6
+        | Set r -> no_arg (); r := true;
f231f6
+        | Clear r -> no_arg (); r := false;
f231f6
+        | String f ->
0db310
+            let arg = get_arg () in
0db310
+            f arg;
0db310
             incr current;
0db310
-        | Symbol (symb, f) when !current + 1 < l ->
0db310
-            let arg = argv.(!current + 1) in
f231f6
+        | Symbol (symb, f) ->
f231f6
+            let arg = get_arg () in
f231f6
             if List.mem arg symb then begin
f231f6
-              f argv.(!current + 1);
f231f6
+              f arg;
0db310
               incr current;
f231f6
             end else begin
f231f6
               raise (Stop (Wrong (s, arg, "one of: "
f231f6
                                           ^ (make_symlist "" " " "" symb))))
f231f6
             end
f231f6
-        | Set_string r when !current + 1 < l ->
f231f6
-            r := argv.(!current + 1);
f231f6
+        | Set_string r ->
f231f6
+            r := get_arg ();
0db310
             incr current;
0db310
-        | Int f when !current + 1 < l ->
0db310
-            let arg = argv.(!current + 1) in
f231f6
+        | Int f ->
f231f6
+            let arg = get_arg () in
f231f6
             begin try f (int_of_string arg)
f231f6
             with Failure "int_of_string" ->
f231f6
                    raise (Stop (Wrong (s, arg, "an integer")))
f231f6
             end;
0db310
             incr current;
f231f6
-        | Set_int r when !current + 1 < l ->
f231f6
-            let arg = argv.(!current + 1) in
f231f6
+        | Set_int r ->
f231f6
+            let arg = get_arg () in
f231f6
             begin try r := (int_of_string arg)
f231f6
             with Failure "int_of_string" ->
f231f6
                    raise (Stop (Wrong (s, arg, "an integer")))
f231f6
             end;
0db310
             incr current;
f231f6
-        | Float f when !current + 1 < l ->
f231f6
-            let arg = argv.(!current + 1) in
f231f6
+        | Float f ->
f231f6
+            let arg = get_arg () in
f231f6
             begin try f (float_of_string arg);
f231f6
             with Failure "float_of_string" ->
f231f6
                    raise (Stop (Wrong (s, arg, "a float")))
f231f6
             end;
0db310
             incr current;
f231f6
-        | Set_float r when !current + 1 < l ->
f231f6
-            let arg = argv.(!current + 1) in
f231f6
+        | Set_float r ->
f231f6
+            let arg = get_arg () in
f231f6
             begin try r := (float_of_string arg);
f231f6
             with Failure "float_of_string" ->
f231f6
                    raise (Stop (Wrong (s, arg, "a float")))
0db310
@@ -196,7 +202,6 @@ let parse_argv_dynamic ?(current=current) argv speclist anonfun errmsg =
0db310
               f argv.(!current + 1);
0db310
               incr current;
0db310
             done;
f231f6
-        | _ -> raise (Stop (Missing s))
f231f6
         in
f231f6
         treat_action action
f231f6
       with Bad m -> stop (Message m);
f231f6
-- 
23620d
2.3.1
f231f6