package climate

  1. Overview
  2. Docs

Source file arg_parser.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
open! Import
module Completion_ = Completion
module Parse_error = Error.Parse_error
module Spec_error = Error.Spec_error

let name_of_string_exn string =
  match Name.of_string string with
  | Ok name -> name
  | Error e -> Error.spec_error (Invalid_name (string, e))
;;

module Command_doc = struct
  (* Documentation about an entire (sub)command. Not relevant to parsing
     arguments but needs to be included in some error messages. *)
  type t =
    { doc : string option
    ; child_subcommands : Subcommand.t list
    }

  let empty = { doc = None; child_subcommands = [] }
end

module Context = struct
  type t =
    { raw_arg_table : Raw_arg_table.t
    ; command_line : Command_line.Rich.t
    ; command_doc_spec : Command_doc_spec.t
    ; error_subcommand : string list
    }
end

type 'a arg_compute = Context.t -> ('a, Non_ret.t) result

(* A parser for an argument or set of arguments. Typically parsers for each
   argument are combined into a single giant parser that parses all arguments
   to a program either returning some record containing all values or
   returning a unit and having the side effect of running the entire program
   once parsing is complete. A parser is made up of a spec that tells the low
   level parser in [Raw_arg_table] how to interpret terms on the command
   line, and a function [arg_compute] which knows how to retrieve the
   necessary raw values from a [Context.t] and convert them into the
   appropriate type for the parser. *)
type 'a t =
  { arg_spec : Spec.t
  ; arg_compute : 'a arg_compute
  ; command_doc : Command_doc.t
  }

(* Create a parser with no command documentation. This is the standard way of
   creating a parser, as conceptually we often don't need to think of a
   parser as even being part of a command. The main exception to this is when
   returning a [Non_ret.t], as these must contain a specification of the
   current command for printing usage strings in response to (say) an error
   parsing command line arguments. The [command_doc] field is set when adding
   the --help argument, and it will be added to the context passed to
   [arg_compute] so argument parsing logic has access to the final command
   documentation for use in error messages. *)
let with_empty_command_doc ~arg_spec ~arg_compute =
  { arg_spec; arg_compute; command_doc = Command_doc.empty }
;;

let spec { arg_spec; _ } = arg_spec

let command_doc_spec
  arg_spec
  (command_doc : Command_doc.t)
  (command_line : Command_line.Rich.t)
  =
  let args = Spec.command_doc_spec arg_spec in
  let subcommands =
    List.map command_doc.child_subcommands ~f:Subcommand.command_doc_spec
  in
  { Command_doc_spec.program_name = command_line.program
  ; subcommand = command_line.subcommand
  ; doc = command_doc.doc
  ; args
  ; subcommands
  }
;;

let eval
  t
  ~(command_line : Command_line.Rich.t)
  ~ignore_errors
  ~alt_subcommand_for_usage
  ~alt_subcommand_for_errors
  =
  let open Result.O in
  let subcommand_for_errors =
    Option.value alt_subcommand_for_errors ~default:command_line.subcommand
  in
  let subcommand_for_usage =
    Option.value alt_subcommand_for_usage ~default:command_line.subcommand
  in
  let command_doc_spec subcommand =
    { (command_doc_spec t.arg_spec t.command_doc command_line) with subcommand }
  in
  let* raw_arg_table =
    Raw_arg_table.parse t.arg_spec command_line.args ~ignore_errors
    |> Result.map_error ~f:(fun error ->
      Non_ret.Parse_error
        { command_doc_spec = command_doc_spec subcommand_for_errors; error })
  in
  let context =
    { Context.raw_arg_table
    ; command_line
    ; command_doc_spec = command_doc_spec subcommand_for_usage
    ; error_subcommand = subcommand_for_errors
    }
  in
  t.arg_compute context
;;

