adopt better fp patterns

This commit is contained in:
atagen 2026-03-21 21:54:24 +11:00
parent 18c97eacd0
commit 163e330716
4 changed files with 111 additions and 83 deletions

View file

@ -257,7 +257,15 @@ let help_parser =
in
many (choice [ try_entry; try_subcommand; try_skip ]) >>| fun items ->
let entries = List.filter_map (function `Entry e -> Some e | _ -> None) items in
let subcommands = List.filter_map (function `Subcommand sc -> Some sc | _ -> None) items in
let subcommands =
List.filter_map (function `Subcommand sc -> Some sc | _ -> None) items
|> List.fold_left (fun acc sc ->
match List.assoc_opt sc.name acc with
| Some prev when String.length prev.desc >= String.length sc.desc -> acc
| _ -> (sc.name, sc) :: List.remove_assoc sc.name acc
) []
|> List.rev_map snd
in
{ entries; subcommands; positionals = [] })
let skip_command_prefix s =
@ -390,10 +398,12 @@ let parse_usage_args s =
| _ ->
incr i
done;
let seen = Hashtbl.create 8 in
List.rev !results |> List.filter (fun p ->
if Hashtbl.mem seen p.pos_name then false
else (Hashtbl.replace seen p.pos_name true; true))
List.rev !results
|> List.fold_left (fun (seen, acc) p ->
if List.mem p.pos_name seen then (seen, acc)
else (p.pos_name :: seen, p :: acc)
) ([], [])
|> snd |> List.rev
let extract_usage_positionals text =
let lines = String.split_on_char '\n' text in