Skip to content

Commit

Permalink
feat(complete): Offer - as a path option
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 21, 2024
1 parent 27b348d commit bb6493e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 13 additions & 1 deletion clap_complete/src/engine/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ where
pub struct PathCompleter {
current_dir: Option<std::path::PathBuf>,
filter: Option<Box<dyn Fn(&std::path::Path) -> bool + Send + Sync>>,
stdio: bool,
}

impl PathCompleter {
Expand All @@ -175,6 +176,7 @@ impl PathCompleter {
Self {
filter: None,
current_dir: None,
stdio: false,
}
}

Expand All @@ -188,6 +190,12 @@ impl PathCompleter {
Self::any().filter(|p| p.is_dir())
}

/// Include stdio (`-`)
pub fn stdio(mut self) -> Self {
self.stdio = true;
self
}

/// Select which paths should be completed
pub fn filter(
mut self,
Expand Down Expand Up @@ -218,7 +226,11 @@ impl ValueCompleter for PathCompleter {
current_dir_actual = std::env::current_dir().ok();
current_dir_actual.as_deref()
});
complete_path(current, current_dir, filter)
let mut candidates = complete_path(current, current_dir, filter);
if self.stdio && current.is_empty() {
candidates.push(CompletionCandidate::new("-").help(Some("stdio".into())));
}
candidates
}
}

Expand Down
5 changes: 4 additions & 1 deletion clap_complete/tests/testsuite/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ fn suggest_value_path_file() {
.long("input")
.short('i')
.add(ArgValueCompleter::new(
PathCompleter::file().current_dir(testdir_path.to_owned()),
PathCompleter::file()
.stdio()
.current_dir(testdir_path.to_owned()),
)),
)
.args_conflicts_with_subcommands(true);
Expand All @@ -604,6 +606,7 @@ a_file
b_file
c_dir/
d_dir/
- stdio
"#]],
);

Expand Down

0 comments on commit bb6493e

Please sign in to comment.