Source file cmdliner_base.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
let strf = Printf.sprintf
let uid =
let c = ref 0 in
fun () ->
let id = !c in
incr c; if id > !c then assert false else id
let edit_distance s0 s1 =
let minimum (a : int) (b : int) (c : int) : int = min a (min b c) in
let s0,s1 = if String.length s0 <= String.length s1 then s0,s1 else s1,s0 in
let m = String.length s0 and n = String.length s1 in
let rec rows row0 row i = match i > n with
| true -> row0.(m)
| false ->
row.(0) <- i;
for j = 1 to m do
if s0.[j - 1] = s1.[i - 1] then row.(j) <- row0.(j - 1) else
row.(j) <- minimum (row0.(j - 1) + 1) (row0.(j) + 1) (row.(j - 1) + 1)
done;
rows row row0 (i + 1)
in
rows (Array.init (m + 1) (fun x -> x)) (Array.make (m + 1) 0) 1
let suggest s candidates =
let add (min, acc) name =
let d = edit_distance s name in
if d = min then min, (name :: acc) else
if d < min then d, [name] else
min, acc
in
let dist, suggs = List.fold_left add (max_int, []) candidates in
if dist < 3 then suggs else []
let err_empty_list = "empty list"
let err_incomplete_enum ss =
strf "Arg.enum: missing printable string for a value, other strings are: %s"
(String.concat ", " ss)
let pp = Format.fprintf
let pp_sp = Format.pp_print_space
let pp_str = Format.pp_print_string
let pp_char = Format.pp_print_char
let pp_text = Format.pp_print_text
let pp_lines ppf s =
let rec stop_at sat ~start ~max s =
if start > max then start else
if sat s.[start] then start else
stop_at sat ~start:(start + 1) ~max s
in
let sub s start stop ~max =
if start = stop then "" else
if start = 0 && stop > max then s else
String.sub s start (stop - start)
in
let is_nl c = c = '\n' in
let max = String.length s - 1 in
let rec loop start s = match stop_at is_nl ~start ~max s with
| stop when stop > max -> Format.pp_print_string ppf (sub s start stop ~max)
| stop ->
Format.pp_print_string ppf (sub s start stop ~max);
Format.pp_force_newline ppf ();
loop (stop + 1) s
in
loop 0 s
let pp_tokens ~spaces ppf s =
let is_space = function ' ' | '\n' | '\r' | '\t' -> true | _ -> false in
let i_max = String.length s - 1 in
let flush start stop = pp_str ppf (String.sub s start (stop - start + 1)) in
let rec skip_white i =
if i > i_max then i else
if is_space s.[i] then skip_white (i + 1) else i
in
let rec loop start i =
if i > i_max then flush start i_max else
if not (is_space s.[i]) then loop start (i + 1) else
let next_start = skip_white i in
(flush start (i - 1); if spaces then pp_sp ppf () else pp_char ppf ' ';
if next_start > i_max then () else loop next_start next_start)
in
loop 0 0
let quote s = strf "'%s'" s
let alts_str ?quoted alts =
let quote = match quoted with
| None -> strf "$(b,%s)"
| Some quoted -> if quoted then quote else (fun s -> s)
in
match alts with
| [] -> invalid_arg err_empty_list
| [a] -> (quote a)
| [a; b] -> strf "either %s or %s" (quote a) (quote b)
| alts ->
let rev_alts = List.rev alts in
strf "one of %s or %s"
(String.concat ", " (List.rev_map quote (List.tl rev_alts)))
(quote (List.hd rev_alts))
let err_multi_def ~kind name doc v v' =
strf "%s %s defined twice (doc strings are '%s' and '%s')"
kind name (doc v) (doc v')
let err_ambiguous ~kind s ~ambs =
strf "%s %s ambiguous and could be %s" kind (quote s)
(alts_str ~quoted:true ambs)
let err_unknown ?(dom = []) ?(hints = []) ~kind v =
let hints = match hints, dom with
| [], [] -> "."
| [], dom -> strf ", must be %s." (alts_str ~quoted:true dom)
| hints, _ -> strf ", did you mean %s?" (alts_str ~quoted:true hints)
in
strf "unknown %s %s%s" kind (quote v) hints
let err_no kind s = strf "no %s %s" (quote s) kind
let err_not_dir s = strf "%s is not a directory" (quote s)
let err_is_dir s = strf "%s is a directory" (quote s)
let err_element kind s exp =
strf "invalid element in %s ('%s'): %s" kind s exp
let err_invalid kind s exp = strf "invalid %s %s, %s" kind (quote s) exp
let err_invalid_val = err_invalid "value"
let err_sep_miss sep s =
err_invalid_val s (strf "missing a '%c' separator" sep)
type 'a parser = string -> [ `Ok of 'a | `Error of string ]
type 'a printer = Format.formatter -> 'a -> unit
type 'a conv = 'a parser * 'a printer
let some ?(none = "") (parse, print) =
let parse s = match parse s with `Ok v -> `Ok (Some v) | `Error _ as e -> e in
let print ppf v = match v with
| None -> Format.pp_print_string ppf none
| Some v -> print ppf v
in
parse, print
let some' ?none (parse, print) =
let parse s = match parse s with `Ok v -> `Ok (Some v) | `Error _ as e -> e in
let print ppf = function
| None -> (match none with None -> () | Some v -> print ppf v)
| Some v -> print ppf v
in
parse, print
let bool =
let parse s = try `Ok (bool_of_string s) with
| Invalid_argument _ ->
`Error (err_invalid_val s (alts_str ~quoted:true ["true"; "false"]))
in
parse, Format.pp_print_bool
let char =
let parse s = match String.length s = 1 with
| true -> `Ok s.[0]
| false -> `Error (err_invalid_val s "expected a character")
in
parse, pp_char
let parse_with t_of_str exp s =
try `Ok (t_of_str s) with Failure _ -> `Error (err_invalid_val s exp)
let int =
parse_with int_of_string "expected an integer", Format.pp_print_int
let int32 =
parse_with Int32.of_string "expected a 32-bit integer",
(fun ppf -> pp ppf "%ld")
let int64 =
parse_with Int64.of_string "expected a 64-bit integer",
(fun ppf -> pp ppf "%Ld")
let nativeint =
parse_with Nativeint.of_string "expected a processor-native integer",
(fun ppf -> pp ppf "%nd")
let float =
parse_with float_of_string "expected a floating point number",
Format.pp_print_float
let string = (fun s -> `Ok s), pp_str
let enum sl =
if sl = [] then invalid_arg err_empty_list else
let t = Cmdliner_trie.of_list sl in
let parse s = match Cmdliner_trie.find t s with
| `Ok _ as r -> r
| `Ambiguous ->
let ambs = List.sort compare (Cmdliner_trie.ambiguities t s) in
`Error (err_ambiguous ~kind:"enum value" s ~ambs)
| `Not_found ->
let alts = List.rev (List.rev_map (fun (s, _) -> s) sl) in
`Error (err_invalid_val s ("expected " ^ (alts_str ~quoted:true alts)))
in
let print ppf v =
let sl_inv = List.rev_map (fun (s,v) -> (v,s)) sl in
try pp_str ppf (List.assoc v sl_inv)
with Not_found -> invalid_arg (err_incomplete_enum (List.map fst sl))
in
parse, print
let file =
let parse s = match Sys.file_exists s with
| true -> `Ok s
| false -> `Error (err_no "file or directory" s)
in
parse, pp_str
let dir =
let parse s = match Sys.file_exists s with
| true -> if Sys.is_directory s then `Ok s else `Error (err_not_dir s)
| false -> `Error (err_no "directory" s)
in
parse, pp_str
let non_dir_file =
let parse s = match Sys.file_exists s with
| true -> if not (Sys.is_directory s) then `Ok s else `Error (err_is_dir s)
| false -> `Error (err_no "file" s)
in
parse, pp_str
let split_and_parse sep parse s =
let parse sub = match parse sub with
| `Error e -> failwith e | `Ok v -> v
in
let rec split accum j =
let i = try String.rindex_from s j sep with Not_found -> -1 in
if (i = -1) then
let p = String.sub s 0 (j + 1) in
if p <> "" then parse p :: accum else accum
else
let p = String.sub s (i + 1) (j - i) in
let accum' = if p <> "" then parse p :: accum else accum in
split accum' (i - 1)
in
split [] (String.length s - 1)
let list ?(sep = ',') (parse, pp_e) =
let parse s = try `Ok (split_and_parse sep parse s) with
| Failure e -> `Error (err_element "list" s e)
in
let rec print ppf = function
| v :: l -> pp_e ppf v; if (l <> []) then (pp_char ppf sep; print ppf l)
| [] -> ()
in
parse, print
let array ?(sep = ',') (parse, pp_e) =
let parse s = try `Ok (Array.of_list (split_and_parse sep parse s)) with
| Failure e -> `Error (err_element "array" s e)
in
let print ppf v =
let max = Array.length v - 1 in
for i = 0 to max do pp_e ppf v.(i); if i <> max then pp_char ppf sep done
in
parse, print
let split_left sep s =
try
let i = String.index s sep in
let len = String.length s in
Some ((String.sub s 0 i), (String.sub s (i + 1) (len - i - 1)))
with Not_found -> None
let pair ?(sep = ',') (pa0, pr0) (pa1, pr1) =
let parser s = match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some (v0, v1) ->
match pa0 v0, pa1 v1 with
| `Ok v0, `Ok v1 -> `Ok (v0, v1)
| `Error e, _ | _, `Error e -> `Error (err_element "pair" s e)
in
let printer ppf (v0, v1) = pp ppf "%a%c%a" pr0 v0 sep pr1 v1 in
parser, printer
let t2 = pair
let t3 ?(sep = ',') (pa0, pr0) (pa1, pr1) (pa2, pr2) =
let parse s = match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some (v0, s) ->
match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some (v1, v2) ->
match pa0 v0, pa1 v1, pa2 v2 with
| `Ok v0, `Ok v1, `Ok v2 -> `Ok (v0, v1, v2)
| `Error e, _, _ | _, `Error e, _ | _, _, `Error e ->
`Error (err_element "triple" s e)
in
let print ppf (v0, v1, v2) =
pp ppf "%a%c%a%c%a" pr0 v0 sep pr1 v1 sep pr2 v2
in
parse, print
let t4 ?(sep = ',') (pa0, pr0) (pa1, pr1) (pa2, pr2) (pa3, pr3) =
let parse s = match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some(v0, s) ->
match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some (v1, s) ->
match split_left sep s with
| None -> `Error (err_sep_miss sep s)
| Some (v2, v3) ->
match pa0 v0, pa1 v1, pa2 v2, pa3 v3 with
| `Ok v1, `Ok v2, `Ok v3, `Ok v4 -> `Ok (v1, v2, v3, v4)
| `Error e, _, _, _ | _, `Error e, _, _ | _, _, `Error e, _
| _, _, _, `Error e -> `Error (err_element "quadruple" s e)
in
let print ppf (v0, v1, v2, v3) =
pp ppf "%a%c%a%c%a%c%a" pr0 v0 sep pr1 v1 sep pr2 v2 sep pr3 v3
in
parse, print
let env_bool_parse s = match String.lowercase_ascii s with
| "" | "false" | "no" | "n" | "0" -> `Ok false
| "true" | "yes" | "y" | "1" -> `Ok true
| s ->
let alts = alts_str ~quoted:true ["true"; "yes"; "false"; "no" ] in
`Error (err_invalid_val s alts)