@@ -4,7 +4,15 @@ mod inspect;
44mod list;
55mod token;
66
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 ;
714use std:: process:: ExitCode ;
15+ use std:: { env, io} ;
816
917#[ derive( Debug , clap:: Subcommand ) ]
1018pub enum Command {
@@ -13,6 +21,7 @@ pub enum Command {
1321 Token ( token:: GetToken ) ,
1422 List ( list:: List ) ,
1523 Inspect ( inspect:: Inspect ) ,
24+ Completion { shell : String } ,
1625}
1726
1827impl Command {
@@ -23,6 +32,25 @@ impl Command {
2332 Self :: Token ( cmd) => cmd. run ( ) . await ,
2433 Self :: List ( cmd) => cmd. run ( ) . await ,
2534 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+ }
2654 }
2755 . map ( |( ) | ExitCode :: SUCCESS )
2856 }
0 commit comments