Source file spec.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
open! Import
module Spec_error = Error.Spec_error

type untyped_completion_function = Command_line.Rich.t -> string list
type untyped_completion_hint = untyped_completion_function Completion_spec.Hint.t

module Named = struct
  module Info = struct
    type t =
      { names : Name.t Nonempty_list.t
      ; has_param : [ `No | `Yes_with_value_name of string ]
      ; default_string :
          string option (* default value to display in documentation (if any) *)
      ; required : bool (* determines if argument is shown in usage string *)
      ; doc : string option
      ; completion : untyped_completion_hint option
      ; hidden : bool
      ; repeated : bool
      }

    let has_param t =
      match t.has_param with
      | `No -> false
      | `Yes_with_value_name _ -> true
    ;;

    let flag names ~doc ~hidden ~repeated =
      { names
      ; has_param = `No
      ; default_string = None
      ; required = false
      ; doc
      ; completion = None
      ; hidden
      ; repeated
      }
    ;;

    let to_completion_named_arg t =
      if t.hidden
      then None
      else
        Some
          { Completion_spec.Named_arg.names = t.names
          ; has_param = has_param t
          ; hint = t.completion
          }
    ;;

    let command_doc_spec t =
      if t.hidden
      then None
      else (
        let value =
          match t.has_param with
          | `No -> None
          | `Yes_with_value_name name ->
            Some { Command_doc_spec.Value.name; required = true }
        in
        Some
          { Command_doc_spec.Named_arg.names = t.names
          ; value
          ; repeated = t.repeated
          ; default_string = t.default_string
          ; doc = t.doc
          })
    ;;
  end

  type t = { infos : Info.t list }

  let empty = { infos = [] }
  let is_empty { infos } = List.is_empty infos

  let command_doc_spec { infos } =
    List.rev infos |> List.filter_map ~f:Info.command_doc_spec
  ;;

  let get_info_by_name { infos } name =
    List.find_opt infos ~f:(fun (info : Info.t) ->
      List.exists (Nonempty_list.to_list info.names) ~f:(Name.equal name))
  ;;

  let contains_name { infos } name =
    List.exists infos ~f:(fun (info : Info.t) ->
      List.exists (Nonempty_list.to_list info.names) ~f:(Name.equal name))
  ;;

  let add t (info : Info.t) =
    List.iter (Nonempty_list.to_list info.names) ~f:(fun name ->
      if contains_name t name then Error.spec_error (Duplicate_name name));
    { infos = info :: t.infos }
  ;;

  let merge x y = List.fold_left y.infos ~init:x ~f:add

  (* Checks that none of the names in the spec are also the names of
     built-in arguments (such as "--help") *)
  let validate_no_built_in_names t =
    match
      List.find_map (Nonempty_list.to_list Built_in.help_names) ~f:(fun name ->
        if contains_name t name then Some name else None)
    with
    | None -> Ok ()
    | Some help_name -> Error (Spec_error.Name_reserved_for_help help_name)
  ;;

  let to_completion_named_args { infos } =
    List.filter_map infos ~f:Info.to_completion_named_arg
  ;;
end

module Positional = struct
  type single_arg =
    { required : bool
    ; value_name : string
    ; completion : untyped_completion_hint option
    ; doc : string option
    }

  type all_above_inclusive =
    { index : int
    ; arg : single_arg
    }

  (* Keeps track of which indices of positional argument have parsers registered *)
  type t =
    { all_above_inclusive : all_above_inclusive option
    ; others_by_index : single_arg Int.Map.t
    }

  let empty = { all_above_inclusive = None; others_by_index = Int.Map.empty }

  let is_empty { all_above_inclusive; others_by_index } =
    Option.is_none all_above_inclusive && Int.Map.is_empty others_by_index
  ;;

  let command_doc_spec_of_single_arg { required; value_name; doc; _ }
    : Command_doc_spec.Positional_arg.t
    =
    let value = { Command_doc_spec.Value.name = value_name; required } in
    { Command_doc_spec.Positional_arg.value; doc }
  ;;

  let command_doc_spec { all_above_inclusive; others_by_index } =
    let fixed =
      Int.Map.bindings others_by_index
      |> List.map ~f:snd
      |> List.map ~f:command_doc_spec_of_single_arg
    in
    let repeated =
      Option.map all_above_inclusive ~f:(fun { arg; _ } ->
        command_doc_spec_of_single_arg arg)
    in
    { Command_doc_spec.Positional_args.fixed; repeated }
  ;;

  let check_value_names index value_name1 value_name2 =
    if not (String.equal value_name1 value_name2)
    then
      Error.spec_error
        (Positional_argument_collision_with_different_value_names
           { index; value_name1; value_name2 })
  ;;

  let trim_map t =
    match t.all_above_inclusive with
    | None -> t
    | Some all_above_inclusive ->
      let others_by_index =
        Int.Map.filter t.others_by_index ~f:(fun index { value_name; required; _ } ->
          if index >= all_above_inclusive.index
          then (
            check_value_names index value_name all_above_inclusive.arg.value_name;
            if required
            then Error.spec_error (Conflicting_requiredness_for_positional_argument index);
            false)
          else true)
      in
      { t with others_by_index }
  ;;

  let add_index t index ~value_name ~required ~completion ~doc =
    let others_by_index =
      Int.Map.update t.others_by_index ~key:index ~f:(function
        | None -> Some { value_name; required; completion; doc }
        | Some x ->
          check_value_names index x.value_name value_name;
          if x.required <> required
          then Error.spec_error (Conflicting_requiredness_for_positional_argument index);
          Some x)
    in
    trim_map { t with others_by_index }
  ;;

  let add_all_above_inclusive t index ~value_name ~completion ~doc =
    match t.all_above_inclusive with
    | Some x when x.index < index ->
      check_value_names index x.arg.value_name value_name;
      t
    | _ ->
      trim_map
        { t with
          all_above_inclusive =
            Some { index; arg = { required = false; value_name; completion; doc } }
        }
  ;;

  let add_all_below_exclusive t index ~value_name ~required ~completion ~doc =
    Seq.init index Fun.id
    |> Seq.fold_left (add_index ~value_name ~required ~completion ~doc) t
    |> trim_map
  ;;

  let merge x y =
    let all_above_inclusive =
      match x.all_above_inclusive, y.all_above_inclusive with
      | None, None -> None
      | Some a, None | None, Some a -> Some a
      | Some x, Some y ->
        check_value_names (Int.max x.index y.index) x.arg.value_name y.arg.value_name;
        let index = Int.min x.index y.index in
        Some { index; arg = x.arg }
    in
    let others_by_index =
      Int.Map.merge x.others_by_index y.others_by_index ~f:(fun index x y ->
        match x, y with
        | None, None -> None
        | Some value_name, None | None, Some value_name -> Some value_name
        | Some x, Some y ->
          check_value_names index x.value_name y.value_name;
          if x.required <> y.required
          then Error.spec_error (Conflicting_requiredness_for_positional_argument index);
          Some x)
    in
    trim_map { all_above_inclusive; others_by_index }
  ;;

  let single_at_index i = add_index empty i
  let all_above_inclusive i = add_all_above_inclusive empty i
  let all_below_exclusive i = add_all_below_exclusive empty i

  (* Check that there are no gaps in the declared positional arguments (E.g.
     if the parser would interpret the argument at position 0 and 2 but not 1
     it's probably an error.) *)
  let validate_no_gaps { all_above_inclusive; others_by_index } =
    let other_indices = Int.Map.to_seq others_by_index |> Seq.map fst |> Int.Set.of_seq in
    let set_to_validate =
      match all_above_inclusive with
      | Some { index; _ } -> Int.Set.add index other_indices
      | None -> other_indices
    in
    match Int.Set.max_elt_opt set_to_validate with
    | None -> Ok ()
    | Some max ->
      let gap =
        Seq.init max Fun.id |> Seq.find (fun i -> not (Int.Set.mem i set_to_validate))
      in
      (match gap with
       | Some i -> Error (Spec_error.Gap_in_positional_argument_range i)
       | None -> Ok ())
  ;;

  let to_completions ({ all_above_inclusive; others_by_index } as t) =
    if Result.is_error (validate_no_gaps t)
    then raise (Invalid_argument "positional argument spec has gaps");
    let finite_args =
      Int.Map.bindings others_by_index
      |> List.map ~f:(fun (_, { completion; _ }) -> completion)
    in
    let repeated_arg =
      Option.map all_above_inclusive ~f:(fun { arg = { completion; _ }; _ } ->
        match completion with
        | None -> `No_hint
        | Some hint -> `Hint hint)
    in
    { Completion_spec.Positional_args_hints.finite_args; repeated_arg }
  ;;

  let arg_count { all_above_inclusive; others_by_index } =
    match all_above_inclusive with
    | Some _ -> `Unlimited
    | None -> `Limited (Int.Map.cardinal others_by_index)
  ;;
end

type t =
  { named : Named.t
  ; positional : Positional.t
  }

let merge x y =
  { named = Named.merge x.named y.named
  ; positional = Positional.merge x.positional y.positional
  }
;;

let empty = { named = Named.empty; positional = Positional.empty }

let is_empty { named; positional } =
  Named.is_empty named && Positional.is_empty positional
;;

let create_positional positional = { named = Named.empty; positional }

let create_named info =
  let named = Named.add Named.empty info in
  { named; positional = Positional.empty }
;;

let create_flag names ~doc ~hidden ~repeated =
  create_named (Named.Info.flag names ~doc ~hidden ~repeated)
;;

let command_doc_spec { named; positional } =
  { Command_doc_spec.Args.named = Named.command_doc_spec named
  ; positional = Positional.command_doc_spec positional
  }
;;

let to_completion_parser_spec { named; positional } =
  let named_args = Named.to_completion_named_args named in
  let positional_args_hints = Positional.to_completions positional in
  { Completion_spec.Parser_spec.named_args; positional_args_hints }
;;

let validate { named; positional } =
  (match Positional.validate_no_gaps positional with
   | Ok () -> ()
   | Error e -> Error.spec_error e);
  match Named.validate_no_built_in_names named with
  | Ok () -> ()
  | Error e -> Error.spec_error e
;;