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