@@ -4,7 +4,15 @@ mod inspect;
4
4
mod list;
5
5
mod token;
6
6
7
+ use crate :: Cli ;
8
+ use clap:: CommandFactory ;
9
+ use clap_complete:: {
10
+ generate,
11
+ shells:: { Bash , Fish , Zsh } ,
12
+ } ;
13
+ use std:: path:: Path ;
7
14
use std:: process:: ExitCode ;
15
+ use std:: { env, io} ;
8
16
9
17
#[ derive( Debug , clap:: Subcommand ) ]
10
18
pub enum Command {
@@ -13,6 +21,7 @@ pub enum Command {
13
21
Token ( token:: GetToken ) ,
14
22
List ( list:: List ) ,
15
23
Inspect ( inspect:: Inspect ) ,
24
+ Completion { shell : String } ,
16
25
}
17
26
18
27
impl Command {
@@ -23,6 +32,25 @@ impl Command {
23
32
Self :: Token ( cmd) => cmd. run ( ) . await ,
24
33
Self :: List ( cmd) => cmd. run ( ) . await ,
25
34
Self :: Inspect ( cmd) => cmd. run ( ) . await ,
35
+ Self :: Completion { shell } => {
36
+ let mut cmd = Cli :: command ( ) ;
37
+ let bin_name = env:: args ( )
38
+ . next ( )
39
+ . and_then ( |path| {
40
+ Path :: new ( & path)
41
+ . file_stem ( )
42
+ . map ( |name| name. to_string_lossy ( ) . into_owned ( ) )
43
+ } )
44
+ . unwrap ( ) ;
45
+
46
+ match shell. as_str ( ) {
47
+ "bash" => generate ( Bash , & mut cmd, & bin_name, & mut io:: stdout ( ) ) ,
48
+ "zsh" => generate ( Zsh , & mut cmd, & bin_name, & mut io:: stdout ( ) ) ,
49
+ "fish" => generate ( Fish , & mut cmd, & bin_name, & mut io:: stdout ( ) ) ,
50
+ _ => eprintln ! ( "Unsupported shell: {}" , shell) ,
51
+ }
52
+ Ok ( ( ) )
53
+ }
26
54
}
27
55
. map ( |( ) | ExitCode :: SUCCESS )
28
56
}
0 commit comments