Skip to content

Commit b2c8e44

Browse files
committed
feat(complete): Allow user to override bin/completer
1 parent 21c9892 commit b2c8e44

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

clap_complete/src/env/mod.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ pub use shells::*;
9191
pub struct CompleteEnv<'s, F> {
9292
factory: F,
9393
var: &'static str,
94+
bin: Option<String>,
95+
completer: Option<String>,
9496
shells: Shells<'s>,
9597
}
9698

@@ -137,6 +139,8 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
137139
Self {
138140
factory,
139141
var: "COMPLETE",
142+
bin: None,
143+
completer: None,
140144
shells: Shells::builtins(),
141145
}
142146
}
@@ -147,6 +151,22 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
147151
self
148152
}
149153

154+
/// Override the name of the binary to complete
155+
///
156+
/// Default: `Command::get_bin_name`
157+
pub fn bin(mut self, bin: impl Into<String>) -> Self {
158+
self.bin = Some(bin.into());
159+
self
160+
}
161+
162+
/// Override the binary to call to get completions
163+
///
164+
/// Default: `Command::get_bin_name`
165+
pub fn completer(mut self, completer: impl Into<String>) -> Self {
166+
self.completer = Some(completer.into());
167+
self
168+
}
169+
150170
/// Override the shells supported for completions
151171
pub fn shells(mut self, shells: Shells<'s>) -> Self {
152172
self.shells = shells;
@@ -230,10 +250,19 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
230250
args.drain(0..escape_index);
231251
if args.is_empty() {
232252
let name = cmd.get_name();
233-
let bin = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
253+
let bin = self
254+
.bin
255+
.as_deref()
256+
.or_else(|| cmd.get_bin_name())
257+
.unwrap_or_else(|| cmd.get_name());
258+
let completer = self
259+
.completer
260+
.as_deref()
261+
.or_else(|| cmd.get_bin_name())
262+
.unwrap_or_else(|| cmd.get_name());
234263

235264
let mut buf = Vec::new();
236-
shell.write_registration(self.var, name, bin, bin, &mut buf)?;
265+
shell.write_registration(self.var, name, bin, completer, &mut buf)?;
237266
std::io::stdout().write_all(&buf)?;
238267
} else {
239268
let mut buf = Vec::new();
@@ -297,8 +326,8 @@ pub trait EnvCompleter {
297326
///
298327
/// - `var`: see [`CompleteEnv::var`]
299328
/// - `name`: an identifier to use in the script
300-
/// - `bin`: the binary being completed
301-
/// - `completer`: the command to run to generate completions
329+
/// - `bin`: see [`CompleteEnv::bin`]
330+
/// - `completer`: see [`CompleteEnv::completer`]
302331
///
303332
/// **WARNING:** There are no stability guarantees between the call to
304333
/// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`].

0 commit comments

Comments
 (0)