Project

General

Profile

« Previous | Next » 

Revision 5a71ed25

Added by Christophe Garion over 4 years ago

json-parser: use Logs and Cmdliner librairies

View differences:

src/tools/stateflow/main_with_json.ml
1 1
open Basetypes
2
open Cmdliner
2 3
open Datatype
3 4
open Parser_json
4 5
open Sys
......
12 13

  
13 14
module Parse = Parser (ParseExt)
14 15

  
15
let main ()  =
16
(* setup for logging *)
17
let setup_log style_renderer level =
18
  Fmt_tty.setup_std_outputs ?style_renderer ();
19
  Logs.set_level level;
20
  Logs.set_reporter (Logs_fmt.reporter ());
21
  ()
22

  
23
(* function representing the program to execute *)
24
let json_parse _ file pp =
25
  let prog = Parse.parse_prog (Yojson.Basic.from_file file) in
26
  if pp then
27
    SF.pp_prog Format.std_formatter prog
28

  
29
(* term representing argument for file *)
30
let file =
31
  let doc = "The file to parse." in
32
  let env = Arg.env_var "JSON_FILE" ~doc in
33
  let doc = "The file to parse." in
34
  Arg.(required & pos 0 (some string) None & info [] ~env ~docv:"FILE" ~doc)
35

  
36
(* term representing argument for flag for pretty printing the program *)
37
let pp =
38
  let doc = "Pretty print the resulting program" in
39
  Arg.(value & flag & info ["pp"; "pretty-print"] ~docv:"PP" ~doc)
40

  
41
(* term for argument for logging *)
42
let setup_log_arg =
43
  let env = Arg.env_var "TOOL_VERBOSITY" in
44
  Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ~env ())
45

  
46
(* term representing the program to execute *)
47
let json_parse_t = Term.(const json_parse $ setup_log_arg $ file $ pp)
48

  
49
(* term info for manpages etc. *)
50
let info =
51
  let doc = "parse a JSON file representing a Stateflow model" in
52
  let man = [
53
    `S Manpage.s_bugs;
54
    `P "Report bug to Github issues tracking." ]
55
  in
56
  Term.info "json-parser-example" ~doc ~exits:Term.default_exits ~man
57

  
58
let main () =
16 59
  begin
17 60
    let json = Yojson.Basic.from_file Sys.argv.(1) in
18 61
    SF.pp_prog Format.std_formatter (Parse.parse_prog json);
19 62
  end
20 63

  
21
let _ = main ()
64
(* program *)
65
let _ = Term.exit @@ Term.eval (json_parse_t, info)

Also available in: Unified diff