Debian Codegraph @

src/Util/Arg.v

raw
1 (** Coq version of OCaml's Arg module *)
2 From Coq Require Import List.
3 From Coq Require Import Ascii.
4 From Coq Require Import String.
5 From Coq Require Import ZArith.
6 From Coq Require Import OctalString.
7 From Coq Require Import HexString.
8 From Coq Require Import BinaryString.
9 Require Import Crypto.Util.Strings.Ascii.
10 Require Import Crypto.Util.Strings.String.
11 Require Import Crypto.Util.Strings.Decimal.
12 Require Import Crypto.Util.Strings.Show.
13 Require Import Crypto.Util.ListUtil.
14 Require Import Crypto.Util.ErrorT.
15 Require Import Crypto.Util.Option.
16 Require Import Crypto.Util.Notations.
17 Import ListNotations.
18 Local Open Scope string_scope.
19 Local Open Scope list_scope.
20
21 (** Adapted from https://caml.inria.fr/pub/docs/manual-ocaml/libref/Arg.html *)
22
23 Inductive base_system := bin | hex | oct | dec.
24
25 Inductive spec :=
26 | Unit
27 | Bool
28 | String
29 | Int (b : list base_system)
30 | Nat (b : list base_system)
31 | Tuple (ls : list spec)
32 | Symbol (syms : list string)
33 | CustomSymbol {dataT : Type} (syms : list (string * dataT))
34 | Custom {dataT : Type} (parse : string -> option dataT) (expected_descr : string)
35 .
36
37 Inductive key := long_key (_ : string) | short_key (_ : string).
38 Definition doc := list string.
39 Definition usage_msg := list string.
40
41 Local Notation type_of_list l ls := (List.fold_left (fun a b : Type => prod a b) ls l).
42 Local Notation type_of_list' ls
43 := match ls return Type with
44 | [] => unit
45 | l :: ls' => type_of_list l ls'
46 end.
47
48 Fixpoint spec_data (s : spec) : Type
49 := match s with
50 | Unit => unit
51 | Bool => bool
52 | String => string
53 | Int b => base_system * Z
54 | Nat b => base_system * nat
55 | Tuple nil => unit
56 | Tuple (l :: ls) => type_of_list (spec_data l) (List.map spec_data ls)
57 | Symbol syms => string
58 | CustomSymbol dataT syms => dataT
59 | Custom dataT parse err_msg => dataT
60 end.
61
62
63 Definition spec_data_with_key (s : spec) : Type
64 := list (string * spec_data s).
65
66 Definition opt_spec_data (opt : bool) (s : spec) : Type
67 := if opt then option (spec_data s) else spec_data s.
68
69 Definition keyed_spec_list_data (s : list (list key * spec * doc)) : Type
70 := type_of_list' (List.map (fun '(_, s, _) => spec_data_with_key s) s).
71
72 Definition spec_list_data (opt : bool) (s : list (string * spec * doc)) : Type
73 := type_of_list' (List.map (fun '(_, s, _) => opt_spec_data opt s) s).
74
75
76 Fixpoint update_type_of_list (T1 T2 : Type) Ts (f : T1 -> T2) {struct Ts}
77 : type_of_list T1 Ts -> type_of_list T2 Ts
78 := match Ts return type_of_list T1 Ts -> type_of_list T2 Ts with
79 | nil => f
80 | cons T Ts
81 => @update_type_of_list _ _ Ts (fun '(t1, t) => (f t1, t))
82 end.
83
84 Fixpoint update_type_of_list2 (T1 T2 T : Type) Ts {struct Ts}
85 : (type_of_list T1 Ts -> type_of_list T2 Ts) -> type_of_list (T * T1)%type Ts -> type_of_list (T * T2)%type Ts
86 := match Ts return (type_of_list T1 Ts -> type_of_list T2 Ts) -> type_of_list (T * T1)%type Ts -> type_of_list (T * T2)%type Ts with
87 | nil => fun f '(t, t1) => (t, f t1)
88 | cons T' Ts
89 => fun f v
90 => let v := @update_type_of_list
91 _ _ Ts (fun '(a, b, c) => (a, (b, c)))
92 v in
93 @update_type_of_list
94 _ _ Ts (fun '(a, (b, c)) => (a, b, c))
95 (@update_type_of_list2 _ _ _ Ts f v)
96 end.
97
98 Definition cons_type_of_list {x x' xs} (v : x)
99 : type_of_list x' xs -> type_of_list x (x' :: xs)
100 := update_type_of_list _ _ _ (@pair _ _ v).
101
102 Definition cons_type_of_list' {x xs} (v : x)
103 : type_of_list' xs -> type_of_list x xs
104 := match xs with
105 | [] => fun _ => v
106 | _ :: _ => @cons_type_of_list _ _ _ v
107 end.
108
109 Fixpoint uncons_type_of_list {l ls}
110 : type_of_list l ls -> l * type_of_list' ls
111 := match ls return type_of_list l ls -> l * type_of_list' ls with
112 | [] => fun v => (v, tt)
113 | l' :: ls
114 => fun v
115 => let '(v, v', vs) := @uncons_type_of_list (l * l')%type ls v in
116 (v, cons_type_of_list' v' vs)
117 end.
118
119 Fixpoint split_type_of_list_' {l1 ls1 ls2} {struct ls1}
120 : type_of_list l1 (ls1 ++ ls2) -> type_of_list l1 ls1 * type_of_list' ls2
121 := match ls1 with
122 | [] => uncons_type_of_list
123 | l1' :: ls1 => @split_type_of_list_' (l1 * l1')%type ls1 ls2
124 end.
125
126 Definition split_type_of_list' {ls1 ls2}
127 : type_of_list' (ls1 ++ ls2) -> type_of_list' ls1 * type_of_list' ls2
128 := match ls1 with
129 | [] => @pair _ _ tt
130 | _ :: _ => split_type_of_list_'
131 end.
132
133 Local Definition make_symlist prefix sep suffix l
134 := match l with
135 | nil => "<none>"
136 | h :: t => List.fold_left (fun x y => x ++ sep ++ y) t (prefix ++ h) ++ suffix
137 end%string.
138
139 Local Definition doc_length (d : doc) : nat := List.fold_right Nat.add 0 (List.map String.length d).
140
141 Definition print_key (k : key) : string
142 := match k with
143 | long_key k => "--" ++ k
144 | short_key k => "-" ++ k
145 end.
146 Local Definition print_key_list (k : list key) : string
147 := match k with
148 | nil => "<none>"
149 | k => String.concat ", " (List.map print_key k)
150 end.
151
152 Local Definition print_spec_parts : (list key + string) * spec * doc -> string * list string
153 := fun '(k, s, d)
154 => if Nat.ltb 0 (doc_length d)
155 then ((" "
156 ++ (match k with
157 | inl k => print_key_list k
158 | inr k => k
159 end) ++ " "
160 ++ (match s with
161 | Symbol syms => make_symlist "{" "|" "}" syms
162 | CustomSymbol _ syms => make_symlist "{" "|" "}" (List.map (@fst _ _) syms)
163 | _ => ""
164 end))%string,
165 ([hd "" d])
166 ++ tl d
167 ++ (if Nat.eqb (List.length (tl d)) 0 then nil else [""]))
168 else ("", nil).
169
170 Definition print_spec_short (k : string) (s : spec) : string
171 := match s with
172 | Symbol syms => make_symlist "{" "|" "}" syms
173 | CustomSymbol _ syms => make_symlist "{" "|" "}" (List.map (@fst _ _) syms)
174 | _ => k
175 end.
176
177 Local Definition help_specs : list (list key * spec * doc)
178 := [([short_key "h"; long_key "help"],
179 Unit,
180 ["Display this list of options"])].
181
182 Definition arg_matches_key (arg : string) (k : key) : bool
183 := (arg =? print_key k)%string.
184
185 Definition arg_matches_key_list (arg : string) (k : list key) : bool
186 := List.fold_right orb false (List.map (arg_matches_key arg) k).
187
188 Definition parse_bool_opt (s : string) : option bool
189 := if (s =? "true")%string
190 then Some true
191 else if (s =? "false")%string
192 then Some false
193 else None.
194
195 Definition print_base_system (b : base_system) : string
196 := match b with
197 | bin => "binary"
198 | hex => "hexadecimal"
199 | oct => "octal"
200 | dec => "decimal"
201 end.
202
203 Definition print_base_systems (b : list base_system) : string
204 := match List.rev b with
205 | nil => "<magical>"
206 | b::nil => print_base_system b
207 | b2::b1::nil => print_base_system b1 ++ " or " ++ print_base_system b2
208 | b :: bs => String.concat ", " ((List.map print_base_system (List.rev bs)) ++ ["or " ++ print_base_system b]%string)
209 end.
210
211 Definition parse_int_as_opt (b : base_system) (s : string) : option Z
212 := let '(to_Z, of_Z) := match b with
213 | bin => (BinaryString.to_Z, BinaryString.of_Z)
214 | hex => (HexString.to_Z, HexString.of_Z)
215 | oct => (OctalString.to_Z, OctalString.of_Z)
216 | dec => (fun s => Option.sequence_return (Decimal.Z.of_string s) 0%Z, Decimal.Z.to_string)
217 end in
218 let v := to_Z s in
219 let s' := of_Z v in
220 if (s =? s')%string
221 then Some v
222 else None.
223
224 Definition parse_int_opt (b : list base_system) (s : string) : option (base_system * Z)
225 := List.fold_right
226 (fun b default => match parse_int_as_opt b s with
227 | Some v => Some (b, v)
228 | None => default
229 end)
230 None
231 b.
232
233 Definition parse_nat_opt (b : list base_system) (s : string) : option (base_system * nat)
234 := option_map (fun '(b, z) => (b, Z.to_nat z)) (parse_int_opt b s).
235
236 Inductive parse_error :=
237 | help (prog_name : string)
238 | missing (prog_name : string) (opt_name : string)
239 | wrong (prog_name : string) (opt_name : string) (got : string) (expected : string)
240 | no_keyed_arg (prog_name : string) (bad_args : list string)
241 | missing_prog_name
242 | too_many_args (prog_name : string) (remaining : list string)
243 | out_of_fuel (prog_name : string)
244 | internal_error (prog_name : string) (msg : string).
245
246 Definition parse_result T := ErrorT parse_error T.
247
248 Fixpoint parse_args_via_spec (s : spec) (argv : list string)
249 (missing : parse_error)
250 (wrong : string (* got *) -> string (* expected *) -> parse_error)
251 {struct s}
252 : parse_result (spec_data s * list string)
253 := match s, argv return parse_result (spec_data s * list string) with
254 | Unit, argv => Success (tt, argv)
255 | Bool, arg::argv
256 => v <- match parse_bool_opt arg with
257 | Some b => Success b
258 | None => Error (wrong arg "a boolean")
259 end;
260 Success (v, argv)
261 | String, arg::argv => Success (arg, argv)
262 | Int b, arg::argv
263 => v <- match parse_int_opt b arg with
264 | Some v => Success v
265 | None => Error (wrong arg ("a " ++ print_base_systems b ++ " integer")%string)
266 end;
267 Success (v, argv)
268 | Nat b, arg::argv
269 => v <- match parse_nat_opt b arg with
270 | Some v => Success v
271 | None => Error (wrong arg ("a " ++ print_base_systems b ++ " integer")%string)
272 end;
273 Success (v, argv)
274 | Custom dataT parse expected_descr, arg::argv
275 => v <- match parse arg with
276 | Some v => Success v
277 | None => Error (wrong arg expected_descr)
278 end;
279 Success (v, argv)
280 | Tuple nil, argv
281 => Success (tt, argv)
282 | Tuple (l :: ls), argv
283 => lv <- parse_args_via_spec l argv missing wrong;
284 let '(lv, argv) := lv in
285 list_rect
286 (fun ls => forall T : Type,
287 T -> list string -> parse_result (fold_left (fun a b : Type => (a * b)%type) (List.map spec_data ls) T * list string))
288 (fun T lv argv => Success (lv, argv))
289 (fun x xs rec T lv argv
290 => xv <- parse_args_via_spec x argv missing wrong;
291 let '(xv, argv) := xv in
292 rec _ (lv, xv) argv)
293 ls
294 _
295 lv
296 argv
297 | Symbol syms, arg::argv
298 => if List.existsb (fun s => arg =? s)%string syms
299 then Success (arg, argv)
300 else Error (wrong arg ("one of: " ++ (make_symlist "" " " "" syms))%string)
301 | CustomSymbol dataT syms, arg::argv
302 => match List.find (fun '(s, v) => arg =? s)%string syms with
303 | Some (s, v) => Success (v, argv)
304 | None => Error (wrong arg ("one of: " ++ (make_symlist "" " " "" (List.map (@fst _ _) syms)))%string)
305 end
306 | Bool, nil
307 | String, nil
308 | Int _, nil
309 | Nat _, nil
310 | Symbol _, nil
311 | CustomSymbol _ _, nil
312 | Custom _ _ _, nil
313 => Error missing
314 end%error.
315
316 Record arg_spec :=
317 { named_args : list (list key * spec * doc);
318 anon_args : list (string * spec * doc);
319 anon_opt_args : list (string * spec * doc);
320 anon_opt_repeated_arg : option (string * spec * doc) }.
321
322 Inductive ShouldBlockIndent := NoIndent | ArbitraryIndent | IndentUpTo (n : nat).
323 Existing Class ShouldBlockIndent.
324
325 Local Definition spaces (n : nat) : string
326 := String.string_of_list_ascii (List.repeat " "%char n).
327
328 Local Definition maybe_block_indent {should_block_indent : ShouldBlockIndent} (indent : nat) (lhs : string) (rhs : list string)
329 := let lhs := (lhs ++ " ")%string in
330 let indent := match should_block_indent with
331 | NoIndent => spaces (String.length lhs)
332 | ArbitraryIndent => spaces indent
333 | IndentUpTo n => spaces (Nat.min indent n)
334 end in
335 ((lhs ++ String.substring 0 (String.length indent - String.length lhs) indent ++ hd "" rhs)%string)
336 :: List.map (fun s => indent ++ s)%string (tl rhs).
337
338 Definition usage_string {should_block_indent : ShouldBlockIndent} : arg_spec -> usage_msg -> list string
339 := fun specs err
340 => err
341 ++ [String.NewLine]
342 ++ (let spec_parts
343 := ((List.map (fun '(k, s, d) => print_spec_parts (inl k, s, d)) (specs.(named_args) ++ help_specs))
344 ++ (List.map (fun '(k, s, d) => print_spec_parts (inr k, s, d)) (specs.(anon_args) ++ specs.(anon_opt_args) ++ match specs.(anon_opt_repeated_arg) with Some v => [v] | None => nil end))) in
345 let max_length := List.fold_right Nat.max O (List.map String.length (List.map (@fst _ _) spec_parts)) in
346 List.flat_map
347 (fun '(lhs, rhs) => maybe_block_indent (2 + max_length) lhs rhs)
348 spec_parts).
349
350 Definition arg_spec_results (s : arg_spec)
351 := (keyed_spec_list_data s.(named_args) * spec_list_data false s.(anon_args) * spec_list_data true s.(anon_opt_args) * (match s.(anon_opt_repeated_arg) with None => unit | Some (k, s, d) => list (spec_data s) end))%type.
352
353 Definition consume_one_named_arg
354 (arg : string)
355 (argv : list string)
356 (named_arg : list key * spec * doc)
357 (missing : parse_error)
358 (wrong : string (* got *) -> string (* expected *) -> parse_error)
359 : parse_result (option ((spec_data_with_key (snd (fst named_arg)) -> spec_data_with_key (snd (fst named_arg)))
360 * list string (* remaining *)))
361 := (if arg_matches_key_list arg (fst (fst named_arg))
362 then (res <- parse_args_via_spec (snd (fst named_arg)) argv missing wrong;
363 Success (Some ((fun data_so_far => data_so_far ++ [(arg, fst res)]), snd res)))
364 else Success None)%error.
365
366
367 Fixpoint consume_named_arg
368 (arg : string)
369 (argv : list string)
370 (named_args : list (list key * spec * doc))
371 (missing : string (* key *) -> parse_error)
372 (wrong : string (* key *) -> string (* got *) -> string (* expected *) -> parse_error)
373 {struct named_args}
374 : parse_result ((keyed_spec_list_data named_args -> keyed_spec_list_data named_args) * list string (* remaining *))
375 := (match named_args return parse_result ((keyed_spec_list_data named_args -> keyed_spec_list_data named_args) * list string (* remaining *))
376 with
377 | nil => Success (fun data_so_far => data_so_far, arg::argv)
378 | (k, s, d) :: named_args
379 => (lv <- consume_one_named_arg arg argv (k, s, d) (missing arg) (wrong arg);
380 match lv with
381 | Some (upd, argv)
382 => Success (update_type_of_list _ _ _ upd, argv)
383 | None
384 => (rec <- @consume_named_arg arg argv named_args missing wrong;
385 let '(upd, argv) := rec in
386 Success (match named_args
387 return (keyed_spec_list_data named_args -> keyed_spec_list_data named_args) ->
388 type_of_list (spec_data_with_key s) (List.map (fun '(_, s0, _) => spec_data_with_key s0) named_args) ->
389 type_of_list (spec_data_with_key s) (List.map (fun '(_, s0, _) => spec_data_with_key s0) named_args)
390 with
391 | nil => fun _ x => x
392 | cons (k', s', d') xs => update_type_of_list2 _ _ _ _
393 end upd,
394 argv))
395 end)
396 end%error).
397
398 Definition consume_help_or_named_arg
399 (arg : string)
400 (argv : list string)
401 (named_args : list (list key * spec * doc))
402 (missing : string (* key *) -> parse_error)
403 (wrong : string (* key *) -> string (* got *) -> string (* expected *) -> parse_error)
404 (help : parse_error)
405 : parse_result ((keyed_spec_list_data named_args -> keyed_spec_list_data named_args) * list string (* remaining *))
406 := if arg_matches_key_list arg [short_key "h"; long_key "help"]
407 then Error help
408 else consume_named_arg arg argv named_args missing wrong.
409
410 Definition consume_one_anon_arg (opt : bool) (argv : list string) (s : spec)
411 (missing : parse_error)
412 (wrong : string (* got *) -> string (* expected *) -> parse_error)
413 : parse_result (opt_spec_data opt s * list string (* remaining *))
414 := match parse_args_via_spec s argv missing wrong with
415 | Success (v, argv)
416 => Success (match opt return opt_spec_data opt s with
417 | true => Some v
418 | false => v
419 end,
420 argv)
421 | Error err
422 => match opt return parse_result (opt_spec_data opt s * list string (* remaining *)) with
423 | true => Success (None, argv)
424 | false => Error err
425 end
426 end.
427
428 Fixpoint Build_type_of_list_map {A}
429 {f l ls}
430 (v : l)
431 (vf : forall x, f x)
432 {struct ls}
433 : type_of_list l (@List.map A _ f ls)
434 := match ls with
435 | [] => v
436 | l' :: ls => @Build_type_of_list_map A f _ ls (v, vf _) vf
437 end.
438
439 Definition cons_spec_list_data {opt s1 s2 s3 ss}
440 : opt_spec_data opt s2 -> spec_list_data opt ss -> spec_list_data opt ((s1, s2, s3) :: ss)
441 := match ss with
442 | [] => fun v _ => v
443 | (_, _, _) :: _ => fun v vs => cons_type_of_list v vs
444 end.
445
446 Definition empty_spec_list_data_opt
447 {anon_opt_args : list (string * spec * doc)}
448 : spec_list_data true anon_opt_args
449 := match anon_opt_args return spec_list_data true anon_opt_args with
450 | [] => tt
451 | (_, _, _) :: _ => Build_type_of_list_map (f:=fun '(_, _, _) => _) None (fun '(_, _, _) => None)
452 end.
453
454 Definition empty_opt_repeated_arg {A B} {v : option (A * _ * B)}
455 : match v with
456 | Some (_, s0, _) => list (spec_data s0)
457 | None => unit
458 end
459 := match v with
460 | Some (_, _, _) => []
461 | None => tt
462 end.
463
464 Fixpoint consume_args
465 (fuel : nat)
466 (argv : list string)
467 (s : arg_spec)
468 (missing : string (* key *) -> parse_error)
469 (wrong : string (* key *) -> string (* got *) -> string (* expected *) -> parse_error)
470 (too_many : list string (* remaining *) -> parse_error)
471 (internal_error : string (* msg *) -> parse_error)
472 (no_keyed_arg : list string (* unrecognized arguments *) -> parse_error)
473 (help : parse_error)
474 (out_of_fuel : parse_error)
475 {struct fuel}
476 : parse_result ((keyed_spec_list_data s.(named_args) -> keyed_spec_list_data s.(named_args)) * spec_list_data false s.(anon_args) * spec_list_data true s.(anon_opt_args) * (match s.(anon_opt_repeated_arg) with None => unit | Some (k, s, d) => list (spec_data s) end))%type
477 := let initial_len := List.length argv in
478 let do_recr s fuel' argv'
479 := if list_beq _ String.eqb argv argv'
480 then (* no progress *)
481 match argv' with
482 | [] => Error (internal_error "Encountered empty argument list when doing recursion in consume_args")
483 | arg :: _
484 => if String.startswith "-" arg
485 then Error (no_keyed_arg [arg])
486 else Error (too_many argv')
487 end
488 else
489 @consume_args fuel' argv' s missing wrong too_many internal_error no_keyed_arg help out_of_fuel in
490 let spec := s in
491 match argv, fuel with
492 | nil, _
493 => match s.(anon_args) as saa
494 return parse_result (_ * spec_list_data false saa * _ * _)%type
495 with
496 | [] => Success (fun data => data, tt, empty_spec_list_data_opt, empty_opt_repeated_arg)
497 | (k, s, d) :: _ => Error (missing k)
498 end
499 | cons _ _, O => Error out_of_fuel
500 | cons arg argv, S fuel'
501 => if String.startswith "-" arg
502 then (* named *)
503 (res <- consume_help_or_named_arg arg argv s.(named_args) missing wrong help;
504 let '(upd0, argv) := res in
505 res <- do_recr s fuel' argv;
506 let '(upd1, anon_data, anon_opt_data, anon_opt_repeated_data) := res in
507 Success (fun data => upd1 (upd0 data), anon_data, anon_opt_data, anon_opt_repeated_data))
508 else (* anonymous *)
509 let sna := s.(named_args) in
510 match s.(anon_args) as saa, s.(anon_opt_args) as saoa, s.(anon_opt_repeated_arg) as saora
511 return parse_result ((keyed_spec_list_data s.(named_args) -> keyed_spec_list_data s.(named_args)) * spec_list_data false saa * spec_list_data true saoa * (match saora with None => unit | Some (k, s, d) => list (spec_data s) end))%type
512 with
513 | [], [], None => Error (too_many (arg :: argv))
514 | [], [], Some (k, s, d)
515 => (res <- consume_one_anon_arg false (arg :: argv) s (missing k) (wrong k);
516 let '(v, argv) := res in
517 res <- do_recr {| named_args := sna ; anon_args := [] ; anon_opt_args := [] ; anon_opt_repeated_arg := Some (k, s, d) |} fuel' argv;
518 let '(upd, tt, tt, vs) := res in
519 Success (upd, tt, tt, v :: vs))
520 | [], (k, s, d) :: saoa, saora
521 => (res <- consume_one_anon_arg true (arg :: argv) s (missing k) (wrong k);
522 let '(v, argv) := res in
523 res <- do_recr {| named_args := sna ; anon_args := [] ; anon_opt_args := saoa ; anon_opt_repeated_arg := saora |} fuel' argv;
524 let '(upd, tt, vs, rest) := res in
525 Success (upd, tt, cons_spec_list_data (opt:=true) v vs, rest))
526 | (k, s, d) :: saa, saoa, saora
527 => (res <- consume_one_anon_arg false (arg :: argv) s (missing k) (wrong k);
528 let '(v, argv) := res in
529 res <- do_recr {| named_args := sna ; anon_args := saa ; anon_opt_args := saoa ; anon_opt_repeated_arg := saora |} fuel' argv;
530 let '(upd, vs, rest_opt, rest) := res in
531 Success (upd, cons_spec_list_data (opt:=false) v vs, rest_opt, rest))
532 end
533 end%error.
534
535 Definition default_named_result {s} : spec_data_with_key s := nil.
536
537 Fixpoint default_named_results' {T : Type} {ss : list (list key * spec * doc)} (t : T) {struct ss}
538 : type_of_list T (List.map (fun '(_, s0, _) => spec_data_with_key s0) ss)
539 := match ss return type_of_list T (List.map (fun '(_, s0, _) => spec_data_with_key s0) ss) with
540 | nil => t
541 | cons (_, s, _) ss => @default_named_results' _ ss (t, default_named_result)
542 end.
543
544 Definition default_named_results {ls}
545 : keyed_spec_list_data ls
546 := match ls with
547 | nil => tt
548 | (_, s, _) :: ss => @default_named_results' _ ss default_named_result
549 end.
550
551 Class prog_name_countT := prog_name_count : nat.
552 Global Typeclasses Opaque prog_name_countT.
553 Ltac fill_default_prog_name_countT _
554 := lazymatch goal with
555 | [ H : prog_name_countT |- prog_name_countT ] => exact H
556 | [ |- prog_name_countT ] => exact 1%nat
557 end.
558 Global Hint Extern 1 prog_name_countT => fill_default_prog_name_countT () : typeclass_instances.
559
560 Definition parse_argv {prog_name_count : prog_name_countT} (argv : list string) (arg_spec : arg_spec)
561 : parse_result (arg_spec_results arg_spec)
562 := if (List.length argv <? prog_name_count)%nat
563 then Error missing_prog_name
564 else let '(prog_names, argv) := (List.firstn prog_name_count argv, List.skipn prog_name_count argv) in
565 let prog_name := String.concat " " prog_names in
566 (res <- consume_args (10 + (List.length argv)) argv arg_spec (missing prog_name) (wrong prog_name) (too_many_args prog_name) (internal_error prog_name) (no_keyed_arg prog_name) (help prog_name) (out_of_fuel prog_name);
567 let '(upd, anon_data, anon_opt_data, anon_opt_repeated_data) := res in
568 Success (upd default_named_results, anon_data, anon_opt_data, anon_opt_repeated_data))%error.
569
570 Definition is_real_error (e : parse_error) : bool := match e with help _ => false | _ => true end.
571
572 Definition make_usage (prog_name : string) (spec : arg_spec) : string
573 := "USAGE: "
574 ++ prog_name ++ " [OPTS...] "
575 ++ (String.concat
576 " "
577 ((List.map (fun '(k, s, d) => print_spec_short k s) spec.(anon_args))
578 ++ (List.map (fun '(k, s, d) => "[" ++ print_spec_short k s ++ "]")%string spec.(anon_opt_args))
579 ++ match spec.(anon_opt_repeated_arg) with
580 | Some (k, s, d) => ["[" ++ print_spec_short k s ++ "...]"]%string
581 | None => nil
582 end)).
583
584 Definition show_list_parse_error {should_block_indent : ShouldBlockIndent} (spec : arg_spec) (err : parse_error) : list string
585 := let mk_usage prog_name err := (usage_string spec (err ++ [String.NewLine; make_usage prog_name spec]) ++ [String.NewLine])%list in
586 match err with
587 | help prog_name
588 => mk_usage prog_name []
589 | missing prog_name opt_name
590 => mk_usage prog_name [prog_name ++ ": option '" ++ opt_name ++ "' needs an argument."]%string
591 | wrong prog_name opt_name got expected
592 => mk_usage prog_name [prog_name ++ ": wrong argument '" ++ got ++ "'; option '" ++ opt_name ++ "' expects " ++ expected ++ "."]%string
593 | missing_prog_name => ["Empty argv" ++ String.NewLine]%string
594 | too_many_args prog_name remaining
595 => mk_usage prog_name [prog_name ++ ": unrecognized arguments: " ++ String.concat " " remaining]%string
596 | no_keyed_arg prog_name invalid_args
597 => mk_usage prog_name [prog_name ++ ": unrecognized keyed arguments: " ++ String.concat " " invalid_args]%string
598 | internal_error prog_name msg
599 => mk_usage prog_name [prog_name ++ ": Internal error (please report as a bug): " ++ msg]%string
600 | out_of_fuel prog_name
601 => mk_usage prog_name [prog_name ++ ": Internal fatal error while parsing command line arguments: Out of fuel (probably a bugged command line argument specification)"]%string
602 end.
603
604 #[global]
605 Instance default_should_block_indent : ShouldBlockIndent := ArbitraryIndent.