From d1ebe941d45539a8844044f2dc7c6fb7de7c72cd Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 23:33:10 +0100 Subject: [PATCH 1/4] add flag detections --- 01_phase_rust_src/sv/src/main.rs | 45 +++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index 2304b2d..6278c01 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -1,8 +1,8 @@ -//mod misc; mod parser; use std::env; +use std::process::exit; fn main() { @@ -15,7 +15,7 @@ fn main() { Err(..) => "/run/runit/service".to_string(), }; - let svwait_var = match svwait_env { + let mut svwait_var = match svwait_env { Ok(env) => match env.parse::() { Ok(var) => var, Err(..) => 7, @@ -23,6 +23,8 @@ fn main() { Err(..) => 7, }; + let mut verbose = 0; + dbg!(svdir_var); dbg!(svwait_var); @@ -32,12 +34,43 @@ fn main() { let arg_parser: Vec = parser::parse_args(args); - dbg!(arg_parser); + //dbg!(arg_parser); + + + + let mut i = 0; + + for val in arg_parser.iter() { + if i == 0 { + if val == "-v" { + verbose = 1; + dbg!(verbose); + } + else if val == "-w" { + i = 3; + // take next parametter before returning to i = 0. + } + else if val == "--" { + i += 1; + } + else { + parser::misc::usage(); + exit(100); + } + } + // TODO: Set the command if it is given + // TODO: Exec the command on all given services. + else if i == 3 { + svwait_var = match val.parse::() { + Ok(val) => val, + Err(..) => svwait_var, + }; + dbg!(svwait_var); + i = 0; + } + } - // TODO: Interpret the flags given if some are given. - // TODO: Set the command if it is given - // TODO: Exec the command on all given services. -- 2.45.2 From f12f786f460b58633cf77652adadacd31b4fbd80 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 23:33:31 +0100 Subject: [PATCH 2/4] add misc as public parser module --- 01_phase_rust_src/sv/src/parser.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/01_phase_rust_src/sv/src/parser.rs b/01_phase_rust_src/sv/src/parser.rs index aa2a608..ce1cdce 100644 --- a/01_phase_rust_src/sv/src/parser.rs +++ b/01_phase_rust_src/sv/src/parser.rs @@ -1,6 +1,6 @@ -mod misc; +pub mod misc; use std::process::exit; @@ -18,6 +18,7 @@ pub fn parse_args(args: Vec) -> Vec{ tmpstr = parse_args_1(arg); if tmpstr == "--" { + parser.push("--".to_string()); i += 1; } if tmpstr == "-w" { @@ -30,6 +31,7 @@ pub fn parse_args(args: Vec) -> Vec{ } else if tmpstr == "" { // We are actually in phase II. Do exactly the same if it was phase II. + parser.push("--".to_string()); parser.push(phase2(arg)); i = 3; } -- 2.45.2 From f3a03ab4d196500dd904188ca15dcd201072e4a0 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 23:41:24 +0100 Subject: [PATCH 3/4] set the command --- 01_phase_rust_src/sv/src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index 6278c01..eeac18b 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -39,6 +39,7 @@ fn main() { let mut i = 0; + let mut command; for val in arg_parser.iter() { if i == 0 { @@ -58,7 +59,11 @@ fn main() { exit(100); } } - // TODO: Set the command if it is given + else if i == 1 { + command = val; + dbg!(command); + i += 1; + } // TODO: Exec the command on all given services. else if i == 3 { svwait_var = match val.parse::() { -- 2.45.2 From 89efeec505b7eb687c83275a76d0cb128e7e1580 Mon Sep 17 00:00:00 2001 From: primardj Date: Fri, 5 Apr 2024 23:46:50 +0100 Subject: [PATCH 4/4] if flag not known, print a proper message jaaj --- 01_phase_rust_src/sv/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index eeac18b..8f722c3 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -55,6 +55,10 @@ fn main() { i += 1; } else { + if val.len() == 2 { + println!("sv: illegal option -- {}", val.chars().nth(1).unwrap() ); + // TODO: thread NOT pannic if it is not an ASCII character. + } parser::misc::usage(); exit(100); } -- 2.45.2