fix subcommands in json model
This commit is contained in:
parent
640732de4b
commit
76656231be
2 changed files with 33 additions and 2 deletions
28
lib/store.ml
28
lib/store.ml
|
|
@ -308,6 +308,34 @@ let lookup_raw dirs command =
|
|||
let has_command dirs command =
|
||||
find_file dirs command <> None
|
||||
|
||||
let subcommands_of dirs command =
|
||||
let prefix = filename_of_command command ^ "_" in
|
||||
let plen = String.length prefix in
|
||||
let module SMap = Map.Make(String) in
|
||||
let subs = ref SMap.empty in
|
||||
List.iter (fun dir ->
|
||||
if Sys.file_exists dir && Sys.is_directory dir then
|
||||
Array.iter (fun f ->
|
||||
if String.starts_with ~prefix f then
|
||||
let base =
|
||||
if Filename.check_suffix f ".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
|
||||
match base with
|
||||
| Some b ->
|
||||
let rest = String.sub b plen (String.length b - plen) in
|
||||
(* Only direct children: no further underscores *)
|
||||
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
|
||||
| None -> ()
|
||||
) (Sys.readdir dir)
|
||||
) dirs;
|
||||
SMap.fold (fun _ sc acc -> sc :: acc) !subs [] |> List.rev
|
||||
|
||||
let all_commands dirs =
|
||||
let module SSet = Set.Make(String) in
|
||||
let cmds = ref SSet.empty in
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue