From de1c764905947102616b5d2c10770dbd76e0065b Mon Sep 17 00:00:00 2001 From: Nicolas Stinus Date: Thu, 14 Oct 2021 14:41:11 -0400 Subject: [PATCH] fix(gen): in bash, pass use possible_values if available Change-Id: I7a54821666f0ba366379d6cac83898fcd9b76ee6 --- clap_generate/src/generators/shells/bash.rs | 17 ++++++++++++++--- clap_generate/tests/completions/bash.rs | 5 +++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/clap_generate/src/generators/shells/bash.rs b/clap_generate/src/generators/shells/bash.rs index 11075726..e975e43a 100644 --- a/clap_generate/src/generators/shells/bash.rs +++ b/clap_generate/src/generators/shells/bash.rs @@ -201,9 +201,20 @@ fn all_options_for_path(app: &App, path: &str) -> String { 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| format!("{} {}", acc, p)), + 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) + } + }), subcmds = scs.join(" "), ); diff --git a/clap_generate/tests/completions/bash.rs b/clap_generate/tests/completions/bash.rs index 5a1773b1..99966edf 100644 --- a/clap_generate/tests/completions/bash.rs +++ b/clap_generate/tests/completions/bash.rs @@ -14,6 +14,7 @@ fn build_app_with_name(s: &'static str) -> App<'static> { .value_hint(ValueHint::FilePath) .about("some input file"), ) + .arg(Arg::new("choice").possible_values(["first", "second"])) .subcommand( App::new("test").about("tests things").arg( Arg::new("case") @@ -57,7 +58,7 @@ static BASH: &str = r#"_myapp() { case "${cmd}" in myapp) - opts=" -h -V --help --version 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 @@ -160,7 +161,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() { case "${cmd}" in my_app) - opts=" -h -V --help --version 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