@@ -8,17 +8,45 @@ use winproc::find_process_id_by_name;
88
99use crate :: winproc:: capture_debug_output;
1010
11- fn get_app_name_from_args ( ) -> Option < String > {
11+ struct AppArgs {
12+ app_name : Option < String > ,
13+ wait : bool ,
14+ }
15+
16+ fn get_args ( ) -> AppArgs {
17+ let mut app_name = None ;
18+ let mut wait = false ;
1219 let mut args = env:: args ( ) ;
1320 args. next ( ) ; // skip program name
14- args. next ( )
21+ for arg in args {
22+ if arg == "--wait" {
23+ wait = true ;
24+ } else if app_name. is_none ( ) {
25+ app_name = Some ( arg) ;
26+ }
27+ }
28+ AppArgs { app_name, wait }
1529}
1630
1731fn main ( ) {
18- match get_app_name_from_args ( ) {
19- Some ( app_name) => match find_process_id_by_name ( & app_name) {
32+ let args = get_args ( ) ;
33+ match ( args. app_name , args. wait ) {
34+ ( Some ( app_name) , true ) => {
35+ // Wait for process to appear
36+ let pid = loop {
37+ if let Some ( pid) = find_process_id_by_name ( & app_name) {
38+ break pid;
39+ }
40+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
41+ } ;
42+ println ! ( "Process ID: {}" , pid) ;
43+ if let Err ( e) = capture_debug_output ( Some ( pid) ) {
44+ eprintln ! ( "Error capturing debug output: {}" , e) ;
45+ process:: exit ( 1 ) ;
46+ }
47+ }
48+ ( Some ( app_name) , false ) => match find_process_id_by_name ( & app_name) {
2049 Some ( pid) => {
21- // Print the process ID and capture debug output
2250 println ! ( "Process ID: {}" , pid) ;
2351 if let Err ( e) = capture_debug_output ( Some ( pid) ) {
2452 eprintln ! ( "Error capturing debug output: {}" , e) ;
@@ -30,7 +58,11 @@ fn main() {
3058 process:: exit ( 1 ) ;
3159 }
3260 } ,
33- None => {
61+ ( None , true ) => {
62+ eprintln ! ( "Error: --wait switch requires an app name." ) ;
63+ process:: exit ( 1 ) ;
64+ }
65+ ( None , false ) => {
3466 println ! ( "No app name provided. Capturing debug output from all processes." ) ;
3567 if let Err ( e) = capture_debug_output ( None ) {
3668 eprintln ! ( "Error capturing debug output: {}" , e) ;
0 commit comments