@@ -4,15 +4,43 @@ use anyhow::Result;
44use std:: fs;
55use std:: io;
66use std:: io:: { Read , Write } ;
7+ use clap:: { App , AppSettings , Arg , SubCommand } ;
78
8- use mates:: app;
99use mates:: cli;
1010use mates:: utils;
1111
1212fn main ( ) -> Result < ( ) > {
13- let matches = app:: app ( ) . get_matches ( ) ;
14-
15- let command = matches. subcommand_name ( ) . unwrap ( ) ;
13+ let app = App :: new ( "mates" )
14+ . version ( env ! ( "CARGO_PKG_VERSION" ) )
15+ . author ( "Markus Unterwaditzer" )
16+ . about ( "A simple commandline addressbook" )
17+ . setting ( AppSettings :: SubcommandRequired )
18+ . subcommand ( SubCommand :: with_name ( "index" ) . about ( "Rewrite/create the index" ) )
19+ . subcommand (
20+ SubCommand :: with_name ( "mutt-query" )
21+ . about ( "Search for contact, output is usable for mutt's query_command." )
22+ . arg ( Arg :: with_name ( "query" ) . index ( 1 ) ) ,
23+ )
24+ . subcommand (
25+ SubCommand :: with_name ( "file-query" )
26+ . about ( "Search for contact, return just the filename." )
27+ . arg ( Arg :: with_name ( "query" ) . index ( 1 ) ) ,
28+ )
29+ . subcommand (
30+ SubCommand :: with_name ( "email-query" )
31+ . about ( "Search for contact, return \" name <email>\" ." )
32+ . arg ( Arg :: with_name ( "query" ) . index ( 1 ) ) ,
33+ )
34+ . subcommand (
35+ SubCommand :: with_name ( "add" )
36+ . about ( "Take mail from stdin, add sender to contacts. Print filename." ) ,
37+ )
38+ . subcommand (
39+ SubCommand :: with_name ( "edit" )
40+ . about ( "Open contact (given by filepath or search-string) interactively." )
41+ . arg ( Arg :: with_name ( "file-or-query" ) . index ( 1 ) ) ,
42+ )
43+ . get_matches ( ) ;
1644
1745 let config = match cli:: Configuration :: new ( ) {
1846 Ok ( x) => x,
@@ -21,31 +49,30 @@ fn main() -> Result<()> {
2149 }
2250 } ;
2351
24- let submatches = matches
25- . subcommand_matches ( command)
26- . expect ( "Internal error." ) ;
27-
28- match command {
29- "index" => {
52+ match app. subcommand ( ) {
53+ ( "index" , Some ( _subs) ) => {
3054 println ! (
3155 "Rebuilding index file \" {}\" ..." ,
3256 config. index_path. display( )
3357 ) ;
3458 cli:: build_index ( & config. index_path , & config. vdir_path ) ?;
3559 }
36- "mutt-query" => {
37- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
38- cli:: mutt_query ( & config, & query[ ..] ) ?;
60+ ( "mutt-query" , Some ( subs) ) => {
61+ if let Some ( value) = subs. value_of ( "query" ) {
62+ cli:: mutt_query ( & config, value) ?
63+ }
3964 }
40- "file-query" => {
41- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
42- cli:: file_query ( & config, & query[ ..] ) ?;
65+ ( "file-query" , Some ( subs) ) => {
66+ if let Some ( value) = subs. value_of ( "query" ) {
67+ cli:: file_query ( & config, value) ?
68+ }
4369 }
44- "email-query" => {
45- let query = submatches. value_of ( "query" ) . unwrap_or ( "" ) ;
46- cli:: email_query ( & config, & query[ ..] ) ?;
70+ ( "email-query" , Some ( subs) ) => {
71+ if let Some ( value) = subs. value_of ( "query" ) {
72+ cli:: email_query ( & config, value) ?
73+ }
4774 }
48- "add" => {
75+ ( "add" , Some ( .. ) ) => {
4976 let stdin = io:: stdin ( ) ;
5077 let mut email = String :: new ( ) ;
5178 stdin. lock ( ) . read_to_string ( & mut email) ?;
@@ -60,13 +87,13 @@ fn main() -> Result<()> {
6087 let index_entry = utils:: index_item_from_contact ( & contact) ?;
6188 index_fp. write_all ( index_entry. as_bytes ( ) ) ?;
6289 }
63- "edit" => {
64- let query = submatches. value_of ( "file-or-query" ) . unwrap_or ( "" ) ;
65- cli:: edit_contact ( & config, & query[ ..] ) ?;
66- }
67- _ => {
68- return Err ( anyhow ! ( "Invalid command: {}" , command) ) ;
90+ ( "edit" , Some ( subs) ) => {
91+ if let Some ( value) = subs. value_of ( "file-or-query" ) {
92+ cli:: edit_contact ( & config, value) ?
93+ }
6994 }
70- } ;
95+ _ => ( ) ,
96+ }
97+
7198 Ok ( ( ) )
7299}
0 commit comments