Merge branch 'handle-stdout'
[SXSI/xpathcomp.git] / src / options.ml
1 open Format
2 open Config
3
4 let set_index_type = function
5   | "default" -> text_index_type := 0
6   | "swcsa" -> text_index_type := 1
7   | "rlcsa" -> text_index_type := 2
8   | s -> raise (Arg.Bad(s))
9
10 let usage_msg = Printf.sprintf "%s [options] <input.{xml|srx}> 'query' [output]" Sys.argv.(0)
11
12 let pos = ref 0
13 let anon_fun =
14   fun s -> match !pos with
15     | 0 -> input_file:= s;incr pos
16     | 1 -> query := s; incr pos
17     | 2 -> output_file := Some s; incr pos
18     | _ -> raise (Arg.Bad(s))
19
20 let set_logger s =
21   List.iter (fun t ->
22     if t = "" then ()
23     else
24       match Utils.String.explode t ':' with
25         [ tr; lvl ] ->
26           let l = try int_of_string lvl with _ -> raise (Arg.Bad (lvl)) in
27           if Logger.is_logger tr then Logger.activate tr l
28           else raise (Arg.Bad (t))
29       | _ -> raise (Arg.Bad (t))
30   ) (Utils.String.explode s ',')
31
32 let pretty_loggers () =
33   ignore(flush_str_formatter());
34   Pretty.print_list
35     ~sep:", "
36     (fun f s -> fprintf f "%s" s)
37     str_formatter
38     (Logger.available ());
39   flush_str_formatter ()
40
41 let spec = Arg.align
42   [ "-c", Arg.Set(count_only),
43     " counting only (don't materialize the result set)";
44
45     "-two", Arg.Set(twopass),
46     " Use twopass algorithm";
47
48     "-f", Arg.Set_int(sample_factor),
49     "<n> sample factor [default=64]";
50
51     "-ne", Arg.Clear(index_empty_texts),
52     " don't index empty texts [default=index]";
53
54     "-d", Arg.Set(disable_text_collection),
55     " disable text collection[default=false]";
56
57     "-s", Arg.Set_string(save_file),
58     "<save_file> save the intermediate representation into file.srx";
59
60     "-b", Arg.Set(bottom_up), " real bottom up run";
61
62     "-nj", Arg.Set(no_jump), " disable jumping";
63
64     "-nc", Arg.Set(no_cache), " disable caching";
65
66     "-nw", Arg.Set(no_wrap_results), " do not wrap results in <xml_results/>";
67
68     "-p",  Arg.Set(do_perf), " dump perf counters (Linux only)";
69
70     "-index-type", Arg.Symbol ([ "default"; "swcsa"; "rlcsa" ],
71                                set_index_type),
72     " choose text index type";
73
74     "-r", Arg.Set_int(repeat),
75     " repeat query execution n time (benchmarking only, default 1)";
76
77     "-doc-stats", Arg.Set(docstats),
78     " Compute document statistics (performs full traversal)";
79
80     "-v", Arg.Set(verbose), " verbose mode"; ] @
81 IFNDEF NTRACE
82 THEN [
83     "-log", Arg.String (set_logger),
84     "<logger1:l1,...,loggern:ln> enable logging with the specified level. Valid loggers are: "
85       ^ (pretty_loggers ())
86      ]
87 ELSE []
88 END
89
90 let real_spec =
91   spec @ [
92     "-", Arg.Unit (fun () ->  anon_fun "-"), "";
93   ]
94
95
96 let parse_cmdline() =
97   let _ =
98     try
99       Arg.parse_argv Sys.argv real_spec anon_fun usage_msg;
100       if (!pos > 3 || !pos < 2) then raise (Arg.Bad "Invalid number of arguments")
101     with
102       Arg.Bad s | Arg.Help s ->
103         eprintf "%s\n" (Arg.usage_string spec usage_msg);
104         exit 1
105   in
106   Logger.set_verbose !verbose
107
108
109
110
111