Commit 1486e078 authored by Benoit Barbot's avatar Benoit Barbot
Browse files

improve argument parser

parent 95ea9a80
Pipeline #1683 failed with stages
in 23 seconds
module type SYM = sig
type t
val content : t ref
val def : t
val list : string list
val of_string : string -> t
val to_string : t -> string
end
type 'a arg_t =
Bool of bool ref
| Set_string of string option ref
| Set_int of int option ref
| Set_float of float option ref
| Int of int ref * int
| Float of float ref * float
| String of string ref * string
| Symbol of (module SYM)
(*string list * 'a ref * 'a * ('a -> string) * (string -> 'a)*)
let to_caml_arg = function
Bool b -> Arg.Set b
| Set_string sr -> Arg.String (fun s -> sr := Some s)
| Set_int ir -> Arg.Int (fun i -> ir := Some i)
| Set_float fr -> Arg.Float (fun f -> fr := Some f)
| Int (ir,_) -> Arg.Set_int ir
| Float (fr,_) -> Arg.Set_float fr
| String (sr,_) -> Arg.Set_string sr
| Symbol s ->
let module S = (val s : SYM) in
Arg.Symbol (S.list, fun s -> S.content := S.of_string s)
type samplingMeth =
Exact
| Receding of int
......@@ -39,7 +72,6 @@ let out_style = ref OutFormat.Timestamp
let store_traj = ref false
let print_rg = ref false
let sampler = ref Low_disc_sampler.random
let sampling_meth = ref Exact
let gnuplot_driver = ref None
let export_zone_graph = ref None
let frequency = ref None
......@@ -47,44 +79,75 @@ let max_iteration = ref 20
let rational_impl = ref false
let expected_duration = ref None
let random_seed = ref None
let receding = ref None
let template = ref None
let sampling_meth = ref Exact
let spec_short = [
"--debug", Arg.Set print_rg, "Print the intermediate representation";
"--poly",Arg.Set_int npoly, "Number of polynome Iteration (default 3)";
"--traj",Arg.Set_int nbtraj, "Number of trajectories (default 10)";
"--receding", Arg.Int (fun x -> sampling_meth := Receding x), "trajectory length";
"--seed", Arg.Int (fun i -> random_seed := Some i), "seed of the random sampler";
"--frequency", Arg.Float (fun f ->frequency := Some f), "frequency parameter s (default 0)";
"--time-duration", Arg.Float (fun f -> expected_duration:= Some f), "time word duration parameter";
"--debug", Bool print_rg, "Print the intermediate representation";
"--poly", Int (npoly,3), "Number of polynome Iteration";
"--traj", Int (nbtraj,10), "Number of trajectories";
"--receding", Set_int receding, "trajectory length";
"--seed", Set_int random_seed, "seed of the random sampler";
"--frequency", Set_float frequency, "frequency parameter s";
"--time-duration", Set_float expected_duration, "time word duration parameter";
(*"-o", Arg.String (fun s -> outfile := Format.formatter_of_out_channel @@ open_out s), "Output file for trajectories";*)
"--template", Arg.String (fun s -> try
sampling_meth := Driven (timeword_parser s);
with
| x -> print_endline "Fail to parse driving command"; raise x;
), "specify a template for time words; template look like '0.2[a]_[b]'";
"--max-iteration", Arg.Set_int max_iteration, "Number of iteration for Newton method (default 20)";
"--sampler",Arg.Symbol (["random"; "halton";"kronecker"], (
function
"halton" -> sampler:=Low_disc_sampler.halton
| "kronecker" -> sampler:=Low_disc_sampler.kronecker_phi
| _ -> ())),
" choose sampler (default random)";
"--output-format",Arg.Symbol (OutFormat.style_list, (fun s -> out_style := OutFormat.style_of_string s)),
" choose the format of the output";
"--template", Set_string template, "specify a template for time words; template look like '0.2[a]_[b]'";
"--max-iteration", Int (max_iteration,20) , "Number of iteration for Newton method";
"--sampler", Symbol (module struct
type t = int -> int -> float
let list = ["random"; "halton";"kronecker"]
let content = sampler
let def = Low_disc_sampler.random
let to_string = function
f when f=Low_disc_sampler.halton -> "halton"
| f when f=Low_disc_sampler.kronecker_phi -> "kronecker"
| _ -> "random"
let of_string = function
"halton" -> Low_disc_sampler.halton
| "kronecker" -> Low_disc_sampler.kronecker_phi
| _ -> Low_disc_sampler.random
end), " choose sampler (default random)";
"--output-format",Symbol (module struct
type t = OutFormat.output_style
let list = OutFormat.style_list
let content = out_style
let def = OutFormat.Timeword
let of_string = OutFormat.style_of_string
let to_string = OutFormat.string_of_style
end), " choose the format of the output";
"--apericube", Arg.Set store_traj, "Compute apericube";
"-v", Arg.Set_int verbose, "Verbose level";
"--exact-rational", Arg.Set rational_impl, "Use exact arithmetic for computation";
]
"--apericube", Bool store_traj, "Compute apericube";
"-v", Int (verbose,1), "Verbose level";
"--exact-rational", Bool rational_impl, "Use exact arithmetic for computation";
]
let spec_list_full = spec_short @ [
"--gnuplot-driver", Arg.String (fun d -> gnuplot_driver := Some d), "launch gnuplot";
"--export-splitreach", Arg.String (fun s -> export_zone_graph:= Some s), "Export the splitted zone graph";
"--splitting-debug", Arg.Set_string splitting_debug, "Export debug information on the splitting in tex format"
let spec_full =
spec_short @ [
"--gnuplot-driver", Set_string gnuplot_driver, "launch gnuplot";
"--export-splitreach", Set_string export_zone_graph, "Export the splitted zone graph";
"--splitting-debug", String (splitting_debug,"") , "Export debug information on the splitting in tex format"
]
let usage_str = "wordgen automaton [outputfile]"
let post_parse () =
(match !frequency, !expected_duration with
Some _ ,Some _ -> failwith "Set frequency or expected duration not both"
| _ -> ());
match !receding,!template with
None, None -> sampling_meth := Exact
| Some r, None -> sampling_meth := Receding r
| None, Some t -> (try
sampling_meth := Driven (timeword_parser t);
with
| x -> failwith "Fail to parse driving command")
| Some _, Some _ -> failwith "Set receding horizon or a template not both"
let spec_caml_arg = List.map (fun (n,spec,def) -> (n, to_caml_arg spec,def)) spec_full
let usage_str = "wordgen automaton.prism [outputfile]"
......
......@@ -13,6 +13,15 @@ let style_of_string = function
| "debug" -> Debug
| _ -> Void
let string_of_style = function
| Void -> "void"
| Timestamp -> "timestamp"
| Timeword -> "timeword"
| Word -> "word"
| TimewordState -> "timeword_state"
| StateList -> "state_list"
| StateListFull -> "state_list_full"
| Debug -> "debug"
open Format
......
......@@ -32,14 +32,15 @@ let _ =
let narg = ref 0
(* Parse command line arguments *)
let _ =
Arg.parse spec_list_full (fun st -> incr narg;
Arg.parse spec_caml_arg (fun st -> incr narg;
match !narg with
| 1 -> infile := st
| 2 -> outfilename := Some st
| _ -> raise @@ Arg.Bad "Too many arguments") usage_str;
if !narg < 1 then (
Arg.usage spec_list_full usage_str;
exit 1)
Arg.usage spec_caml_arg usage_str;
exit 1);
post_parse ()
(* Main *)
let _ =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment