Debian Codegraph @

tools/coqdoc/cmdArgs.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Command-line argument parsing *)
12 open Common
13
14 let banner () =
15 Printf.eprintf "This is rocq doc version %s\n" Coq_config.version;
16 flush stderr
17
18 let normalize_path p =
19 (* We use the Unix subsystem to normalize a physical path (relative
20 or absolute) and get rid of symbolic links, relative links (like
21 ./ or ../ in the middle of the path; it's tricky but it
22 works... *)
23 (* Rq: Sys.getcwd () returns paths without '/' at the end *)
24 let orig = Sys.getcwd () in
25 Sys.chdir p;
26 let res = Sys.getcwd () in
27 Sys.chdir orig;
28 res
29
30 let normalize_filename f =
31 let basename = Filename.basename f in
32 let dirname = Filename.dirname f in
33 normalize_path dirname, basename
34
35 let rec name_of_path p name dirname suffix =
36 if p = dirname then String.concat "." (if name = "" then suffix else (name::suffix))
37 else
38 let subdir = Filename.dirname dirname in
39 if subdir = dirname then raise Not_found
40 else name_of_path p name subdir (Filename.basename dirname::suffix)
41
42
43 let coq_module filename =
44 let bfname = Filename.chop_extension filename in
45 let dirname, fname = normalize_filename bfname in
46 let _ = match Unicode.ident_refutation fname with
47 | Some err -> raise (Arg.Bad (Printf.sprintf "Not a valid filename: %s" fname))
48 | None -> () in
49 let rec change_prefix = function
50 (* Follow coqc: if in scope of -R, substitute logical name *)
51 (* otherwise, keep only base name *)
52 | [] -> fname
53 | (p, name) :: rem ->
54 try name_of_path p name dirname [fname]
55 with Not_found -> change_prefix rem
56 in
57 change_prefix !prefs.paths
58
59 let what_file f =
60 FileUtil.check_if_file_exists f;
61 if Filename.check_suffix f ".v" || Filename.check_suffix f ".g" then
62 Vernac_file (f, coq_module f)
63 else if Filename.check_suffix f ".tex" then
64 Latex_file f
65 else
66 raise (Arg.Bad (Printf.sprintf "Expected a .v, .g, or .tex file: %s" f))
67
68 let arg_set f = Arg.Unit (fun () -> prefs := f !prefs)
69 let arg_string f = Arg.String (fun s -> prefs := f !prefs s)
70 let arg_file f = Arg.String (fun s -> FileUtil.check_if_file_exists s; prefs := f !prefs s)
71 let arg_int f = Arg.Int (fun d -> prefs := f !prefs d)
72
73 let current = Arg.current
74
75 let argv = ref [||]
76
77 (* TODO: replace these hacks with Arg.Rest_all, when rocq moves to a newer version of OCaml stdlib *)
78 let arg_path f = Arg.String (fun s ->
79 if Array.length !argv < !current + 3 ||
80 CString.is_prefix "-" !argv.(!current + 2) then
81 raise (Arg.Bad ("Two arguments expected: <dir> and <name>"))
82 else
83 Arg.current := !current + 1;
84 prefs := f !prefs (normalize_path s, !argv.(!current + 1)))
85 let arg_url_path f = Arg.String (fun s ->
86 if Array.length !argv < !current + 3 ||
87 CString.is_prefix "-" !argv.(!current + 2) then
88 raise (Arg.Bad ("Two arguments expected: <url> and <path>"))
89 else
90 current := !current + 1;
91 f s !argv.(!current + 1))
92
93 let args_options = Arg.align [
94 "--html", arg_set (fun p -> { p with targetlang = HTML }),
95 " Produce a HTML document (default)";
96 "--latex", arg_set (fun p -> { p with targetlang = LaTeX }),
97 " Produce a LaTeX document";
98 "--texmacs",arg_set (fun p -> { p with targetlang = TeXmacs }),
99 " Produce a TeXmacs document";
100 "--raw", arg_set (fun p -> { p with targetlang = Raw }),
101 " Produce a text document";
102 "--dvi", arg_set (fun p -> { { p with targetlang = LaTeX }
103 with compile_targets = Dvi :: !prefs.compile_targets }),
104 " Output the DVI";
105 "--ps", arg_set (fun p -> { { p with targetlang = LaTeX }
106 with compile_targets = Ps :: !prefs.compile_targets }),
107 " Output the PostScript";
108 "--pdf", arg_set (fun p -> { { p with targetlang = LaTeX }
109 with compile_targets = Pdf :: !prefs.compile_targets }),
110 " Output the Pdf";
111 "--stdout", arg_set (fun p -> { p with out_to = StdOut }),
112 " Write output to stdout";
113 "-o", arg_string (fun p f -> { { p with out_to = File (Filename.basename f) }
114 with output_dir = Filename.dirname f }),
115 "<file> Write output to file <file>";
116 "--output", arg_string (fun p f -> { { p with out_to = File (Filename.basename f) }
117 with output_dir = Filename.dirname f }),
118 "<file> Write output to file <file>";
119 "-d", arg_string (fun p f -> { p with output_dir = f }),
120 "<dir> Output files into directory <dir>";
121 "--directory", arg_string (fun p f -> { p with output_dir = f }),
122 "<dir> Output files into directory <dir>";
123 "-g", arg_set (fun p -> { p with gallina = true }),
124 " Skip proofs (gallina)";
125 "--gallina", arg_set (fun p -> { p with gallina = true }),
126 " Skip proofs";
127 "-s", arg_set (fun p -> { p with short = true }),
128 " No titles for files (short)";
129 "--short", arg_set (fun p -> { p with short = true }),
130 " No titles for files";
131 "-l", arg_set (fun p -> { { p with gallina = true } with light = true }),
132 " Light mode (only defs and statements)";
133 "--light", arg_set (fun p -> { { p with gallina = true } with light = true }),
134 " Light mode (only defs and statements)";
135 "-t", arg_string (fun p s -> { p with title = s }),
136 "<string> Give a title to the document";
137 "--title", arg_string (fun p s -> { p with title = s }),
138 "<string> Give a title to the document";
139 "--body-only", arg_set (fun p -> { p with header_trailer = false }),
140 " Suppress LaTeX/HTML header and trailer";
141 "--no-preamble", arg_set (fun p -> { p with header_trailer = false }),
142 " Suppress LaTeX/HTML header and trailer";
143 "--with-header", arg_file (fun p f -> { p with header_trailer = true;
144 header_file = Some f }),
145 "<file> Prepend <file> as html header";
146 "--with-footer", arg_file (fun p f -> { p with header_trailer = true;
147 footer_file = Some f }),
148 "<file> append <file> as html footer";
149 "--no-index", arg_set (fun p -> { p with index = false }),
150 " Do not output the index";
151 "--binder-index", arg_set (fun p -> { p with binder_index = true }),
152 " Include variable binders in index";
153 "--multi-index", arg_set (fun p -> { p with multi_index = true }),
154 " Index split in multiple files";
155 "--index", arg_string (fun p s -> { p with index_name = s }),
156 "<string> Set index name to <string> (default is index)";
157 "--toc", arg_set (fun p -> { p with toc = true }),
158 " Output a table of contents";
159 "--table-of-contents", arg_set (fun p -> { p with toc = true }),
160 " Output a table of contents";
161 "--vernac-file",
162 arg_file (fun p f -> { p with files = Vernac_file (f, coq_module f) :: !prefs.files }),
163 "<file> consider <file> as a .v file";
164 "--tex-file", arg_file (fun p f -> { p with files = Latex_file f :: !prefs.files }),
165 "<file> Consider <file> as a .tex file";
166 "-p", Arg.String (fun f -> Output.push_in_preamble f),
167 "<string> Insert <string> in LaTeX preamble";
168 "--preamble", Arg.String (fun f -> Output.push_in_preamble f),
169 "<string> Insert <string> in LaTeX preamble";
170 "--files-from", arg_file (fun p f -> { p with
171 files = List.append (List.map what_file (FileUtil.files_from_file f)) !prefs.files }),
172 "<file> Read file names to process in <file>";
173 "--files", arg_file (fun p f -> { p with
174 files = List.append (List.map what_file (FileUtil.files_from_file f)) !prefs.files }),
175 "<file> Read file names to process in <file>";
176 "--glob-from", arg_file (fun p f -> { p with glob_source = GlobFile f }),
177 "<file> Read globalization information from <file>";
178 "--no-glob", arg_set (fun p -> { p with glob_source = NoGlob }),
179 " Don't use any globalization information (no links will be inserted at identifiers)";
180 "-q", arg_set (fun p -> { p with quiet = true }), " Quiet mode (default)";
181 "--quiet", arg_set (fun p -> { p with quiet = true }), " Quiet mode (default)";
182 "--verbose", arg_set (fun p -> { p with quiet = false }), " Verbose mode";
183 "--no-externals", arg_set (fun p -> { p with externals = false }),
184 " No links to Rocq standard library";
185 "--external", arg_url_path (fun url lp -> Index.add_external_library lp url),
186 "<url> <d>  Set URL for external library <d>";
187 "--coqlib_url", arg_string (fun p u -> { p with coqlib_url = u }),
188 "<url> Set URL for Rocq standard library (default: " ^ Coq_config.wwwstdlib ^ ")";
189 "--coqlib", arg_string (fun p d -> { p with coqlib = Some d }),
190 "<dir> Set the path where Rocq files are installed";
191 "-R", arg_path (fun p l -> { p with paths = l :: !prefs.paths }),
192 "<dir> <coqdir>  Map physical dir to Rocq dir";
193 "-Q", arg_path (fun p l -> { p with paths = l :: !prefs.paths }),
194 "<dir> <coqdir>  Map physical dir to Rocq dir";
195 "--latin1", arg_set (fun p -> {p with encoding = { charset = "iso-8859-1";
196 inputenc = "latin1";
197 latin1 = true;
198 utf8 = false } }),
199 " Set ISO-8859-1 mode";
200 "--utf8", arg_set (fun p -> {p with encoding = { charset = "utf-8";
201 inputenc = "utf8x";
202 latin1 = false;
203 utf8 = true } }),
204 " Set UTF-8 mode";
205 "--charset", arg_string (fun p s -> { p with encoding = { !prefs.encoding with charset = s } }),
206 "<string> Set HTML charset";
207 "--inputenc", arg_string (fun p s -> { p with encoding = { !prefs.encoding with inputenc = s } }),
208 "<string> Set LaTeX input encoding";
209 "--interpolate", arg_set (fun p -> {p with interpolate = true }),
210 " Try to typeset identifiers in comments using definitions in the same module";
211 "--raw-comments", arg_set (fun p -> {p with raw_comments = true }),
212 " Raw comments";
213 "--parse-comments", arg_set (fun p -> {p with parse_comments = true }),
214 " Parse regular comments";
215 "--plain-comments", arg_set (fun p -> {p with plain_comments = true }),
216 " Consider comments as non-literate text";
217 "--toc-depth", arg_int (fun p d -> { p with toc_depth = Some d }),
218 "<int> Don't include TOC entries for sections below level <int>";
219 "--no-lib-name", arg_set (fun p -> { p with lib_name = "" }),
220 " Don't display \"Library\" before library names in the toc";
221 "--lib-name", arg_string (fun p s -> { p with lib_name = s }),
222 "<string> Call top level toc entries <string> instead of \"Library\"";
223 "--lib-subtitles", arg_set (fun p -> { p with lib_subtitles = true }),
224 " First line comments of the form (** * ModuleName : text *) will be interpreted as subtitles";
225 "--inline-notmono", arg_set (fun p -> { p with inline_notmono = true }),
226 " Use a proportional width font for inline code (possibly with a different color)";
227 "--version", Arg.Unit (fun () -> banner()), " Display rocq doc version";
228 ]
229
230 let add_input_files f = prefs := { !prefs with files = what_file f :: !prefs.files }
231 let usage_msg = "rocq doc [options] <input file>...\nAvailable options are:"
232
233 let single_hyphen_opts =
234 ["-html"; "-latex"; "-texmacs"; "-raw"; "-dvi"; "-ps"; "-pdf"; "-stdout"; "-output"; "-directory"; "-gallina"; "-short"; "-light"; "-title"; "-body-only"; "-no-preamble"; "-with-header"; "-with-footer"; "-no-index"; "-multi-index"; "-index"; "-toc"; "-table-of-contents"; "-vernac-file"; "-tex-file"; "-preamble"; "-files-from"; "-files"; "-glob-from"; "-no-glob"; "-quiet"; "-verbose"; "-no-externals"; "-external"; "-coqlib_url"; "-coqlib"; "-latin1"; "-utf8"; "-charset"; "-inputenc"; "-interpolate"; "-raw-comments"; "-parse-comments"; "-plain-comments"; "-toc-depth"; "-no-lib-name"; "-lib-name"; "-lib-subtitles"; "-inline-notmono"; "-version"]
235
236 let deprecated_mapper_opts =
237 [("-noindex", "--no-index"); ("-nopreamble", "--no-preamble"); ("-noexternals", "--no-externals"); ("-V", "--version")]
238
239 let translate_arg s =
240 match List.find_opt (fun m -> m = s) single_hyphen_opts with
241 | Some _ -> Printf.sprintf "-%s" s
242 | None -> (match List.assoc_opt s deprecated_mapper_opts with
243 | Some b -> b
244 | None -> s)
245
246 let parse_args ~prog args =
247 (* Deprecated options *)
248 let new_argv = List.map translate_arg args in
249 let new_argv = Array.of_list (prog::new_argv) in
250 argv := new_argv;
251 current := 0;
252 try
253 Arg.parse_argv new_argv args_options add_input_files usage_msg
254 with
255 | Arg.Bad s -> Printf.eprintf "%s" s
256 | Arg.Help s -> Printf.printf "%s" s