Skip to content

Commit

Permalink
fix(complete): Fix path completion in bash
Browse files Browse the repository at this point in the history
Fix #5239
  • Loading branch information
sudotac committed Jan 20, 2024
1 parent 62a5ace commit 3a222de
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
51 changes: 35 additions & 16 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,48 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String {
let mut opts = vec![String::new()];

for o in p.get_opts() {
let compopt = match o.get_value_hint() {
ValueHint::FilePath => Some("compopt -o filenames"),
_ => None,
};

if let Some(longs) = o.get_long_and_visible_aliases() {
opts.extend(longs.iter().map(|long| {
format!(
"--{})
COMPREPLY=({})
return 0
;;",
long,
vals_for(o)
)
let mut v = vec![
format!("--{})", long),
format!("COMPREPLY=({})", vals_for(o)),
];

if let Some(copt) = compopt {
v.extend([
r#"if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then"#.to_string(),
format!(" {}", copt),
"fi".to_string(),
]);
}

v.extend(["return 0", ";;"].iter().map(|s| s.to_string()));
v.join("\n ")
}));
}

if let Some(shorts) = o.get_short_and_visible_aliases() {
opts.extend(shorts.iter().map(|short| {
format!(
"-{})
COMPREPLY=({})
return 0
;;",
short,
vals_for(o)
)
let mut v = vec![
format!("-{})", short),
format!("COMPREPLY=({})", vals_for(o)),
];

if let Some(copt) = compopt {
v.extend([
r#"if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then"#.to_string(),
format!(" {}", copt),
"fi".to_string(),
]);
}

v.extend(["return 0", ";;"].iter().map(|s| s.to_string()));
v.join("\n ")
}));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,16 @@ _exhaustive() {
;;
--file)
COMPREPLY=($(compgen -f "${cur}"))
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
-f)
COMPREPLY=($(compgen -f "${cur}"))
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
--dir)
Expand Down
6 changes: 6 additions & 0 deletions clap_complete/tests/snapshots/value_hint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ _my-app() {
;;
--file)
COMPREPLY=($(compgen -f "${cur}"))
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
-f)
COMPREPLY=($(compgen -f "${cur}"))
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o filenames
fi
return 0
;;
--dir)
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn complete() {

// Issue 5239 (https://github.com/clap-rs/clap/issues/5239)
let input = "exhaustive hint --file test\t";
let expected = "exhaustive hint --file test % exhaustive hint --file tests ";
let expected = "exhaustive hint --file test % exhaustive hint --file tests/";
let actual = runtime.complete(input, &term).unwrap();
snapbox::assert_eq(expected, actual);

Expand Down

0 comments on commit 3a222de

Please sign in to comment.