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