type 'a parse = string -> ('a, [ `Msg of string ]) result
type 'a print = Format.formatter -> 'a -> unit

let to_string_print to_string fmt value = Format.pp_print_string fmt (to_string value)

let value_to_string print value =
  print Format.str_formatter value;
  Format.flush_str_formatter ()
;;

module Completion = struct
  type command_line = Command_line.Rich.t =
    { program : string
    ; subcommand : string list
    ; args : string list
    }

  type 'a t =
    | File
    | Strings of string list
    | Strings_reentrant of (command_line -> string list)
    | Values of 'a list
    | Values_reentrant of (command_line -> 'a list)

  let file = File
  let values values = Values values
  let reentrant f = Values_reentrant f

  let reentrant_parse parser =
    let f command_line =
      match
        eval
          parser
          ~command_line
          ~ignore_errors:true
          ~alt_subcommand_for_errors:None
          ~alt_subcommand_for_usage:None
      with
      | Ok value -> value
      | Error _ -> failwith "Reentrant parser did not yield a result"
    in
    Values_reentrant f
  ;;

  let reentrant_thunk f =
    let f _ = f () in
    Values_reentrant f
  ;;

  let map t ~f =
    match t with
    | (File | Strings _ | Strings_reentrant _) as t' -> t'
    | Values xs -> Values (List.map ~f xs)
    | Values_reentrant get_suggestions ->
      Values_reentrant
        (fun command_line ->
          let suggestions = get_suggestions command_line in
          List.map ~f suggestions)
  ;;

  let some t = map t ~f:Option.some

  let stringify t print =
    match t with
    | (File | Strings _ | Strings_reentrant _) as t' -> t'
    | Values xs -> Strings (List.map ~f:(value_to_string print) xs)
    | Values_reentrant get_suggestions ->
      Strings_reentrant
        (fun command_line ->
          let suggestions = get_suggestions command_line in
          List.map ~f:(value_to_string print) suggestions)
  ;;
end

type 'a conv =
  { parse : 'a parse
  ; print : 'a print
  ; default_value_name : string
  ; completion : 'a Completion.t option
  }

let make_conv ~parse ~print ?(default_value_name = "VALUE") ?(completion = None) () =
  { parse; print; default_value_name; completion }
;;

let conv_untyped_completion print completion =
  match (completion : _ Completion.t) with
  | File -> Completion_spec.Hint.File
  | Strings strings -> Completion_spec.Hint.Values strings
  | Strings_reentrant f -> Completion_spec.Hint.Reentrant f
  | Values values ->
    Completion_spec.Hint.Values (List.map values ~f:(value_to_string print))
  | Values_reentrant f ->
    Completion_spec.Hint.Reentrant
      (fun command_line ->
        let suggestions = f command_line in
        List.map suggestions ~f:(value_to_string print))
;;

(* A conv can have a built-in completion, but it's also possible for
   this to be overridden for a specific parser. This is a helper
   function for converting a given completion, falling back to the
   built-in completion if none is given. *)
let conv_untyped_completion_opt_with_default conv completion_opt =
  let completion_opt =
    if Option.is_some completion_opt then completion_opt else conv.completion
  in
  Option.map completion_opt ~f:(conv_untyped_completion conv.print)
;;

let string =
  { parse = Result.ok
  ; print = Format.pp_print_string
  ; default_value_name = "STRING"
  ; completion = None
  }
;;

let int =
  let parse s =
    match int_of_string_opt s with
    | Some i -> Ok i
    | None -> Error (`Msg (sprintf "invalid value: %S (not an int)" s))
  in
  { parse; print = Format.pp_print_int; default_value_name = "INT"; completion = None }
;;

let float =
  let parse s =
    match float_of_string_opt s with
    | Some i -> Ok i
    | None -> Error (`Msg (sprintf "invalid value: %S (not an float)" s))
  in
  { parse
  ; print = Format.pp_print_float
  ; default_value_name = "FLOAT"
  ; completion = None
  }
;;

let bool =
  let parse s =
    match bool_of_string_opt s with
    | Some i -> Ok i
    | None -> Error (`Msg (sprintf "invalid value: %S (not an bool)" s))
  in
  { parse
  ; print = Format.pp_print_bool
  ; default_value_name = "BOOL"
  ; completion = Some (Completion.values [ true; false ])
  }
;;

let file = { string with default_value_name = "FILE"; completion = Some Completion.file }

