From b482d48fe86d42e22a221997a571f87affd28cdc Mon Sep 17 00:00:00 2001
From: Ali Caglayan <alizter@gmail.com>
Date: Wed, 7 Dec 2022 00:24:22 +0100
Subject: [PATCH] Add get_subcommands to Cmd for fetching the subcommands of a
 command

Signed-off-by: Ali Caglayan <alizter@gmail.com>
---
 src/cmdliner.mli     | 6 ++++++
 src/cmdliner_cmd.ml  | 2 ++
 src/cmdliner_cmd.mli | 1 +
 3 files changed, 9 insertions(+)

diff --git a/src/cmdliner.mli b/src/cmdliner.mli
index c6d179a..f7364f9 100644
--- a/src/cmdliner.mli
+++ b/src/cmdliner.mli
@@ -773,6 +773,12 @@ module Cmd : sig
       specification of the command line: we can't tell apart a
       positional argument from the value of an unknown optional
       argument.  *)
+
+  val get_subcommands : 'a t -> 'a t list
+  (** [get_subcommands cmd] returns the list of subcommands of [cmd].
+
+      We use this in Dune so that we can crawl all the subcommands of a
+      command and automatically generate the man pages for them. *)
 end
 
 (** Terms for command line arguments.
diff --git a/src/cmdliner_cmd.ml b/src/cmdliner_cmd.ml
index 5a156f3..695100b 100644
--- a/src/cmdliner_cmd.ml
+++ b/src/cmdliner_cmd.ml
@@ -29,6 +29,8 @@ let group ?default i cmds =
 
 let name c = Cmdliner_info.Cmd.name (get_info c)
 
+let get_subcommands = function Cmd _ -> [] | Group (_, (_, cs)) -> cs
+
 (*---------------------------------------------------------------------------
    Copyright (c) 2022 The cmdliner programmers
 
diff --git a/src/cmdliner_cmd.mli b/src/cmdliner_cmd.mli
index 54da153..bb80377 100644
--- a/src/cmdliner_cmd.mli
+++ b/src/cmdliner_cmd.mli
@@ -22,6 +22,7 @@ val v : info -> 'a Cmdliner_term.t -> 'a t
 val group : ?default:'a Cmdliner_term.t -> info -> 'a t list -> 'a t
 val name : 'a t -> string
 val get_info : 'a t -> info
+val get_subcommands : 'a t -> 'a t list
 
 (*---------------------------------------------------------------------------
    Copyright (c) 2022 The cmdliner programmers