From 02212b2ca0916ea3aaf3d44fef2b4a41efb05092 Mon Sep 17 00:00:00 2001 From: Sebastian Holmin Date: Tue, 16 Jan 2024 11:50:41 +0100 Subject: [PATCH] fix(complete): Use `bin_name` for subcommands Bash completions for subcommands used package name, which broke completions when the `bin_name` was different. --- clap_complete/src/shells/bash.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clap_complete/src/shells/bash.rs b/clap_complete/src/shells/bash.rs index 726078ab3663..708a21449122 100644 --- a/clap_complete/src/shells/bash.rs +++ b/clap_complete/src/shells/bash.rs @@ -18,6 +18,8 @@ impl Generator for Bash { .get_bin_name() .expect("crate::generate should have set the bin_name"); + let fn_name = bin_name.replace('-', "__"); + w!( buf, format!( @@ -65,10 +67,10 @@ else fi ", name = bin_name, - cmd = bin_name.replace('-', "__"), + cmd = fn_name, name_opts = all_options_for_path(cmd, bin_name), name_opts_details = option_details_for_path(cmd, bin_name), - subcmds = all_subcommands(cmd), + subcmds = all_subcommands(cmd, &fn_name), subcmd_details = subcommand_details(cmd) ) .as_bytes() @@ -76,7 +78,7 @@ fi } } -fn all_subcommands(cmd: &Command) -> String { +fn all_subcommands(cmd: &Command, parent_fn_name: &str) -> String { debug!("all_subcommands"); fn add_command( @@ -106,9 +108,8 @@ fn all_subcommands(cmd: &Command) -> String { } } let mut subcmds = vec![]; - let fn_name = cmd.get_name().replace('-', "__"); for subcmd in cmd.get_subcommands() { - add_command(&fn_name, subcmd, &mut subcmds); + add_command(parent_fn_name, subcmd, &mut subcmds); } subcmds.sort();