Skip to content

Commit

Permalink
fix(complete): Strip wrappers for running completer
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 17, 2024
1 parent b2c8e44 commit a7a8f93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion clap_complete/examples/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use clap_complete::{generate, Generator, Shell};

fn main() {
#[cfg(feature = "unstable-dynamic")]
clap_complete::CompleteEnv::with_factory(cli).complete();
clap_complete::CompleteEnv::with_factory(cli)
// Avoid tests snapshotting a path into `target/`
.completer("exhaustive")
.complete();

let matches = cli().get_matches();
if let Some(generator) = matches.get_one::<Shell>("generate") {
Expand Down
21 changes: 14 additions & 7 deletions clap_complete/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {

/// Override the binary to call to get completions
///
/// Default: `Command::get_bin_name`
/// Default: `args_os()[0]`
pub fn completer(mut self, completer: impl Into<String>) -> Self {
self.completer = Some(completer.into());
self
Expand Down Expand Up @@ -242,6 +242,7 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
let mut cmd = (self.factory)();
cmd.build();

let completer = args.remove(0);
let escape_index = args
.iter()
.position(|a| *a == "--")
Expand All @@ -255,14 +256,20 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
.as_deref()
.or_else(|| cmd.get_bin_name())
.unwrap_or_else(|| cmd.get_name());
let completer = self
.completer
.as_deref()
.or_else(|| cmd.get_bin_name())
.unwrap_or_else(|| cmd.get_name());
let completer = if let Some(completer) = self.completer.as_deref() {
completer.to_owned()
} else {
let mut completer = std::path::PathBuf::from(completer);
if let Some(current_dir) = current_dir.as_deref() {
if 1 < completer.components().count() {
completer = current_dir.join(completer);
}
}
completer.to_string_lossy().into_owned()
};

let mut buf = Vec::new();
shell.write_registration(self.var, name, bin, completer, &mut buf)?;
shell.write_registration(self.var, name, bin, &completer, &mut buf)?;
std::io::stdout().write_all(&buf)?;
} else {
let mut buf = Vec::new();
Expand Down

0 comments on commit a7a8f93

Please sign in to comment.