Plugins: allow registration of multiple subcommands

This commit is contained in:
Louis Gesbert 2024-09-26 11:25:00 +02:00
parent d5b5fe71c4
commit 3f0c9d9947
4 changed files with 23 additions and 0 deletions

View File

@ -1238,4 +1238,9 @@ module Plugin = struct
let name = String.lowercase_ascii name in
let info = Cmdliner.Cmd.info name ?man ?doc ~docs:Cli.s_plugins in
Plugin.register info term
let register_subcommands name ?man ?doc cmds =
let name = String.lowercase_ascii name in
let info = Cmdliner.Cmd.info name ?man ?doc ~docs:Cli.s_plugins in
Plugin.register_subcommands info cmds
end

View File

@ -119,4 +119,11 @@ module Plugin : sig
?doc:string ->
(Global.options -> unit) Cmdliner.Term.t ->
unit
val register_subcommands :
string ->
?man:Cmdliner.Manpage.block list ->
?doc:string ->
unit Cmdliner.Cmd.t list ->
unit
end

View File

@ -26,6 +26,10 @@ let register info term =
Hashtbl.replace backend_plugins name
(Cmd.v info Term.(term $ Cli.Flags.Global.options))
let register_subcommands info cmds =
let name = String.lowercase_ascii (Cmd.name (Cmd.v info (Term.const ()))) in
Hashtbl.replace backend_plugins name (Cmd.group info cmds)
let list () = Hashtbl.to_seq_values backend_plugins |> List.of_seq
let names () = Hashtbl.to_seq_keys backend_plugins |> List.of_seq
let load_failures = Hashtbl.create 17

View File

@ -28,6 +28,13 @@ val register :
[--plugins-dirs] to be handled correctly, and for setting debug flags), but
can add more. *)
val register_subcommands : Cmdliner.Cmd.info -> unit Cmdliner.Cmd.t list -> unit
(** This alternative to [register] allows to register plugins that define
multiple subcommands (e.g. [catala myplugin subcommand --help]). Be aware
that all subcommands should take the [Catala_utils.Cli.Flags.Global.options]
term that handles the [--plugins-dirs] flags and performs some
initialisations. *)
(** {2 catala-facing API} *)
val list : unit -> t list