diff --git a/clap_generate/src/generators/shells/bash.rs b/clap_generate/src/generators/shells/bash.rs index e975e43a..2d74bfc7 100644 --- a/clap_generate/src/generators/shells/bash.rs +++ b/clap_generate/src/generators/shells/bash.rs @@ -1,9 +1,10 @@ // Std -use std::io::Write; +use std::{fmt::Write as _, io::Write}; // Internal use crate::utils; use crate::Generator; + use clap::*; /// Generate bash completion file @@ -191,32 +192,27 @@ fn all_options_for_path(app: &App, path: &str) -> String { debug!("all_options_for_path: path={}", path); let p = utils::find_subcommand_with_path(app, path.split("__").skip(1).collect()); - let scs: Vec<_> = utils::subcommands(p).iter().map(|x| x.0.clone()).collect(); - let opts = format!( - "{shorts} {longs} {pos} {subcmds}", - shorts = utils::shorts_and_visible_aliases(p) - .iter() - .fold(String::new(), |acc, s| format!("{} -{}", acc, s)), - longs = utils::longs_and_visible_aliases(p) - .iter() - .fold(String::new(), |acc, l| format!("{} --{}", acc, l)), - pos = p.get_positionals().fold(String::new(), |acc, p| { - if let Some(vals) = p.get_possible_values() { - format!( - "{} {}", - acc, - vals.iter() - .map(|x| x.get_name()) - .collect::>() - .join(" ") - ) - } else { - format!("{} {}", acc, p) + let mut opts = String::new(); + for short in utils::shorts_and_visible_aliases(p) { + write!(&mut opts, "-{} ", short).unwrap(); + } + for long in utils::longs_and_visible_aliases(p) { + write!(&mut opts, "--{} ", long).unwrap(); + } + for pos in p.get_positionals() { + if let Some(vals) = pos.get_possible_values() { + for value in vals { + write!(&mut opts, "{} ", value.get_name()).unwrap(); } - }), - subcmds = scs.join(" "), - ); + } else { + write!(&mut opts, "{} ", pos).unwrap(); + } + } + for (sc, _) in utils::subcommands(p) { + write!(&mut opts, "{} ", sc).unwrap(); + } + opts.pop(); opts } diff --git a/clap_generate/tests/completions/bash.rs b/clap_generate/tests/completions/bash.rs index 99966edf..ca0b94b8 100644 --- a/clap_generate/tests/completions/bash.rs +++ b/clap_generate/tests/completions/bash.rs @@ -58,7 +58,7 @@ static BASH: &str = r#"_myapp() { case "${cmd}" in myapp) - opts=" -h -V --help --version first second test help" + opts="-h -V --help --version first second test help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -72,7 +72,7 @@ static BASH: &str = r#"_myapp() { return 0 ;; myapp__help) - opts=" -h --help " + opts="-h --help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -86,7 +86,7 @@ static BASH: &str = r#"_myapp() { return 0 ;; myapp__test) - opts=" -h -V --case --help --version " + opts="-h -V --case --help --version" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -161,7 +161,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { case "${cmd}" in my_app) - opts=" -h -V --help --version first second test some_cmd some-cmd-with-hypens help" + opts="-h -V --help --version first second test some_cmd some-cmd-with-hypens help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -175,7 +175,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { return 0 ;; my_app__help) - opts=" -h --help " + opts="-h --help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -189,7 +189,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { return 0 ;; my_app__some__cmd__with__hypens) - opts=" -h -V --help --version " + opts="-h -V --help --version" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -203,7 +203,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { return 0 ;; my_app__some_cmd) - opts=" -h -V --config --help --version " + opts="-h -V --config --help --version" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -221,7 +221,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { return 0 ;; my_app__test) - opts=" -h -V --case --help --version " + opts="-h -V --case --help --version" if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 @@ -295,7 +295,7 @@ static BASH_ALIASES: &str = r#"_cmd() { case "${cmd}" in cmd) - opts=" -h -V -F -f -O -o --help --version --flg --flag --opt --option " + opts="-h -V -F -f -O -o --help --version --flg --flag --opt --option " if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0