let enum ?(default_value_name = "VALUE") ?(eq = ( = )) l =
  let all_names = List.map l ~f:fst in
  let all_values = List.map l ~f:snd in
  let duplicate_names = String.find_duplicates all_names in
  if List.length duplicate_names > 0
  then Error.spec_error (Duplicate_enum_names duplicate_names);
  let parse s =
    let value_opt =
      List.find_map l ~f:(fun (name, value) ->
        if String.equal name s then Some value else None)
    in
    match value_opt with
    | Some value -> Ok value
    | None ->
      let all_names_string = String.concat ~sep:", " all_names in
      let message =
        sprintf "invalid value: %S (valid values are: %s)" s all_names_string
      in
      Error (`Msg message)
  in
  let print ppf v =
    let name =
      List.find_map l ~f:(fun (name, value) -> if eq value v then Some name else None)
    in
    match name with
    | Some name -> Format.pp_print_string ppf name
    | None -> Error.spec_error (No_such_enum_value { valid_names = List.map l ~f:fst })
  in
  { parse; print; default_value_name; completion = Some (Completion.values all_values) }
;;

let string_enum ?(default_value_name = "VALUE") l =
  enum ~default_value_name (List.map l ~f:(fun s -> s, s)) ~eq:String.equal
;;

let pair ?(sep = ',') a b =
  let parse string =
    match String.split_on_char ~sep string with
    | [] | [ _ ] -> Error (`Msg (sprintf "No separator (%c) found in %S" sep string))
    | x :: xs ->
      let rest = String.concat ~sep:(String.make 1 sep) xs in
      Result.bind (a.parse x) ~f:(fun ax ->
        Result.map (b.parse rest) ~f:(fun bx -> ax, bx))
  in
  let print ppf (ax, bx) =
    a.print ppf ax;
    Format.pp_print_char ppf sep;
    b.print ppf bx
  in
  { parse; print; default_value_name = "PAIR"; completion = None }
;;

let list ?(sep = ',') element_conv =
  let parse string =
    Result.List.all (String.split_on_char ~sep string |> List.map ~f:element_conv.parse)
  in
  let print ppf elements =
    Format.pp_print_list
      ~pp_sep:(fun ppf () -> Format.pp_print_char ppf sep)
      element_conv.print
      ppf
      elements
  in
  { parse; print; default_value_name = "LIST"; completion = None }
;;

let map { arg_spec; arg_compute; command_doc } ~f =
  { arg_spec
  ; arg_compute = (fun context -> Result.map ~f (arg_compute context))
  ; command_doc
  }
;;

let map' { arg_spec; arg_compute; command_doc } ~f =
  { arg_spec
  ; arg_compute = (fun context -> Result.bind ~f (arg_compute context))
  ; command_doc
  }
;;

let map_context' { arg_spec; arg_compute; command_doc } ~f =
  { arg_spec
  ; arg_compute =
      (fun context -> Result.bind ~f:(fun x -> f x context) (arg_compute context))
  ; command_doc
  }
;;

let both x y =
  { arg_spec = Spec.merge x.arg_spec y.arg_spec
  ; arg_compute =
      (fun context ->
        let open Result.O in
        let+ x_value = x.arg_compute context
        and+ y_value = y.arg_compute context in
        x_value, y_value)
  ; command_doc =
      (* It's not expected that args will be both'd once their command
         documentation has been added. *)
      y.command_doc
  }
;;

let ( >>| ) t f = map t ~f
let ( let+ ) = ( >>| )
let ( and+ ) = both

let apply f x =
  let+ f = f
  and+ x = x in
  f x
;;

let ( $ ) f x = apply f x

let names_of_strings strings =
  match Nonempty_list.of_list strings with
  | None -> Error.spec_error Empty_name_list
  | Some strings -> Nonempty_list.map strings ~f:name_of_string_exn
;;

let const x =
  with_empty_command_doc ~arg_spec:Spec.empty ~arg_compute:(fun _context -> Ok x)
;;

let unit = const ()

let argv0 =
  with_empty_command_doc ~arg_spec:Spec.empty ~arg_compute:(fun context ->
    Ok context.command_line.program)
;;

let last { arg_spec; arg_compute; command_doc } =
  { arg_spec
  ; command_doc
  ; arg_compute =
      (fun context ->
        Result.bind (arg_compute context) ~f:(fun list ->
          match List.last list with
          | None ->
            Error
              (Non_ret.Parse_error
                 { error =
                     Conv_failed { locator = None; message = "Unexpected empty list" }
                 ; command_doc_spec = context.command_doc_spec
                 })
          | Some x -> Ok x))
  }
;;

