Debian Codegraph @

texmf-dist/doc/support/cluttex/src/handle-options.sml

raw
1 functor HandleOptions (val showMessageAndFail : string -> 'a
2 val showUsage : unit -> 'a
3 val showVersion : unit -> 'a
4 ) : sig
5 val parse : AppOptions.initial_options * string list -> AppOptions.initial_options * string list
6 end = struct
7 open AppOptions
8 datatype 'a option_action = SIMPLE of 'a
9 | WITH_ARG of string -> 'a
10 | WITH_OPTIONAL_ARG of { default : string, action : string -> 'a }
11 datatype option_desc = SHORT of string
12 | LONG of string
13 fun testOption (_, []) = NONE
14 | testOption ((SHORT s, SIMPLE v), arg :: args) = if arg = s then
15 SOME (v, args)
16 else
17 NONE
18 | testOption ((SHORT s, WITH_ARG f), arg :: args)
19 = if arg = s then
20 case args of
21 [] => raise Fail ("argument missing after " ^ s)
22 | arg' :: args' => SOME (f arg', args') (* -x foo *)
23 else if String.isPrefix s arg then (* -xfoo *)
24 let val arg' = String.extract (arg, String.size s, NONE)
25 in SOME (f arg', args)
26 end
27 else
28 NONE
29 | testOption ((SHORT s, WITH_OPTIONAL_ARG { default, action }), arg :: args)
30 = if arg = s then
31 SOME (action default, args)
32 else if String.isPrefix s arg then (* -xfoo *)
33 let val arg' = String.extract (arg, String.size s, NONE)
34 in SOME (action arg', args)
35 end
36 else
37 NONE
38 | testOption ((LONG s, SIMPLE v), arg :: args) = if arg = s then
39 SOME (v, args)
40 else
41 NONE
42 | testOption ((LONG s, WITH_ARG f), arg :: args)
43 = if arg = s then
44 case args of
45 [] => raise Fail ("argument missing after " ^ s)
46 | arg' :: args' => SOME (f arg', args') (* -option foo *)
47 else if String.isPrefix (s ^ "=") arg then (* -option=foo *)
48 let val arg' = String.extract (arg, String.size s + 1, NONE)
49 in SOME (f arg', args)
50 end
51 else
52 NONE
53 | testOption ((LONG s, WITH_OPTIONAL_ARG { default, action }), arg :: args)
54 = if arg = s then
55 SOME (action default, args)
56 else if String.isPrefix (s ^ "=") arg then (* -option=foo *)
57 let val arg' = String.extract (arg, String.size s + 1, NONE)
58 in SOME (action arg', args)
59 end
60 else
61 NONE
62 fun parseOption (descs, []) = NONE
63 | parseOption (descs, args) = let fun go [] = NONE
64 | go (desc :: descs) = case testOption (desc, args) of
65 SOME r => SOME r
66 | NONE => go descs
67 in go descs
68 end
69 datatype option = OPT_ENGINE of string (* -e,--engine=ENGINE *)
70 | OPT_ENGINE_EXECUTABLE of string (* --engine-executable=EXECUTABLE *)
71 | OPT_OUTPUT of string (* -o,--output=OUTPUT *)
72 | OPT_FRESH (* --fresh *)
73 | OPT_MAX_ITERATIONS of string (* --max-iterations=N *)
74 | OPT_START_WITH_DRAFT (* --start-with-draft *)
75 | OPT_CHANGE_DIRECTORY of bool (* --change-directory,--no-change-directory *)
76 | OPT_WATCH of string (* --watch[=auto] *)
77 | OPT_HELP (* -h,-help,--help *)
78 | OPT_VERSION
79 | OPT_VERBOSE
80 | OPT_COLOR of string (* --color[=always] *)
81 | OPT_INCLUDEONLY of string
82 | OPT_MAKE_DEPENDS of string
83 | OPT_PRINT_OUTPUT_DIRECTORY
84 | OPT_PACKAGE_SUPPORT of string
85 | OPT_CHECK_DRIVER of string
86 | OPT_SOURCE_DATE_EPOCH of string
87 | OPT_SYNCTEX of string
88 | OPT_FILE_LINE_ERROR of bool
89 | OPT_INTERACTION of string
90 | OPT_HALT_ON_ERROR of bool
91 | OPT_SHELL_ESCAPE of ShellEscape.shell_escape
92 | OPT_JOBNAME of string
93 | OPT_FMT of string
94 | OPT_OUTPUT_DIRECTORY of string
95 | OPT_OUTPUT_FORMAT of string
96 | OPT_TEX_OPTION of string
97 | OPT_TEX_OPTIONS of string
98 | OPT_DVIPDFMX_OPTION of string
99 | OPT_DVIPDFMX_OPTIONS of string
100 | OPT_MAKEINDEX of string
101 | OPT_BIBTEX of string
102 | OPT_BIBER of string
103 | OPT_MAKEGLOSSARIES of string
104 | OPT_CONFIG_FILE of string
105 val optionDescs = [(SHORT "-e", WITH_ARG OPT_ENGINE)
106 ,(LONG "--engine", WITH_ARG OPT_ENGINE)
107 ,(LONG "--engine-executable", WITH_ARG OPT_ENGINE_EXECUTABLE)
108 ,(SHORT "-o", WITH_ARG OPT_OUTPUT)
109 ,(LONG "--output", WITH_ARG OPT_OUTPUT)
110 ,(LONG "--fresh", SIMPLE OPT_FRESH)
111 ,(LONG "--max-iterations", WITH_ARG OPT_MAX_ITERATIONS)
112 ,(LONG "--start-with-draft", SIMPLE OPT_START_WITH_DRAFT)
113 ,(LONG "--change-directory", SIMPLE (OPT_CHANGE_DIRECTORY true))
114 ,(LONG "--no-change-directory", SIMPLE (OPT_CHANGE_DIRECTORY false))
115 ,(LONG "--watch", WITH_OPTIONAL_ARG { action = OPT_WATCH, default = "auto" })
116 ,(SHORT "-h", SIMPLE OPT_HELP)
117 ,(LONG "-help", SIMPLE OPT_HELP)
118 ,(LONG "--help", SIMPLE OPT_HELP)
119 ,(SHORT "-v", SIMPLE OPT_VERSION)
120 ,(LONG "--version", SIMPLE OPT_VERSION)
121 ,(SHORT "-V", SIMPLE OPT_VERBOSE)
122 ,(LONG "--verbose", SIMPLE OPT_VERBOSE)
123 ,(LONG "--color", WITH_OPTIONAL_ARG { action = OPT_COLOR, default = "always" })
124 ,(LONG "--includeonly", WITH_ARG OPT_INCLUDEONLY)
125 ,(LONG "--make-depends", WITH_ARG OPT_MAKE_DEPENDS)
126 ,(LONG "--print-output-directory", SIMPLE OPT_PRINT_OUTPUT_DIRECTORY)
127 ,(LONG "--package-support", WITH_ARG OPT_PACKAGE_SUPPORT)
128 ,(LONG "--check-driver", WITH_ARG OPT_CHECK_DRIVER)
129 ,(LONG "--source-date-epoch", WITH_ARG OPT_SOURCE_DATE_EPOCH)
130 ,(LONG "-synctex", WITH_ARG OPT_SYNCTEX)
131 ,(LONG "--synctex", WITH_ARG OPT_SYNCTEX)
132 ,(LONG "-file-line-error", SIMPLE (OPT_FILE_LINE_ERROR true))
133 ,(LONG "--file-line-error", SIMPLE (OPT_FILE_LINE_ERROR true))
134 ,(LONG "-no-file-line-error", SIMPLE (OPT_FILE_LINE_ERROR false))
135 ,(LONG "--no-file-line-error", SIMPLE (OPT_FILE_LINE_ERROR false))
136 ,(LONG "-interaction", WITH_ARG OPT_INTERACTION)
137 ,(LONG "--interaction", WITH_ARG OPT_INTERACTION)
138 ,(LONG "-halt-on-error", SIMPLE (OPT_HALT_ON_ERROR true))
139 ,(LONG "--halt-on-error", SIMPLE (OPT_HALT_ON_ERROR true))
140 ,(LONG "-no-halt-on-error", SIMPLE (OPT_HALT_ON_ERROR false))
141 ,(LONG "--no-halt-on-error", SIMPLE (OPT_HALT_ON_ERROR false))
142 ,(LONG "-shell-escape", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.ALLOWED))
143 ,(LONG "--shell-escape", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.ALLOWED))
144 ,(LONG "-no-shell-escape", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.FORBIDDEN))
145 ,(LONG "--no-shell-escape", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.FORBIDDEN))
146 ,(LONG "-shell-restricted", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.RESTRICTED))
147 ,(LONG "--shell-restricted", SIMPLE (OPT_SHELL_ESCAPE ShellEscape.RESTRICTED))
148 ,(LONG "-jobname", WITH_ARG OPT_JOBNAME)
149 ,(LONG "--jobname", WITH_ARG OPT_JOBNAME)
150 ,(LONG "-fmt", WITH_ARG OPT_FMT)
151 ,(LONG "--fmt", WITH_ARG OPT_FMT)
152 ,(LONG "-output-directory", WITH_ARG OPT_OUTPUT_DIRECTORY)
153 ,(LONG "--output-directory", WITH_ARG OPT_OUTPUT_DIRECTORY)
154 ,(LONG "-output-format", WITH_ARG OPT_OUTPUT_FORMAT)
155 ,(LONG "--output-format", WITH_ARG OPT_OUTPUT_FORMAT)
156 ,(LONG "--tex-option", WITH_ARG OPT_TEX_OPTION)
157 ,(LONG "--tex-options", WITH_ARG OPT_TEX_OPTIONS)
158 ,(LONG "--dvipdfmx-option", WITH_ARG OPT_DVIPDFMX_OPTION)
159 ,(LONG "--dvipdfmx-options", WITH_ARG OPT_DVIPDFMX_OPTIONS)
160 ,(LONG "--makeindex", WITH_ARG OPT_MAKEINDEX)
161 ,(LONG "--bibtex", WITH_ARG OPT_BIBTEX)
162 ,(LONG "--biber", WITH_OPTIONAL_ARG { action = OPT_BIBER, default = "biber" })
163 ,(LONG "--makeglossaries", WITH_OPTIONAL_ARG { action = OPT_MAKEGLOSSARIES, default = "makeglossaries" })
164 ,(LONG "--config-file", WITH_ARG OPT_CONFIG_FILE)
165 ]
166 fun parse (opts : initial_options, args)
167 = case parseOption (optionDescs, args) of
168 SOME (OPT_ENGINE engine, args) => (case #engine opts of
169 NONE => parse ({ opts where engine = SOME engine }, args)
170 | SOME _ => showMessageAndFail "multiple --engine options"
171 )
172 | SOME (OPT_ENGINE_EXECUTABLE executable, args) => (case #engine_executable opts of
173 NONE => parse ({ opts where engine_executable = SOME executable }, args)
174 | SOME _ => showMessageAndFail "multiple --engine-executable options"
175 )
176 | SOME (OPT_OUTPUT output, args) => (case #output opts of
177 NONE => parse ({ opts where output = SOME output }, args)
178 | SOME _ => showMessageAndFail "multiple --output options"
179 )
180 | SOME (OPT_FRESH, args) => (case #fresh opts of
181 false => parse ({ opts where fresh = true }, args)
182 | true => showMessageAndFail "multiple --fresh options"
183 )
184 | SOME (OPT_MAX_ITERATIONS n, args) => (case #max_iterations opts of
185 NONE => (case Int.fromString n of
186 SOME n => parse ({ opts where max_iterations = SOME n }, args)
187 | NONE => showMessageAndFail "invalid value for --max-iterations option"
188 )
189 | SOME _ => showMessageAndFail "multiple --max-iterations options"
190 )
191 | SOME (OPT_START_WITH_DRAFT, args) => (case #start_with_draft opts of
192 false => parse ({ opts where start_with_draft = true }, args)
193 | true => showMessageAndFail "multiple --start-with-draft options"
194 )
195 | SOME (OPT_WATCH engine, args) => (case #watch opts of
196 NONE => (case WatchEngine.fromString engine of
197 SOME engine => parse ({ opts where watch = SOME engine }, args)
198 | NONE => showMessageAndFail "invalid value for --watch option"
199 )
200 | SOME _ => showMessageAndFail "multiple --watch options"
201 )
202 | SOME (OPT_HELP, args) => showUsage ()
203 | SOME (OPT_VERSION, args) => showVersion ()
204 | SOME (OPT_VERBOSE, args) => ( Message.beMoreVerbose ()
205 ; parse (opts, args)
206 )
207 | SOME (OPT_COLOR mode, args) => (case #color opts of
208 NONE => (case ColorMode.fromString mode of
209 SOME mode => ( Message.setColors mode
210 ; parse ({ opts where color = SOME mode }, args)
211 )
212 | NONE => showMessageAndFail "invalid value for --color option"
213 )
214 | SOME _ => showMessageAndFail "multiple --color options"
215 )
216 | SOME (OPT_CHANGE_DIRECTORY x, args) => (case #change_directory opts of
217 NONE => parse ({ opts where change_directory = SOME x }, args)
218 | SOME _ => showMessageAndFail "multiple --change-directory options"
219 )
220 | SOME (OPT_INCLUDEONLY x, args) => (case #includeonly opts of
221 NONE => parse ({ opts where includeonly = SOME x }, args)
222 | SOME _ => showMessageAndFail "multiple --includeonly options"
223 )
224 | SOME (OPT_MAKE_DEPENDS x, args) => (case #make_depends opts of
225 NONE => parse ({ opts where make_depends = SOME x }, args)
226 | SOME _ => showMessageAndFail "multiple --make-depends options"
227 )
228 | SOME (OPT_PRINT_OUTPUT_DIRECTORY, args) => (case #print_output_directory opts of
229 false => parse ({ opts where print_output_directory = true }, args)
230 | true => showMessageAndFail "multiple --print-output-directory options"
231 )
232 | SOME (OPT_PACKAGE_SUPPORT s, args) => let val packages = String.tokens (fn c => c = #"," orelse Char.isSpace c) s
233 val ps = List.foldl (fn ("minted", ps) => { ps where minted = true }
234 | ("epstopdf", ps) => { ps where epstopdf = true }
235 | ("pdfx", ps) => { ps where pdfx = true }
236 | (pkg, ps) => ( if Message.getVerbosity () >= 1 then
237 Message.warn ("ClutTeX provides no special support for '" ^ pkg ^ "'.")
238 else
239 ()
240 ; ps
241 )
242 ) (#package_support opts) packages
243 in parse ({ opts where package_support = ps }, args)
244 end
245 | SOME (OPT_CHECK_DRIVER driver, args) => (case #check_driver opts of
246 NONE => (case DviDriver.fromString driver of
247 SOME driver => parse ({ opts where check_driver = SOME driver }, args)
248 | NONE => showMessageAndFail "invalid value for --check-driver option"
249 )
250 | SOME _ => showMessageAndFail "multiple --check-driver options"
251 )
252 | SOME (OPT_SOURCE_DATE_EPOCH time, args) => (case #source_date_epoch opts of
253 NONE => (case SourceDateEpoch.fromString time of
254 SOME time => parse ({ opts where source_date_epoch = SOME time }, args)
255 | NONE => showMessageAndFail "invalid value for --source-date-epoch option"
256 )
257 | SOME _ => showMessageAndFail "multiple --source-date-epoch options"
258 )
259 | SOME (OPT_SYNCTEX x, args) => (case #synctex opts of
260 NONE => parse ({ opts where synctex = SOME x }, args)
261 | SOME _ => showMessageAndFail "multiple --synctex options"
262 )
263 | SOME (OPT_FILE_LINE_ERROR x, args) => parse ({ opts where file_line_error = x }, args)
264 | SOME (OPT_INTERACTION x, args) => (case #interaction opts of
265 NONE => (case InteractionMode.fromString x of
266 SOME interaction => parse ({ opts where interaction = SOME interaction }, args)
267 | NONE => showMessageAndFail "invalid argument for --interaction"
268 )
269 | SOME _ => showMessageAndFail "multiple --interaction options"
270 )
271 | SOME (OPT_HALT_ON_ERROR x, args) => parse ({ opts where halt_on_error = x }, args)
272 | SOME (OPT_SHELL_ESCAPE se, args) => (case #shell_escape opts of
273 NONE => parse ({ opts where shell_escape = SOME se }, args)
274 | SOME _ => showMessageAndFail "multiple --(no-)shell-escape / --shell-restricted options"
275 )
276 | SOME (OPT_JOBNAME x, args) => (case #jobname opts of
277 NONE => parse ({ opts where jobname = SOME x }, args)
278 | SOME _ => showMessageAndFail "multiple --jobname options"
279 )
280 | SOME (OPT_FMT x, args) => (case #fmt opts of
281 NONE => parse ({ opts where fmt = SOME x }, args)
282 | SOME _ => showMessageAndFail "multiple --fmt options"
283 )
284 | SOME (OPT_OUTPUT_DIRECTORY x, args) => (case #output_directory opts of
285 NONE => parse ({ opts where output_directory = SOME x }, args)
286 | SOME _ => showMessageAndFail "multiple --output-directory options"
287 )
288 | SOME (OPT_OUTPUT_FORMAT format, args) => (case #output_format opts of
289 NONE => (case OutputFormat.fromString format of
290 SOME format => parse ({ opts where output_format = SOME format }, args)
291 | NONE => showMessageAndFail "invalid value for --output-format option"
292 )
293 | SOME _ => showMessageAndFail "multiple --output-format options"
294 )
295 | SOME (OPT_TEX_OPTION x, args) => let val x = ShellUtil.escape x
296 in parse ({ opts where tex_extraoptions = x :: #tex_extraoptions opts }, args)
297 end
298 | SOME (OPT_TEX_OPTIONS x, args) => parse ({ opts where tex_extraoptions = x :: #tex_extraoptions opts }, args)
299 | SOME (OPT_DVIPDFMX_OPTION x, args) => let val x = ShellUtil.escape x
300 in parse ({ opts where dvipdfmx_extraoptions = x :: #dvipdfmx_extraoptions opts }, args)
301 end
302 | SOME (OPT_DVIPDFMX_OPTIONS x, args) => parse ({ opts where tex_extraoptions = x :: #dvipdfmx_extraoptions opts }, args)
303 | SOME (OPT_MAKEINDEX x, args) => (case #makeindex opts of
304 NONE => parse ({ opts where makeindex = SOME x }, args)
305 | SOME _ => showMessageAndFail "multiple --makeindex options"
306 )
307 | SOME (OPT_BIBTEX x, args) => (case #bibtex_or_biber opts of
308 NONE => parse ({ opts where bibtex_or_biber = SOME (BIBTEX x) }, args)
309 | SOME _ => showMessageAndFail "multiple --bibtex / --biber options"
310 )
311 | SOME (OPT_BIBER x, args) => (case #bibtex_or_biber opts of
312 NONE => parse ({ opts where bibtex_or_biber = SOME (BIBER x) }, args)
313 | SOME _ => showMessageAndFail "multiple --bibtex / --biber options"
314 )
315 | SOME (OPT_MAKEGLOSSARIES x, args) => (case #makeglossaries opts of
316 NONE => parse ({ opts where makeglossaries = SOME x }, args)
317 | SOME _ => showMessageAndFail "multiple --makeglossaries options"
318 )
319 | SOME (OPT_CONFIG_FILE x, args) => (case #config_file opts of
320 NONE => parse ({ opts where config_file = SOME x }, args)
321 | SOME _ => showMessageAndFail "multiple --config-file options"
322 )
323 | NONE => (case args of
324 "--" :: args => (opts, args)
325 | arg :: args' =>
326 if String.isPrefix "-" arg then
327 showMessageAndFail ("Unrecognized option: " ^ arg ^ ".\n")
328 else
329 (opts, args)
330 | [] => showUsage () (* showMessageAndFail "No input given. Try --help.\n" *)
331 )
332 end;