From f64411c4cd4f8b8959c0e975de532e5e8438dfeb Mon Sep 17 00:00:00 2001 From: shannmu Date: Wed, 14 Aug 2024 17:17:15 +0800 Subject: [PATCH] feat: Add custom completer for `cargo help ` --- src/bin/cargo/commands/help.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index a92f5d140bc..f9f91cf7249 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -15,7 +15,19 @@ const COMPRESSED_MAN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/man.tgz" pub fn cli() -> Command { subcommand("help") .about("Displays help for a cargo subcommand") - .arg(Arg::new("COMMAND").action(ArgAction::Set)) + .arg(Arg::new("COMMAND").action(ArgAction::Set).add( + clap_complete::ArgValueCandidates::new(|| { + super::builtin() + .iter() + .map(|cmd| { + let name = cmd.get_name(); + clap_complete::CompletionCandidate::new(name) + .help(cmd.get_about().cloned()) + .hide(cmd.is_hide_set()) + }) + .collect() + }), + )) } pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {