Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(complete): Add DirPath support in bash
  • Loading branch information
sudotac committed Jan 20, 2024
commit e25b1abddf7c32001a15caf0aa1e2630a6822ea5
3 changes: 3 additions & 0 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn option_details_for_path(cmd: &Command, path: &str) -> String {
for o in p.get_opts() {
let compopt = match o.get_value_hint() {
ValueHint::FilePath => Some("compopt -o filenames"),
ValueHint::DirPath => Some("compopt -o plusdirs"),
_ => None,
};

Expand Down Expand Up @@ -229,6 +230,8 @@ fn vals_for(o: &Arg) -> String {
.collect::<Vec<_>>()
.join(" ")
)
} else if o.get_value_hint() == ValueHint::DirPath {
String::from("") // should be empty to avoid duplicate candidates
} else if o.get_value_hint() == ValueHint::Other {
String::from("\"${cur}\"")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,17 @@ _exhaustive() {
return 0
;;
--dir)
COMPREPLY=($(compgen -f "${cur}"))
COMPREPLY=()
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o plusdirs
fi
return 0
;;
-d)
COMPREPLY=($(compgen -f "${cur}"))
COMPREPLY=()
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o plusdirs
fi
return 0
;;
--exe)
Expand Down
10 changes: 8 additions & 2 deletions clap_complete/tests/snapshots/value_hint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ _my-app() {
return 0
;;
--dir)
COMPREPLY=($(compgen -f "${cur}"))
COMPREPLY=()
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o plusdirs
fi
return 0
;;
-d)
COMPREPLY=($(compgen -f "${cur}"))
COMPREPLY=()
if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then
compopt -o plusdirs
fi
return 0
;;
--exe)
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ fn complete() {
);
let actual = runtime.complete(input.as_str(), &term).unwrap();
assert!(
actual.contains("a_file")
&& actual.contains("b_file")
!actual.contains("a_file")
&& !actual.contains("b_file")
&& actual.contains("c_dir")
&& actual.contains("d_dir"),
"Actual output:\n{}",
Expand Down