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 Dec 3, 2023
1 parent c0a1814 commit b1d3f9e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
43 changes: 27 additions & 16 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,40 @@ 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.push(copt.to_string());
}

v.extend(vec!["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.push(copt.to_string());
}

v.extend(vec!["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,12 @@ _exhaustive() {
;;
--file)
COMPREPLY=($(compgen -f "${cur}"))
compopt -o filenames
return 0
;;
-f)
COMPREPLY=($(compgen -f "${cur}"))
compopt -o filenames
return 0
;;
--dir)
Expand Down
2 changes: 2 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,12 @@ _my-app() {
;;
--file)
COMPREPLY=($(compgen -f "${cur}"))
compopt -o filenames
return 0
;;
-f)
COMPREPLY=($(compgen -f "${cur}"))
compopt -o filenames
return 0
;;
--dir)
Expand Down

0 comments on commit b1d3f9e

Please sign in to comment.