args.ml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. open StdLabels
  2. module Report = Qsp_syntax.Report
  3. let input_files = ref []
  4. let usage =
  5. Printf.sprintf "%s input_file" (Filename.basename Sys.executable_name)
  6. let anon_fun filename = input_files := filename :: !input_files
  7. let level_value = ref None
  8. let reset_line = ref false
  9. let interractive () = ()
  10. type filters = { level : Report.level option }
  11. type t = { reset_line : bool; filters : filters }
  12. (** All the arguments given from the command line *)
  13. let level : string -> unit =
  14. fun str_level ->
  15. match Report.level_of_string str_level with
  16. | Ok level_ -> level_value := Some level_
  17. | Error e ->
  18. print_endline e;
  19. exit 1
  20. let disable_module modules identifier =
  21. let identifier =
  22. String.sub identifier ~pos:1 ~len:(String.length identifier - 1)
  23. in
  24. List.iter modules ~f:(fun t ->
  25. let (module C : Qsp_syntax.S.Analyzer) = Qsp_syntax.Check.get_module t in
  26. if String.equal C.identifier identifier then C.active := false)
  27. let enable_module modules identifier =
  28. let identifier =
  29. String.sub identifier ~pos:1 ~len:(String.length identifier - 1)
  30. in
  31. List.iter modules ~f:(fun t ->
  32. let (module C : Qsp_syntax.S.Analyzer) = Qsp_syntax.Check.get_module t in
  33. if String.equal C.identifier identifier then C.active := true)
  34. let speclist printer =
  35. let common_arguments =
  36. [
  37. ( "--version",
  38. Arg.Unit
  39. (fun () ->
  40. Printf.printf "Version %s\n" Tools.Git_hash.revision;
  41. exit 0),
  42. "\tDisplay the version of the application and exit" );
  43. ( "--list-tests",
  44. Arg.Unit
  45. (fun () ->
  46. printer Format.std_formatter;
  47. exit 0),
  48. "\tPrint all the available tests then exit" );
  49. ( "--level",
  50. Arg.String level,
  51. "\tFilter with this message level [debug, warn, error]" );
  52. ( "--global",
  53. Arg.Set reset_line,
  54. "\tEach line is refered from the begining of the file and not the \
  55. location" );
  56. ("-<test>", Arg.Unit (fun () -> ()), "\tDisable this test");
  57. ("+<test>", Arg.Unit (fun () -> ()), "\tEnable this test");
  58. ]
  59. and windows_arguments =
  60. match Sys.os_type with
  61. | "Win32" ->
  62. [ ("--no-prompt", Arg.Unit interractive, "\tDeprecated. Does nothing") ]
  63. | _ -> []
  64. in
  65. common_arguments @ windows_arguments
  66. let parse :
  67. modules:Qsp_syntax.Check.t list ->
  68. list_tests:(Format.formatter -> unit) ->
  69. string list * t =
  70. fun ~modules ~list_tests ->
  71. let speclist = speclist list_tests in
  72. let speclist =
  73. let r = ref speclist in
  74. for i = 1 to pred (Array.length Sys.argv) do
  75. let s = Sys.argv.(i) in
  76. if
  77. s.[0] = '-'
  78. && String.length s > 1
  79. && s.[1] != '-'
  80. && (not (String.equal s "--help"))
  81. && (not (String.equal s "-help"))
  82. && not (List.exists !r ~f:(fun (s', _, _) -> String.equal s s'))
  83. then
  84. r :=
  85. ( s,
  86. Arg.Unit (fun () -> disable_module modules s),
  87. "\tDisable this test" )
  88. :: !r
  89. else if s.[0] = '+' then enable_module modules s
  90. done;
  91. !r
  92. in
  93. let () = Arg.parse (Arg.align speclist) anon_fun usage in
  94. match !input_files with
  95. | [] ->
  96. Arg.usage (Arg.align speclist) usage;
  97. prerr_endline "";
  98. prerr_endline "Error, you should provide at least one file to parse.";
  99. exit 1
  100. | _ ->
  101. let filters = { level = !level_value } in
  102. (!input_files, { reset_line = !reset_line; filters })