Add copyright notices and new function String.chomp
[ocaml.git] / ocamldoc / odoc_args.ml
bloba1c00962305b79cf1c52b9ee66125a558b1ddb87
1 (***********************************************************************)
2 (* OCamldoc *)
3 (* *)
4 (* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
5 (* *)
6 (* Copyright 2001 Institut National de Recherche en Informatique et *)
7 (* en Automatique. All rights reserved. This file is distributed *)
8 (* under the terms of the Q Public License version 1.0. *)
9 (* *)
10 (***********************************************************************)
12 (* cvsid $Id$ *)
14 (** Command-line arguments. *)
16 open Clflags
18 module M = Odoc_messages
20 type source_file =
21 Impl_file of string
22 | Intf_file of string
23 | Text_file of string
25 let include_dirs = Clflags.include_dirs
27 let bytecode_mode = ref true
29 class type doc_generator =
30 object
31 method generate : Odoc_module.t_module list -> unit
32 end
34 let doc_generator = ref (None : doc_generator option)
36 let merge_options = ref ([] : Odoc_types.merge_option list)
38 let out_file = ref M.default_out_file
40 let dot_include_all = ref false
42 let dot_types = ref false
44 let dot_reduce = ref false
46 let dot_colors = ref (List.flatten M.default_dot_colors)
48 let man_suffix = ref M.default_man_suffix
49 let man_section = ref M.default_man_section
51 let man_mini = ref false
53 (** Analysis of a string defining options. Return the list of
54 options according to the list giving associations between
55 [(character, _)] and a list of options. *)
56 let analyse_option_string l s =
57 List.fold_left
58 (fun acc -> fun ((c,_), v) ->
59 if String.contains s c then
60 acc @ v
61 else
62 acc)
66 (** Analysis of a string defining the merge options to be used.
67 Returns the list of options specified.*)
68 let analyse_merge_options s =
69 let l = [
70 (M.merge_description, [Odoc_types.Merge_description]) ;
71 (M.merge_author, [Odoc_types.Merge_author]) ;
72 (M.merge_version, [Odoc_types.Merge_version]) ;
73 (M.merge_see, [Odoc_types.Merge_see]) ;
74 (M.merge_since, [Odoc_types.Merge_since]) ;
75 (M.merge_deprecated, [Odoc_types.Merge_deprecated]) ;
76 (M.merge_param, [Odoc_types.Merge_param]) ;
77 (M.merge_raised_exception, [Odoc_types.Merge_raised_exception]) ;
78 (M.merge_return_value, [Odoc_types.Merge_return_value]) ;
79 (M.merge_custom, [Odoc_types.Merge_custom]) ;
80 (M.merge_all, Odoc_types.all_merge_options)
83 analyse_option_string l s
85 let classic = Clflags.classic
87 let dump = ref (None : string option)
89 let load = ref ([] : string list)
91 (** Allow arbitrary recursive types. *)
92 let recursive_types = Clflags.recursive_types
94 let verbose = ref false
96 (** Optional preprocessor command. *)
97 let preprocessor = Clflags.preprocessor
99 let sort_modules = ref false
101 let no_custom_tags = ref false
103 let no_stop = ref false
105 let remove_stars = ref false
107 let keep_code = ref false
109 let inverse_merge_ml_mli = ref false
111 let title = ref (None : string option)
113 let intro_file = ref (None : string option)
115 let with_parameter_list = ref false
117 let hidden_modules = ref ([] : string list)
119 let target_dir = ref Filename.current_dir_name
121 let css_style = ref None
123 let index_only = ref false
125 let colorize_code = ref false
127 let html_short_functors = ref false
129 let with_header = ref true
131 let with_trailer = ref true
133 let separate_files = ref false
135 let latex_titles = ref [
136 1, "section" ;
137 2, "subsection" ;
138 3, "subsubsection" ;
139 4, "paragraph" ;
140 5, "subparagraph" ;
143 let with_toc = ref true
145 let with_index = ref true
147 let esc_8bits = ref false
149 let info_section = ref "Objective Caml"
151 let info_entry = ref []
153 let files = ref []
155 let f_latex_title s =
157 let pos = String.index s ',' in
158 let n = int_of_string (String.sub s 0 pos) in
159 let len = String.length s in
160 let command = String.sub s (pos + 1) (len - pos - 1) in
161 latex_titles := List.remove_assoc n !latex_titles ;
162 latex_titles := (n, command) :: !latex_titles
163 with
164 Not_found
165 | Invalid_argument _ ->
166 incr Odoc_global.errors ;
167 prerr_endline (M.wrong_format s)
169 let add_hidden_modules s =
170 let l = Str.split (Str.regexp ",") s in
171 List.iter
172 (fun n ->
173 let name = Str.global_replace (Str.regexp "[ \n\r\t]+") "" n in
174 match name with
175 "" -> ()
176 | _ ->
177 match name.[0] with
178 'A'..'Z' -> hidden_modules := name :: !hidden_modules
179 | _ ->
180 incr Odoc_global.errors;
181 prerr_endline (M.not_a_module_name name)
185 let latex_value_prefix = ref M.default_latex_value_prefix
186 let latex_type_prefix = ref M.default_latex_type_prefix
187 let latex_exception_prefix = ref M.default_latex_exception_prefix
188 let latex_module_prefix = ref M.default_latex_module_prefix
189 let latex_module_type_prefix = ref M.default_latex_module_type_prefix
190 let latex_class_prefix = ref M.default_latex_class_prefix
191 let latex_class_type_prefix = ref M.default_latex_class_type_prefix
192 let latex_attribute_prefix = ref M.default_latex_attribute_prefix
193 let latex_method_prefix = ref M.default_latex_method_prefix
195 let set_doc_generator (dg_opt : doc_generator option) = doc_generator := dg_opt
197 (** The default html generator. Initialized in the parse function, to be used during the command line analysis.*)
198 let default_html_generator = ref (None : doc_generator option)
200 (** The default latex generator. Initialized in the parse function, to be used during the command line analysis.*)
201 let default_latex_generator = ref (None : doc_generator option)
203 (** The default texinfo generator. Initialized in the parse function, to be used during the command line analysis.*)
204 let default_texi_generator = ref (None : doc_generator option)
206 (** The default man pages generator. Initialized in the parse function, to be used during the command line analysis.*)
207 let default_man_generator = ref (None : doc_generator option)
209 (** The default dot generator. Initialized in the parse function, to be used during the command line analysis.*)
210 let default_dot_generator = ref (None : doc_generator option)
212 (** The default option list *)
213 let options = ref [
214 "-version", Arg.Unit (fun () -> print_string M.message_version ; print_newline () ; exit 0) , M.option_version ;
215 "-v", Arg.Unit (fun () -> verbose := true), M.verbose_mode ;
216 "-I", Arg.String (fun s -> include_dirs := (Misc.expand_directory Config.standard_library s) :: !include_dirs), M.include_dirs ;
217 "-pp", Arg.String (fun s -> preprocessor := Some s), M.preprocess ;
218 "-impl", Arg.String (fun s -> files := !files @ [Impl_file s]), M.option_impl ;
219 "-intf", Arg.String (fun s -> files := !files @ [Intf_file s]), M.option_intf ;
220 "-text", Arg.String (fun s -> files := !files @ [Text_file s]), M.option_text ;
221 "-rectypes", Arg.Set recursive_types, M.rectypes ;
222 "-nolabels", Arg.Unit (fun () -> classic := true), M.nolabels ;
223 "-warn-error", Arg.Set Odoc_global.warn_error, M.werr ;
224 "-hide-warnings", Arg.Clear Odoc_config.print_warnings, M.hide_warnings ;
225 "-o", Arg.String (fun s -> out_file := s), M.out_file ;
226 "-d", Arg.String (fun s -> target_dir := s), M.target_dir ;
227 "-sort", Arg.Unit (fun () -> sort_modules := true), M.sort_modules ;
228 "-no-stop", Arg.Set no_stop, M.no_stop ;
229 "-no-custom-tags", Arg.Set no_custom_tags, M.no_custom_tags ;
230 "-stars", Arg.Set remove_stars, M.remove_stars ;
231 "-inv-merge-ml-mli", Arg.Set inverse_merge_ml_mli, M.inverse_merge_ml_mli ;
232 "-keep-code", Arg.Set keep_code, M.keep_code^"\n" ;
234 "-dump", Arg.String (fun s -> dump := Some s), M.dump ;
235 "-load", Arg.String (fun s -> load := !load @ [s]), M.load^"\n" ;
237 "-t", Arg.String (fun s -> title := Some s), M.option_title ;
238 "-intro", Arg.String (fun s -> intro_file := Some s), M.option_intro ;
239 "-hide", Arg.String add_hidden_modules, M.hide_modules ;
240 "-m", Arg.String (fun s -> merge_options := !merge_options @ (analyse_merge_options s)),
241 M.merge_options ^
242 "\n\n *** choosing a generator ***\n";
244 (* generators *)
245 "-html", Arg.Unit (fun () -> set_doc_generator !default_html_generator), M.generate_html ;
246 "-latex", Arg.Unit (fun () -> set_doc_generator !default_latex_generator), M.generate_latex ;
247 "-texi", Arg.Unit (fun () -> set_doc_generator !default_texi_generator), M.generate_texinfo ;
248 "-man", Arg.Unit (fun () -> set_doc_generator !default_man_generator), M.generate_man ;
249 "-dot", Arg.Unit (fun () -> set_doc_generator !default_dot_generator), M.generate_dot ;
250 "-customdir", Arg.Unit (fun () -> Printf.printf "%s\n" Odoc_config.custom_generators_path; exit 0),
251 M.display_custom_generators_dir ;
252 "-i", Arg.String (fun s -> if !bytecode_mode then () else (prerr_endline (M.option_not_in_native_code "-i"); exit 1)),
253 M.add_load_dir ;
254 "-g", Arg.String (fun s -> if !bytecode_mode then () else (prerr_endline (M.option_not_in_native_code "-g"); exit 1)),
255 M.load_file ^
256 "\n\n *** HTML options ***\n";
258 (* html only options *)
259 "-all-params", Arg.Set with_parameter_list, M.with_parameter_list ;
260 "-css-style", Arg.String (fun s -> css_style := Some s), M.css_style ;
261 "-index-only", Arg.Set index_only, M.index_only ;
262 "-colorize-code", Arg.Set colorize_code, M.colorize_code ;
263 "-short-functors", Arg.Set html_short_functors, M.html_short_functors ^
264 "\n\n *** LaTeX options ***\n";
266 (* latex only options *)
267 "-noheader", Arg.Unit (fun () -> with_header := false), M.no_header ;
268 "-notrailer", Arg.Unit (fun () -> with_trailer := false), M.no_trailer ;
269 "-sepfiles", Arg.Set separate_files, M.separate_files ;
270 "-latextitle", Arg.String f_latex_title, M.latex_title latex_titles ;
271 "-latex-value-prefix", Arg.String (fun s -> latex_value_prefix := s), M.latex_value_prefix ;
272 "-latex-type-prefix", Arg.String (fun s -> latex_type_prefix := s), M.latex_type_prefix ;
273 "-latex-exception-prefix", Arg.String (fun s -> latex_exception_prefix := s), M.latex_exception_prefix ;
274 "-latex-attribute-prefix", Arg.String (fun s -> latex_attribute_prefix := s), M.latex_attribute_prefix ;
275 "-latex-method-prefix", Arg.String (fun s -> latex_method_prefix := s), M.latex_method_prefix ;
276 "-latex-module-prefix", Arg.String (fun s -> latex_module_prefix := s), M.latex_module_prefix ;
277 "-latex-module-type-prefix", Arg.String (fun s -> latex_module_type_prefix := s), M.latex_module_type_prefix ;
278 "-latex-class-prefix", Arg.String (fun s -> latex_class_prefix := s), M.latex_class_prefix ;
279 "-latex-class-type-prefix", Arg.String (fun s -> latex_class_type_prefix := s), M.latex_class_type_prefix ;
280 "-notoc", Arg.Unit (fun () -> with_toc := false),
281 M.no_toc ^
282 "\n\n *** texinfo options ***\n";
284 (* tex only options *)
285 "-noindex", Arg.Clear with_index, M.no_index ;
286 "-esc8", Arg.Set esc_8bits, M.esc_8bits ;
287 "-info-section", Arg.String ((:=) info_section), M.info_section ;
288 "-info-entry", Arg.String (fun s -> info_entry := !info_entry @ [ s ]),
289 M.info_entry ^
290 "\n\n *** dot options ***\n";
292 (* dot only options *)
293 "-dot-colors", Arg.String (fun s -> dot_colors := Str.split (Str.regexp_string ",") s), M.dot_colors ;
294 "-dot-include-all", Arg.Set dot_include_all, M.dot_include_all ;
295 "-dot-types", Arg.Set dot_types, M.dot_types ;
296 "-dot-reduce", Arg.Set dot_reduce, M.dot_reduce^
297 "\n\n *** man pages options ***\n";
299 (* man only options *)
300 "-man-mini", Arg.Set man_mini, M.man_mini ;
301 "-man-suffix", Arg.String (fun s -> man_suffix := s), M.man_suffix ;
302 "-man-section", Arg.String (fun s -> man_section := s), M.man_section ;
306 let add_option o =
307 let (s,_,_) = o in
308 let rec iter = function
309 [] -> [o]
310 | (s2,f,m) :: q ->
311 if s = s2 then
312 o :: q
313 else
314 (s2,f,m) :: (iter q)
316 options := iter !options
318 let parse ~html_generator ~latex_generator ~texi_generator ~man_generator ~dot_generator =
319 let anonymous f =
320 let sf =
321 if Filename.check_suffix f "ml" then
322 Impl_file f
323 else
324 if Filename.check_suffix f "mli" then
325 Intf_file f
326 else
327 if Filename.check_suffix f "txt" then
328 Text_file f
329 else
330 failwith (Odoc_messages.unknown_extension f)
332 files := !files @ [sf]
334 default_html_generator := Some html_generator ;
335 default_latex_generator := Some latex_generator ;
336 default_texi_generator := Some texi_generator ;
337 default_man_generator := Some man_generator ;
338 default_dot_generator := Some dot_generator ;
339 let _ = Arg.parse !options
340 anonymous
341 (M.usage^M.options_are)
343 (* we sort the hidden modules by name, to be sure that for example,
344 A.B is before A, so we will match against A.B before A in
345 Odoc_name.hide_modules.*)
346 hidden_modules := List.sort (fun a -> fun b -> - (compare a b)) !hidden_modules