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