@@ -4,11 +4,12 @@ mod config;
4
4
mod fmt;
5
5
mod opt;
6
6
7
- use clap:: Parser ;
7
+ use clap:: { CommandFactory , Parser } ;
8
8
9
9
use app:: App ;
10
10
use config:: Config ;
11
- use opt:: Opts ;
11
+ use opt:: { Command , CompletionsOpts , Opts , Shell , APP_NAME } ;
12
+ use std:: io;
12
13
use thiserror:: Error as ThisError ;
13
14
14
15
#[ derive( Debug , ThisError ) ]
@@ -29,10 +30,38 @@ pub enum Error {
29
30
30
31
pub type Result < T > = std:: result:: Result < T , Error > ;
31
32
33
+ fn print_completions ( opts : & CompletionsOpts ) -> Result < ( ) > {
34
+ use clap_complete:: {
35
+ generate,
36
+ shells:: { Bash , Elvish , Fish , PowerShell , Zsh } ,
37
+ } ;
38
+
39
+ let mut app = Opts :: command ( ) ;
40
+
41
+ match opts. shell {
42
+ Shell :: Bash => generate ( Bash , & mut app, APP_NAME , & mut io:: stdout ( ) ) ,
43
+ Shell :: Elvish => generate ( Elvish , & mut app, APP_NAME , & mut io:: stdout ( ) ) ,
44
+ Shell :: Fish => generate ( Fish , & mut app, APP_NAME , & mut io:: stdout ( ) ) ,
45
+ Shell :: PowerShell => generate ( PowerShell , & mut app, APP_NAME , & mut io:: stdout ( ) ) ,
46
+ Shell :: Zsh => generate ( Zsh , & mut app, APP_NAME , & mut io:: stdout ( ) ) ,
47
+ }
48
+ Ok ( ( ) )
49
+ }
50
+
32
51
fn main ( ) {
33
52
let config = Config :: load_default_location ( ) . unwrap_or_default ( ) ;
53
+ let opts = Opts :: parse ( ) ;
54
+
55
+ if let Command :: PrintCompletions ( opts) = & opts. cmd {
56
+ if let Err ( e) = print_completions ( opts) {
57
+ eprintln ! ( "Execution failed, reason: {}" , e) ;
58
+ std:: process:: exit ( 1 ) ;
59
+ } else {
60
+ std:: process:: exit ( 0 ) ;
61
+ }
62
+ }
34
63
35
- if let Err ( e) = App :: run ( Opts :: parse ( ) , config) {
64
+ if let Err ( e) = App :: run ( opts , config) {
36
65
eprintln ! ( "Execution failed, reason: {}" , e) ;
37
66
}
38
67
}
0 commit comments