This commit is contained in:
atagen 2026-03-23 16:24:00 +11:00
parent 4e41fcda6d
commit 144d72b223
5 changed files with 188 additions and 258 deletions

View file

@ -746,32 +746,31 @@ let extract_subcommand_sections contents =
let sections = collect_sections [] None [] classified in
(* For each SUBCOMMAND section, extract name from Usage: line and parse entries *)
let usage_re = Str.regexp {|Usage: \([a-zA-Z0-9_-]+\)|} in
let matches_usage s =
try ignore (Str.search_forward usage_re s 0); Some (Str.matched_group 1 s)
with Not_found -> None in
List.filter_map (fun (_header, section_lines) ->
(* Find subcommand name from Usage: line *)
let name = ref None in
let desc_lines = ref [] in
List.iter (fun line ->
if !name = None then
match line with
| Text s ->
if try ignore (Str.search_forward usage_re s 0); true
with Not_found -> false
then name := Some (Str.matched_group 1 s)
else desc_lines := s :: !desc_lines
| Macro (("TP" | "B" | "BI" | "BR"), args) ->
let s = strip_inline_macro_args args |> strip_groff_escapes |> String.trim in
if try ignore (Str.search_forward usage_re s 0); true
with Not_found -> false
then name := Some (Str.matched_group 1 s)
| _ -> ()
) section_lines;
match !name with
let name, desc_lines =
List.fold_left (fun (name, desc_lines) line ->
match name with
| Some _ -> (name, desc_lines)
| None ->
match line with
| Text s ->
(match matches_usage s with
| Some _ as found -> (found, desc_lines)
| None -> (None, s :: desc_lines))
| Macro (("TP" | "B" | "BI" | "BR"), args) ->
let s = strip_inline_macro_args args |> strip_groff_escapes |> String.trim in
(matches_usage s, desc_lines)
| _ -> (None, desc_lines)
) (None, []) section_lines in
match name with
| None -> None
| Some subcmd_name ->
let entries = extract_entries section_lines in
let desc = String.concat " " (List.rev !desc_lines)
let desc = String.concat " " (List.rev desc_lines)
|> strip_groff_escapes |> String.trim in
(* Remove backtick quoting common in clap output *)
let desc = Str.global_replace (Str.regexp "`\\([^`]*\\)`") "\\1" desc in
Some (subcmd_name, desc, { entries; subcommands = []; positionals = []; description = desc })
) sections