Source file command_selector.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
module Selected = struct
type 'a t =
{ command : 'a Ast.Command.t
; resume_parsing_from_index : int
}
end
let select (type a) command ~argv =
let rec aux index command =
match (command : a Ast.Command.t) with
| Make _ -> { Selected.command; resume_parsing_from_index = index }
| Group { default = _; summary = _; readme = _; subcommands } ->
if index >= Array.length argv
then { Selected.command; resume_parsing_from_index = index }
else (
let arg = argv.(index) in
match subcommands |> List.find_opt (fun (name, _) -> String.equal arg name) with
| Some (_, subcommand) -> aux (index + 1) subcommand
| None -> { Selected.command; resume_parsing_from_index = index })
in
aux 1 command
;;