Repository navigation
Replies: 2 comments
|
The docs talk about "raw attributes", see https://docs.rs/clap/latest/clap/_derive/index.html#terminology. Every builder method is available through this. See also #4090 |
|
clap doesn't support multiple subcommand headings natively yet (that's tracked in #1553), but you can get exactly that layout today by hiding the "other" subcommands and rendering them yourself in a use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Do foo
Foo,
/// Do bar
Bar,
/// Do aaa
#[command(hide = true)]
Aaa,
/// Do bbb
#[command(hide = true)]
Bbb,
}
fn main() {
let mut cmd = Cli::command();
let other: Vec<(String, String)> = cmd
.get_subcommands()
.filter(|s| s.is_hide_set())
.map(|s| (s.get_name().to_string(), s.get_about().map(|a| a.to_string()).unwrap_or_default()))
.collect();
let width = other.iter().map(|(n, _)| n.len()).max().unwrap_or(0);
let styles = cmd.get_styles();
let (h, l) = (*styles.get_header(), *styles.get_literal());
let section: String = other
.iter()
.map(|(n, a)| format!(" {l}{n:width$}{l:#} {a}\n"))
.collect();
cmd = cmd.help_template(format!(
"{{usage-heading}} {{usage}}\n\n\
{h}Commands:{h:#}\n{{subcommands}}\n\n\
{h}Other Section:{h:#}\n{section}\n\
{h}Options:{h:#}\n{{options}}"
));
let cli = Cli::from_arg_matches(&cmd.get_matches()).unwrap_or_else(|e| e.exit());
println!("{:?}", cli.command);
}Output of Hidden subcommands still parse normally ( |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
It looks like
subcommand_help_headingis close to what I want, but I can't find a way to put some subcommands under one heading and some under another?The goal is to have clap output something like this:
All reactions