diff --git a/clap_complete/src/shells/bash.rs b/clap_complete/src/shells/bash.rs index 50f825cf992..d7212146363 100644 --- a/clap_complete/src/shells/bash.rs +++ b/clap_complete/src/shells/bash.rs @@ -75,26 +75,45 @@ complete -F _{name} -o bashdefault -o default {name} fn all_subcommands(cmd: &Command) -> String { debug!("all_subcommands"); - let mut subcmds = vec![String::new()]; - let mut scs = utils::all_subcommands(cmd) - .iter() - .map(|x| x.0.clone()) - .collect::>(); - - scs.sort(); - scs.dedup(); + fn add_command( + parent_fn_name: &str, + cmd: &Command, + subcmds: &mut Vec<(String, String, String)>, + ) { + let fn_name = format!( + "{parent_fn_name}__{cmd_name}", + parent_fn_name = parent_fn_name, + cmd_name = cmd.get_name().to_string().replace('-', "__") + ); + subcmds.push(( + parent_fn_name.to_string(), + cmd.get_name().to_string(), + fn_name.clone(), + )); + for subcmd in cmd.get_subcommands() { + add_command(&fn_name, subcmd, subcmds); + } + } + let mut subcmds = vec![]; + let fn_name = cmd.get_name().replace('-', "__"); + for subcmd in cmd.get_subcommands() { + add_command(&fn_name, subcmd, &mut subcmds); + } + subcmds.sort(); - subcmds.extend(scs.iter().map(|sc| { - format!( - "*,{name}) - cmd+=\"__{fn_name}\" + let mut cases = vec![String::new()]; + for (parent_fn_name, name, fn_name) in subcmds { + cases.push(format!( + "{parent_fn_name},{name}) + cmd=\"{fn_name}\" ;;", - name = sc, - fn_name = sc.replace('-', "__") - ) - })); + parent_fn_name = parent_fn_name, + name = name, + fn_name = fn_name, + )); + } - subcmds.join("\n ") + cases.join("\n ") } fn subcommand_details(cmd: &Command) -> String { diff --git a/clap_complete/tests/snapshots/basic.bash b/clap_complete/tests/snapshots/basic.bash index ca9d701d929..cf6197a0ec8 100644 --- a/clap_complete/tests/snapshots/basic.bash +++ b/clap_complete/tests/snapshots/basic.bash @@ -12,11 +12,11 @@ _my-app() { ",$1") cmd="my__app" ;; - *,help) - cmd+="__help" + my__app,help) + cmd="my__app__help" ;; - *,test) - cmd+="__test" + my__app,test) + cmd="my__app__test" ;; *) ;; diff --git a/clap_complete/tests/snapshots/feature_sample.bash b/clap_complete/tests/snapshots/feature_sample.bash index 00001f2768c..3520480a8d4 100644 --- a/clap_complete/tests/snapshots/feature_sample.bash +++ b/clap_complete/tests/snapshots/feature_sample.bash @@ -12,11 +12,11 @@ _my-app() { ",$1") cmd="my__app" ;; - *,help) - cmd+="__help" + my__app,help) + cmd="my__app__help" ;; - *,test) - cmd+="__test" + my__app,test) + cmd="my__app__test" ;; *) ;; diff --git a/clap_complete/tests/snapshots/quoting.bash b/clap_complete/tests/snapshots/quoting.bash index 55fb938298b..4698b5fe313 100644 --- a/clap_complete/tests/snapshots/quoting.bash +++ b/clap_complete/tests/snapshots/quoting.bash @@ -12,26 +12,26 @@ _my-app() { ",$1") cmd="my__app" ;; - *,cmd-backslash) - cmd+="__cmd__backslash" + my__app,cmd-backslash) + cmd="my__app__cmd__backslash" ;; - *,cmd-backticks) - cmd+="__cmd__backticks" + my__app,cmd-backticks) + cmd="my__app__cmd__backticks" ;; - *,cmd-brackets) - cmd+="__cmd__brackets" + my__app,cmd-brackets) + cmd="my__app__cmd__brackets" ;; - *,cmd-double-quotes) - cmd+="__cmd__double__quotes" + my__app,cmd-double-quotes) + cmd="my__app__cmd__double__quotes" ;; - *,cmd-expansions) - cmd+="__cmd__expansions" + my__app,cmd-expansions) + cmd="my__app__cmd__expansions" ;; - *,cmd-single-quotes) - cmd+="__cmd__single__quotes" + my__app,cmd-single-quotes) + cmd="my__app__cmd__single__quotes" ;; - *,help) - cmd+="__help" + my__app,help) + cmd="my__app__help" ;; *) ;; diff --git a/clap_complete/tests/snapshots/special_commands.bash b/clap_complete/tests/snapshots/special_commands.bash index 6be662dce52..642954ab455 100644 --- a/clap_complete/tests/snapshots/special_commands.bash +++ b/clap_complete/tests/snapshots/special_commands.bash @@ -12,20 +12,20 @@ _my-app() { ",$1") cmd="my__app" ;; - *,help) - cmd+="__help" + my__app,help) + cmd="my__app__help" ;; - *,some-cmd-with-hyphens) - cmd+="__some__cmd__with__hyphens" + my__app,some-cmd-with-hyphens) + cmd="my__app__some__cmd__with__hyphens" ;; - *,some-hidden-cmd) - cmd+="__some__hidden__cmd" + my__app,some-hidden-cmd) + cmd="my__app__some__hidden__cmd" ;; - *,some_cmd) - cmd+="__some_cmd" + my__app,some_cmd) + cmd="my__app__some_cmd" ;; - *,test) - cmd+="__test" + my__app,test) + cmd="my__app__test" ;; *) ;; diff --git a/clap_complete/tests/snapshots/sub_subcommands.bash b/clap_complete/tests/snapshots/sub_subcommands.bash index 5f43d010306..a1022c9a4e3 100644 --- a/clap_complete/tests/snapshots/sub_subcommands.bash +++ b/clap_complete/tests/snapshots/sub_subcommands.bash @@ -12,17 +12,20 @@ _my-app() { ",$1") cmd="my__app" ;; - *,help) - cmd+="__help" + my__app,help) + cmd="my__app__help" ;; - *,some_cmd) - cmd+="__some_cmd" + my__app,some_cmd) + cmd="my__app__some_cmd" ;; - *,sub_cmd) - cmd+="__sub_cmd" + my__app,test) + cmd="my__app__test" ;; - *,test) - cmd+="__test" + my__app__some_cmd,help) + cmd="my__app__some_cmd__help" + ;; + my__app__some_cmd,sub_cmd) + cmd="my__app__some_cmd__sub_cmd" ;; *) ;;