let named_multi_gen info conv =
  with_empty_command_doc ~arg_spec:(Spec.create_named info) ~arg_compute:(fun context ->
    Raw_arg_table.get_opts_names_by_name context.raw_arg_table info.names
    |> List.map ~f:(fun (name, value) ->
      conv.parse value
      |> Result.map_error ~f:(fun (`Msg message) ->
        Non_ret.Parse_error
          { error = Conv_failed { locator = Some (`Named name); message }
          ; command_doc_spec = context.command_doc_spec
          }))
    |> Result.List.all)
;;

let named_opt_gen (info : Spec.Named.Info.t) conv ~allow_many =
  named_multi_gen info conv
  |> map_context' ~f:(fun xs context ->
    match xs with
    | [] -> Ok None
    | [ x ] -> Ok (Some x)
    | x :: _ as many ->
      if allow_many
      then Ok (Some x)
      else
        Error
          (Non_ret.Parse_error
             { error = Named_opt_appeared_multiple_times (info.names, List.length many)
             ; command_doc_spec = context.command_doc_spec
             }))
;;

let named_multi ?doc ?value_name ?hidden ?completion names conv =
  named_multi_gen
    { names = names_of_strings names
    ; has_param =
        `Yes_with_value_name (Option.value value_name ~default:conv.default_value_name)
    ; default_string = None
    ; required = false
    ; doc
    ; completion = conv_untyped_completion_opt_with_default conv completion
    ; hidden = Option.value hidden ~default:false
    ; repeated = true
    }
    conv
;;

let named_opt_for_internal ?doc ?value_name ?hidden ?completion names conv =
  named_opt_gen
    { names
    ; has_param =
        `Yes_with_value_name (Option.value value_name ~default:conv.default_value_name)
    ; default_string = None
    ; required = false
    ; doc
    ; completion = conv_untyped_completion_opt_with_default conv completion
    ; hidden = Option.value hidden ~default:false
    ; repeated = false
    }
    conv
;;

let named_opt ?doc ?value_name ?hidden ?completion names conv =
  named_opt_for_internal
    ?doc
    ?value_name
    ?hidden
    ?completion
    (names_of_strings names)
    conv
    ~allow_many:false
;;

let named_with_default_gen
  ?doc
  ?value_name
  ?hidden
  ?completion
  names
  conv
  ~default
  ~allow_many
  =
  named_opt_gen
    { names = names_of_strings names
    ; has_param =
        `Yes_with_value_name (Option.value value_name ~default:conv.default_value_name)
    ; default_string = Some (value_to_string conv.print default)
    ; required = false
    ; doc
    ; completion = conv_untyped_completion_opt_with_default conv completion
    ; hidden = Option.value hidden ~default:false
    ; repeated = false
    }
    conv
    ~allow_many
  >>| Option.value ~default
;;

let named_with_default = named_with_default_gen ~allow_many:false

let named_req ?doc ?value_name ?hidden ?completion names conv =
  named_multi_gen
    { names = names_of_strings names
    ; has_param =
        `Yes_with_value_name (Option.value value_name ~default:conv.default_value_name)
    ; default_string = None
    ; required = true
    ; doc
    ; completion = conv_untyped_completion_opt_with_default conv completion
    ; hidden = Option.value hidden ~default:false
    ; repeated = false
    }
    conv
  |> map_context' ~f:(fun xs context ->
    match xs with
    | [] ->
      Error
        (Non_ret.Parse_error
           { error = Named_req_missing (names_of_strings names)
           ; command_doc_spec = context.command_doc_spec
           })
    | [ x ] -> Ok x
    | many ->
      Error
        (Non_ret.Parse_error
           { error =
               Named_req_appeared_multiple_times (names_of_strings names, List.length many)
           ; command_doc_spec = context.command_doc_spec
           }))
;;

let flag_count ?doc ?hidden names =
  let names = names_of_strings names in
  with_empty_command_doc
    ~arg_spec:
      (Spec.create_flag
         names
         ~doc
         ~hidden:(Option.value hidden ~default:false)
         ~repeated:true)
    ~arg_compute:(fun context ->
      Ok (Raw_arg_table.get_flag_count_names context.raw_arg_table names))
;;

