This commit is contained in:
atagen 2026-03-23 15:06:49 +11:00
parent 76656231be
commit adea668355
6 changed files with 76 additions and 27 deletions

View file

@ -68,8 +68,9 @@ let json_list f items =
"[" ^ String.concat "," (List.map f items) ^ "]"
let json_of_help_result ?(source="help") r =
Printf.sprintf "{\"source\":%s,\"entries\":%s,\"subcommands\":%s,\"positionals\":%s}"
Printf.sprintf "{\"source\":%s,\"description\":%s,\"entries\":%s,\"subcommands\":%s,\"positionals\":%s}"
(json_string source)
(json_string r.description)
(json_list json_entry_of r.entries)
(json_list json_subcommand_of r.subcommands)
(json_list json_positional_of r.positionals)
@ -237,7 +238,8 @@ let positional_of_json j =
let help_result_of_json j =
{ entries = List.map entry_of_json (json_to_list (json_get "entries" j));
subcommands = List.map subcommand_of_json (json_to_list (json_get "subcommands" j));
positionals = List.map positional_of_json (json_to_list (json_get "positionals" j)) }
positionals = List.map positional_of_json (json_to_list (json_get "positionals" j));
description = json_to_string (json_get "description" j) }
(* --- Filesystem operations --- *)
@ -317,9 +319,9 @@ let subcommands_of dirs command =
if Sys.file_exists dir && Sys.is_directory dir then
Array.iter (fun f ->
if String.starts_with ~prefix f then
let is_json = Filename.check_suffix f ".json" in
let base =
if Filename.check_suffix f ".json" then
Some (Filename.chop_suffix f ".json")
if is_json then Some (Filename.chop_suffix f ".json")
else if Filename.check_suffix f ".nu" then
Some (Filename.chop_suffix f ".nu")
else None in
@ -330,7 +332,14 @@ let subcommands_of dirs command =
if not (String.contains rest '_') && String.length rest > 0 then
let name = rest in
if not (SMap.mem name !subs) then
subs := SMap.add name { name; desc = "" } !subs
let desc = if is_json then
match read_file (Filename.concat dir f) with
| Some data ->
(try json_to_string (json_get "description" (parse_json data))
with _ -> "")
| None -> ""
else "" in
subs := SMap.add name { name; desc } !subs
| None -> ()
) (Sys.readdir dir)
) dirs;