@@ -91,6 +91,8 @@ pub use shells::*;
91
91
pub struct CompleteEnv < ' s , F > {
92
92
factory : F ,
93
93
var : & ' static str ,
94
+ bin : Option < String > ,
95
+ completer : Option < String > ,
94
96
shells : Shells < ' s > ,
95
97
}
96
98
@@ -137,6 +139,8 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
137
139
Self {
138
140
factory,
139
141
var : "COMPLETE" ,
142
+ bin : None ,
143
+ completer : None ,
140
144
shells : Shells :: builtins ( ) ,
141
145
}
142
146
}
@@ -147,6 +151,22 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
147
151
self
148
152
}
149
153
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
+
150
170
/// Override the shells supported for completions
151
171
pub fn shells ( mut self , shells : Shells < ' s > ) -> Self {
152
172
self . shells = shells;
@@ -230,10 +250,19 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
230
250
args. drain ( 0 ..escape_index) ;
231
251
if args. is_empty ( ) {
232
252
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 ( ) ) ;
234
263
235
264
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) ?;
237
266
std:: io:: stdout ( ) . write_all ( & buf) ?;
238
267
} else {
239
268
let mut buf = Vec :: new ( ) ;
@@ -297,8 +326,8 @@ pub trait EnvCompleter {
297
326
///
298
327
/// - `var`: see [`CompleteEnv::var`]
299
328
/// - `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`]
302
331
///
303
332
/// **WARNING:** There are no stability guarantees between the call to
304
333
/// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`].
0 commit comments