let flag_gen ?doc names ~allow_many =
  flag_count ?doc names
  |> map_context' ~f:(fun n context ->
    match n with
    | 0 -> Ok false
    | 1 -> Ok true
    | n ->
      if allow_many
      then Ok true
      else
        Error
          (Non_ret.Parse_error
             { error = Flag_appeared_multiple_times (names_of_strings names, n)
             ; command_doc_spec = context.command_doc_spec
             }))
;;

let flag = flag_gen ~allow_many:false

let pos_single_gen i conv ~doc ~value_name ~required ~completion =
  let i =
    match Nonnegative_int.of_int i with
    | Some _ -> i
    | None -> Error.spec_error (Negative_position i)
  in
  with_empty_command_doc
    ~arg_spec:
      (Spec.create_positional
         (Spec.Positional.single_at_index
            i
            ~value_name:(Option.value value_name ~default:conv.default_value_name)
            ~required
            ~completion:(conv_untyped_completion_opt_with_default conv completion)
            ~doc))
    ~arg_compute:(fun context ->
      match Raw_arg_table.get_pos context.raw_arg_table i with
      | None -> Ok None
      | Some x ->
        (match conv.parse x with
         | Ok x -> Ok (Some x)
         | Error (`Msg message) ->
           Error
             (Non_ret.Parse_error
                { error = Conv_failed { locator = Some (`Positional i); message }
                ; command_doc_spec = context.command_doc_spec
                })))
;;

let pos_opt ?doc ?value_name ?completion i conv =
  pos_single_gen i conv ~doc ~value_name ~required:false ~completion
;;

let pos_with_default ?doc ?value_name ?completion i conv ~default =
  pos_opt ?doc ?value_name ?completion i conv
  |> map ~f:(function
    | Some x -> x
    | None -> default)
;;

let pos_req ?doc ?value_name ?completion i conv =
  pos_single_gen i conv ~doc ~value_name ~required:true ~completion
  |> map_context' ~f:(fun x context ->
    match x with
    | Some x -> Ok x
    | None ->
      Error
        (Non_ret.Parse_error
           { error = Pos_req_missing i; command_doc_spec = context.command_doc_spec }))
;;

let pos_left_gen i conv ~doc ~value_name ~required ~completion =
  with_empty_command_doc
    ~arg_spec:
      (Spec.create_positional
         (Spec.Positional.all_below_exclusive
            i
            ~value_name:(Option.value value_name ~default:conv.default_value_name)
            ~required
            ~completion:(conv_untyped_completion_opt_with_default conv completion)
            ~doc))
    ~arg_compute:(fun context ->
      let left, _ = List.split_n (Raw_arg_table.get_pos_all context.raw_arg_table) i in
      List.mapi left ~f:(fun i x ->
        Result.map_error (conv.parse x) ~f:(fun (`Msg message) ->
          Non_ret.Parse_error
            { error = Conv_failed { locator = Some (`Positional i); message }
            ; command_doc_spec = context.command_doc_spec
            }))
      |> Result.List.all)
;;

let pos_left ?doc ?value_name ?completion i conv =
  pos_left_gen i conv ~doc ~value_name ~required:false ~completion
;;

let pos_right_inclusive ?doc ?value_name ?completion i_inclusive conv =
  with_empty_command_doc
    ~arg_spec:
      (Spec.create_positional
         (Spec.Positional.all_above_inclusive
            i_inclusive
            ~value_name:(Option.value value_name ~default:conv.default_value_name)
            ~completion:(conv_untyped_completion_opt_with_default conv completion)
            ~doc))
    ~arg_compute:(fun context ->
      let _, right =
        List.split_n (Raw_arg_table.get_pos_all context.raw_arg_table) i_inclusive
      in
      List.mapi right ~f:(fun i x ->
        Result.map_error (conv.parse x) ~f:(fun (`Msg message) ->
          Non_ret.Parse_error
            { error = Conv_failed { locator = Some (`Positional i); message }
            ; command_doc_spec = context.command_doc_spec
            }))
      |> Result.List.all)
;;

let pos_right ?doc ?value_name ?completion i_exclusive conv =
  pos_right_inclusive ?doc ?value_name ?completion (i_exclusive + 1) conv
;;

let pos_all ?doc ?value_name ?completion conv =
  pos_right_inclusive ?doc ?value_name ?completion 0 conv
;;

let validate t = Spec.validate t.arg_spec

