1
- use clap_complete:: { generate, Generator , Shell } ;
1
+ use std:: { env, path:: PathBuf } ;
2
+
3
+ use clap_complete:: { generate_to, Generator , Shell } ;
2
4
use dialoguer:: Select ;
3
5
4
- pub fn auto_complete ( app : & mut clap:: Command ) {
6
+ pub fn auto_complete ( app : & mut clap:: Command , output_path : Option < & PathBuf > ) {
5
7
let shell = select_shell ( ) ;
6
- print_completer ( shell, app) ;
8
+ print_completer ( shell, app, output_path ) ;
7
9
}
8
10
9
11
fn select_shell ( ) -> Shell {
10
- let items: Vec < Shell > = vec ! [ Shell :: Bash , Shell :: Elvish , Shell :: Fish , Shell :: PowerShell , Shell :: Zsh ] ;
12
+ let items: Vec < Shell > = vec ! [ Shell :: Bash , Shell :: Elvish , Shell :: Fish , Shell :: PowerShell ] ;
11
13
12
14
let selection = Select :: new ( )
13
15
. with_prompt ( "Which shell are you using?" )
@@ -17,8 +19,12 @@ fn select_shell() -> Shell {
17
19
18
20
items[ selection]
19
21
}
20
- fn print_completer < G : Generator > ( generator : G , app : & mut clap:: Command ) {
21
- let name = app. get_name ( ) . to_owned ( ) ;
22
+ fn print_completer < G : Generator > ( generator : G , app : & mut clap:: Command , output_path : Option < & PathBuf > ) {
23
+ let mut name = "auto-complete" . to_string ( ) ;
24
+ if output_path. is_some ( ) {
25
+ name = output_path. unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
26
+ }
27
+ let out_dir: PathBuf = env:: current_dir ( ) . expect ( "can't get current directory" ) ;
22
28
23
- generate ( generator, app, name, & mut std :: io :: stdout ( ) ) ;
29
+ let _ = generate_to ( generator, app, name, out_dir ) ;
24
30
}
0 commit comments