Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(complete): Strip wrappers for running completer #5733

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
44 changes: 40 additions & 4 deletions clap_complete/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub use shells::*;
pub struct CompleteEnv<'s, F> {
factory: F,
var: &'static str,
bin: Option<String>,
completer: Option<String>,
shells: Shells<'s>,
}

Expand Down Expand Up @@ -137,6 +139,8 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
Self {
factory,
var: "COMPLETE",
bin: None,
completer: None,
shells: Shells::builtins(),
}
}
Expand All @@ -147,6 +151,22 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
self
}

/// Override the name of the binary to complete
///
/// Default: `Command::get_bin_name`
pub fn bin(mut self, bin: impl Into<String>) -> Self {
self.bin = Some(bin.into());
self
}

/// Override the binary to call to get completions
///
/// Default: `args_os()[0]`
pub fn completer(mut self, completer: impl Into<String>) -> Self {
self.completer = Some(completer.into());
self
}

/// Override the shells supported for completions
pub fn shells(mut self, shells: Shells<'s>) -> Self {
self.shells = shells;
Expand Down Expand Up @@ -222,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 @@ -230,10 +251,25 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
args.drain(0..escape_index);
if args.is_empty() {
let name = cmd.get_name();
let bin = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
let bin = self
.bin
.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, bin, &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 Expand Up @@ -297,8 +333,8 @@ pub trait EnvCompleter {
///
/// - `var`: see [`CompleteEnv::var`]
/// - `name`: an identifier to use in the script
/// - `bin`: the binary being completed
/// - `completer`: the command to run to generate completions
/// - `bin`: see [`CompleteEnv::bin`]
/// - `completer`: see [`CompleteEnv::completer`]
///
/// **WARNING:** There are no stability guarantees between the call to
/// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`].
Expand Down
Loading