let help_spec =
  Spec.create_flag
    Built_in.help_names
    ~doc:(Some "Show this help message.")
    ~hidden:false
    ~repeated:false
;;

let manpage_spec =
  Spec.create_flag Built_in.manpage_names ~doc:None ~hidden:true ~repeated:false
;;

let usage ~error ~message ~override_doc =
  with_empty_command_doc ~arg_spec:Spec.empty ~arg_compute:(fun context ->
    let doc =
      match override_doc with
      | Some override_doc -> Some override_doc
      | None -> context.command_doc_spec.doc
    in
    Error
      (Non_ret.Help
         { command_doc_spec = { context.command_doc_spec with doc }; error; message }))
;;

let to_usage { arg_spec; arg_compute = _; command_doc } =
  { arg_spec = Spec.empty
  ; command_doc = Command_doc.empty
  ; arg_compute =
      (fun context ->
        Error
          (Non_ret.Help
             { command_doc_spec =
                 { context.command_doc_spec with
                   args = Spec.command_doc_spec arg_spec
                 ; doc = command_doc.doc
                 }
             ; error = false
             ; message = None
             }))
  }
;;

let add_help_and_manpage
  { arg_spec; arg_compute; command_doc = _ }
  ~doc
  ~child_subcommands
  ~prose
  ~use_error_subcommand
  ~help_only_doc
  ~help_only_subcommands
  =
  let command_doc = { Command_doc.doc; child_subcommands } in
  let arg_spec = arg_spec |> Spec.merge help_spec |> Spec.merge manpage_spec in
  { arg_spec
  ; arg_compute =
      (fun context ->
        if Raw_arg_table.get_flag_count_names context.raw_arg_table Built_in.help_names
           > 0
        then (
          let command_doc_spec =
            match use_error_subcommand with
            | false -> context.command_doc_spec
            | true ->
              { context.command_doc_spec with subcommand = context.error_subcommand }
          in
          let command_doc_spec =
            match help_only_doc with
            | None -> command_doc_spec
            | Some doc -> { command_doc_spec with doc = Some doc }
          in
          let command_doc_spec =
            match help_only_subcommands with
            | None -> command_doc_spec
            | Some help_only_subcommands ->
              let help_only_subcommands =
                List.map help_only_subcommands ~f:Subcommand.command_doc_spec
              in
              { command_doc_spec with subcommands = help_only_subcommands }
          in
          Error (Non_ret.Help { command_doc_spec; error = false; message = None }))
        else if Raw_arg_table.get_flag_count_names
                  context.raw_arg_table
                  Built_in.manpage_names
                > 0
        then (
          let prose = Option.value prose ~default:Manpage.Prose.empty in
          Error (Non_ret.Manpage { prose; command_doc_spec = context.command_doc_spec }))
        else arg_compute context)
  ; command_doc
  }
;;

let finalize
  t
  ~doc
  ~child_subcommands
  ~prose
  ~use_error_subcommand
  ~help_only_doc
  ~help_only_subcommands
  =
  validate t;
  add_help_and_manpage
    t
    ~doc
    ~child_subcommands
    ~prose
    ~use_error_subcommand
    ~help_only_doc
    ~help_only_subcommands
;;

module Reentrant = struct
  let unit = unit
  let map = map
  let both = both
  let ( >>| ) = ( >>| )
  let ( let+ ) = ( let+ )
  let ( and+ ) = ( and+ )
  let named_multi names conv = named_multi names conv

  let named_opt names conv =
    named_opt_for_internal (names_of_strings names) conv ~allow_many:true
  ;;

  let named_with_default names conv = named_with_default_gen names conv ~allow_many:true
  let flag_count names = flag_count names
  let flag names = flag_gen names ~allow_many:true
  let pos_opt i conv = pos_opt i conv
  let pos_all conv = pos_all conv
  let pos_left i conv = pos_left i conv
  let pos_right i conv = pos_right i conv
end

module Private = struct
  let usage = usage
  let to_usage = to_usage
  let spec = spec
  let finalize = finalize
  let named_opt_for_internal = named_opt_for_internal
  let eval = eval

  let command_doc_spec { arg_spec; command_doc; _ } command_line =
    command_doc_spec arg_spec command_doc command_line
  ;;
end
OCaml

Innovation. Community. Security.