make generate use recursive help scrape

This commit is contained in:
atagen 2026-03-22 00:36:45 +11:00
parent 163e330716
commit 3cc278b144

View file

@ -229,13 +229,17 @@ let cmd_manpage_dir dir =
) (Sys.readdir subdir)
) command_sections
let help_resolve cmd rest name =
let help_resolve ?(timeout=10_000) cmd rest name =
let resolve_one go rest name depth =
match run_cmd (cmd :: rest @ ["--help"]) 10_000 with
let text = match run_cmd (cmd :: rest @ ["--help"]) timeout with
| Some _ as r -> r
| None -> run_cmd (cmd :: rest @ ["-h"]) timeout in
match text with
| None -> None
| Some text ->
(match parse_help text with
| Error _ -> None
| Ok r when r.entries = [] && r.subcommands = [] -> None
| Ok r when depth >= 5 -> Some (generate_extern name r)
| Ok r ->
let main = generate_extern name { r with subcommands = [] } in
@ -346,17 +350,12 @@ let cmd_generate bindir mandir outdir =
| None -> false)
| _ -> false in
if not native_ok then begin
let text = match run_cmd [path; "--help"] 200 with
| Some _ as r -> r
| None -> run_cmd [path; "-h"] 200 in
match text with
| None -> ()
| Some t ->
(match parse_help t with
| Ok r when r.entries <> [] ->
write_file (Filename.concat outdir (filename_of_cmd name ^ ".nu"))
(generate_module name r)
| _ -> ())
match help_resolve ~timeout:200 path [] name with
| Some content when String.length content > 0 ->
let m = module_name_of name in
let src = Printf.sprintf "module %s {\n%s}\n\nuse %s *\n" m content m in
write_file (Filename.concat outdir (filename_of_cmd name ^ ".nu")) src
| _ -> ()
end;
exit 0
with _ -> exit 1)