package alcotest

  1. Overview
  2. Docs

Source file config.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
open! Utils

type bound = [ `Unlimited | `Limit of int ]

type 'a with_options =
  ?and_exit:bool ->
  ?verbose:bool ->
  ?compact:bool ->
  ?tail_errors:[ `Unlimited | `Limit of int ] ->
  ?quick_only:bool ->
  ?show_errors:bool ->
  ?json:bool ->
  ?filter:Re.re option * int list option ->
  ?log_dir:string ->
  ?bail:bool ->
  'a

(* User configs before defaults have been applied. *)
module User = struct
  type t = {
    and_exit : bool option;
    verbose : bool option;
    compact : bool option;
    tail_errors : bound option;
    quick_only : bool option;
    show_errors : bool option;
    json : bool option;
    filter : (Re.re option * int list option) option;
    log_dir : string option;
    bail : bool option;
  }

  (* Lift a config-sensitive function to one that consumes optional arguments that
     override config defaults. *)
  let kcreate : 'a. (t -> 'a) -> 'a with_options =
   fun f ?and_exit ?verbose ?compact ?tail_errors ?quick_only ?show_errors ?json
       ?filter ?log_dir ?bail ->
    f
      {
        and_exit;
        verbose;
        compact;
        tail_errors;
        quick_only;
        show_errors;
        json;
        filter;
        log_dir;
        bail;
      }

  let create : (unit -> t) with_options = kcreate (fun t () -> t)
end

type t =
  < and_exit : bool
  ; verbose : bool
  ; compact : bool
  ; tail_errors : bound
  ; quick_only : bool
  ; show_errors : bool
  ; json : bool
  ; filter : Re.re option * int list option
  ; log_dir : string
  ; bail : bool >

let apply_defaults ~default_log_dir : User.t -> t =
 fun {
       and_exit;
       verbose;
       compact;
       tail_errors;
       quick_only;
       show_errors;
       json;
       filter;
       log_dir;
       bail;
     } ->
  object
    method and_exit = Option.value ~default:true and_exit
    method verbose = Option.value ~default:false verbose
    method compact = Option.value ~default:false compact
    method tail_errors = Option.value ~default:`Unlimited tail_errors
    method quick_only = Option.value ~default:false quick_only
    method show_errors = Option.value ~default:false show_errors
    method json = Option.value ~default:false json
    method filter = Option.value ~default:(None, None) filter
    method log_dir = Option.value ~default:default_log_dir log_dir
    method bail = Option.value ~default:false bail
  end
OCaml

Innovation. Community. Security.