From 14c01eb796aeda5b5a493c61ab2bc37cf82a8090 Mon Sep 17 00:00:00 2001 From: primardj Date: Sat, 6 Apr 2024 11:48:48 +0100 Subject: [PATCH] =?UTF-8?q?solve=20panicking=20with=20flag=20=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01_phase_rust_src/sv/src/main.rs | 11 ++++++++--- 01_phase_rust_src/sv/src/parser.rs | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/01_phase_rust_src/sv/src/main.rs b/01_phase_rust_src/sv/src/main.rs index 8f722c3..3861aff 100644 --- a/01_phase_rust_src/sv/src/main.rs +++ b/01_phase_rust_src/sv/src/main.rs @@ -55,9 +55,14 @@ 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. + if val.len() >= 2 { + let mut j = 0; + for ch in val.chars() { + if j == 1 { + println!("sv: illegal option -- {}", ch ); + } + j += 1; + } } parser::misc::usage(); exit(100); diff --git a/01_phase_rust_src/sv/src/parser.rs b/01_phase_rust_src/sv/src/parser.rs index 5c9c18e..b7d62bc 100644 --- a/01_phase_rust_src/sv/src/parser.rs +++ b/01_phase_rust_src/sv/src/parser.rs @@ -52,6 +52,7 @@ pub fn parse_args(args: Vec) -> Vec{ } } + return parser; } @@ -79,12 +80,13 @@ pub fn parse_args_1(arg: &String) -> String{ //println!("{}", ch1); if ch1 == '-' && arg.len() >= 2{ - return (&arg[..2]).to_string() + let retour = &arg.chars().collect::>()[..2]; + return retour.into_iter().collect(); } else if ch1 == '-' { return ch1.to_string() } - println!("{}",arg); + //println!("{}",arg); return "".to_string()