I am trying to create a CLI using Clap that contains a relatively large number of subcommands.From the documentation, I would have expected the following to work:
#[derive(Parser)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
/// Determine reads mapping to sample-specific MAGs
#[command(subcommand_help_heading = "FirstHeading")]
Subcommand1(Subcommand1Args),
/// Create sketches from FASTA/Q file(s)
#[command(subcommand_help_heading = "SecondHeading")]
Subcommand2(Subcommand2Args),
}
However, the subcommand_help_heading arguments do not change the CLI. I'm expecting the output to be similar to:
My command line interface.
Usage: my_program <COMMAND>
FirstHeading:
subcommand1 ...
SecondHeading:
subcommand2 ...
Options:
-h, --help Print help
-V, --version Print version
The usage text in clap has one section for subcommands, and therefore one heading. It shows all the available subcommands. You can set that header with
subcommand_help_heading, so if yourCommandsstruct had this additional attribute:you would get, for
--help:As far as I can see there's no easy way to tell
clapto include multiple subcommand headings in the main help text. But if you have multiple nested subcommands inside of your commands, then you can control the help text for them. For example, say we define theSubcommand1Argsas:Then by using
subcommand1 --helpyou get:You can see the
FirstHeadingvalue fromsubcommand_help_headingapplied forSubcommand1being used here.Subcommands are basically nested sub-clis, and they get their own help pages.
Full code in play.rust-lang.