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