1- use clap:: Parser ;
1+ use clap:: { Args , Parser , Subcommand } ;
22use tempfile:: tempdir;
33
44use docxtools:: xml_util:: XMLUtil ;
55use docxtools:: zip_util:: ZipUtil ;
66
7- #[ derive( Parser , Debug ) ]
7+ #[ derive( Parser ) ]
88#[ command( author, version, about, long_about = None ) ]
9- struct Args {
10- #[ arg( long) ]
11- command : String ,
12-
13- #[ arg( long) ]
14- grep_pattern : Option < String > ,
15-
9+ #[ command( propagate_version = true ) ]
10+ struct Cli {
1611 /// The docx file to operate on.
1712 #[ arg( short, long) ]
1813 in_file : String ,
1914
20- // #[arg(short, long)]
21- // out_file: String,
22-
2315 /// The temporary directory to use. If not specified a system temp directory
2416 /// will be used and cleaned after use.
2517 #[ arg( short, long) ]
26- temp_dir : Option < String >
18+ temp_dir : Option < String > ,
19+
20+ #[ command( subcommand) ]
21+ command : Commands ,
22+ }
23+
24+ #[ derive( Subcommand ) ]
25+ enum Commands {
26+ /// List the text from the document to the console
27+ Cat ( CatArgs ) ,
28+
29+ /// Search the text in the document like 'grep'
30+ Grep ( GrepArgs ) ,
31+ }
32+
33+ #[ derive( Args ) ]
34+ struct CatArgs {
35+ }
36+
37+ #[ derive( Args ) ]
38+ struct GrepArgs {
39+ regex : String ,
2740}
41+
2842fn main ( ) {
29- let args = Args :: parse ( ) ;
43+ let args = Cli :: parse ( ) ;
3044
3145 std:: process:: exit ( real_main ( args) ) ;
3246}
3347
34- fn real_main ( args : Args ) -> i32 {
35- // let args: Vec<_> = std::env::args().collect();
36- // if args.len() < 4 {
37- // println!("Usage: {} <in-filename> <out-filename> <tempdir>", args[0]);
38- // return 1;
39- // }
40-
41- // let src_file = &*args[1];
48+ fn real_main ( args : Cli ) -> i32 {
4249 let src_file = args. in_file ;
4350 // let dest_file = args.out_file;
4451
@@ -51,19 +58,17 @@ fn real_main(args: Args) -> i32 {
5158 temp_dir = fstempdir. path ( ) . to_string_lossy ( ) . to_string ( ) ;
5259 }
5360
54- // let temp_dir = args.temp_dir;
55-
5661 ZipUtil :: read_zip ( & src_file, & temp_dir) . unwrap ( ) ;
5762
58- match args. command . as_str ( ) {
59- "cat" => {
63+ match & args. command {
64+ Commands :: Cat ( _ ) => {
6065 XMLUtil :: cat ( & temp_dir) ;
6166 } ,
62- "grep" => {
63- XMLUtil :: grep_xml ( & temp_dir, & args. grep_pattern . unwrap ( ) )
64- } ,
65- _ => panic ! ( "Unknown command {}" , args. command)
67+ Commands :: Grep ( grep_args) => {
68+ XMLUtil :: grep_xml ( & temp_dir, & grep_args. regex )
69+ }
6670 }
71+
6772 // XMLUtil::read_xmls(&temp_dir); // .unwrap();
6873
6974 // ZipUtil::write_zip(&temp_dir, &dest_file).unwrap();
0 commit comments