fix(gen): in bash, pass use possible_values if available

Change-Id: I7a54821666f0ba366379d6cac83898fcd9b76ee6
This commit is contained in:
Nicolas Stinus 2021-10-14 14:41:11 -04:00
parent c5296977e5
commit de1c764905
2 changed files with 17 additions and 5 deletions

View file

@ -201,9 +201,20 @@ fn all_options_for_path(app: &App, path: &str) -> String {
longs = utils::longs_and_visible_aliases(p) longs = utils::longs_and_visible_aliases(p)
.iter() .iter()
.fold(String::new(), |acc, l| format!("{} --{}", acc, l)), .fold(String::new(), |acc, l| format!("{} --{}", acc, l)),
pos = p pos = p.get_positionals().fold(String::new(), |acc, p| {
.get_positionals() if let Some(vals) = p.get_possible_values() {
.fold(String::new(), |acc, p| format!("{} {}", acc, p)), format!(
"{} {}",
acc,
vals.iter()
.map(|x| x.get_name())
.collect::<Vec<_>>()
.join(" ")
)
} else {
format!("{} {}", acc, p)
}
}),
subcmds = scs.join(" "), subcmds = scs.join(" "),
); );

View file

@ -14,6 +14,7 @@ fn build_app_with_name(s: &'static str) -> App<'static> {
.value_hint(ValueHint::FilePath) .value_hint(ValueHint::FilePath)
.about("some input file"), .about("some input file"),
) )
.arg(Arg::new("choice").possible_values(["first", "second"]))
.subcommand( .subcommand(
App::new("test").about("tests things").arg( App::new("test").about("tests things").arg(
Arg::new("case") Arg::new("case")
@ -57,7 +58,7 @@ static BASH: &str = r#"_myapp() {
case "${cmd}" in case "${cmd}" in
myapp) myapp)
opts=" -h -V --help --version <file> test help" opts=" -h -V --help --version <file> first second test help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -160,7 +161,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
case "${cmd}" in case "${cmd}" in
my_app) my_app)
opts=" -h -V --help --version <file> test some_cmd some-cmd-with-hypens help" opts=" -h -V --help --version <file> first second test some_cmd some-cmd-with-hypens help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0