From d1b77d652b18980f45e6a5a19d9a00e5b3b85f2b Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 13 Apr 2024 17:11:04 -0600 Subject: [PATCH] getopt.rs(3): returns last parsed option --- src/getopt.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/getopt.rs b/src/getopt.rs index 70ee801..c4f88ab 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -21,12 +21,13 @@ use std::ffi::{ c_int, c_char, CString, CStr }; pub struct Opt { pub arg: Option, pub ind: i32, - pub opt: i32, + /* opt is set to either the current option or the one that cause an error */ + pub opt: String, } pub enum OptError { - MissingArg, - UnknownOpt, + MissingArg(String), + UnknownOpt(String), } /* function signature */ @@ -51,7 +52,7 @@ impl GetOpt for Vec { let len = self.len() as c_int; unsafe { - let a = match getopt(len, argv_ptr, opts) { + match getopt(len, argv_ptr, opts) { /* From getopt(3p): * * The getopt() function shall return the next option character @@ -69,10 +70,10 @@ impl GetOpt for Vec { * Otherwise, getopt() shall return -1 when all command line * options are parsed. */ 58 => { /* numerical ASCII value for ':' */ - return Some(Err(OptError::MissingArg)); + Some(Err(OptError::MissingArg(optopt.to_string()))) }, 63 => { /* numerical ASCII value for '?' */ - return Some(Err(OptError::UnknownOpt)) + Some(Err(OptError::UnknownOpt(optopt.to_string()))) }, /* From getopt(3p): * @@ -88,10 +89,18 @@ impl GetOpt for Vec { * * getopt() shall return -1 after incrementing optind. */ -1 => return None, - _ => CStr::from_ptr(optarg).to_string_lossy().into_owned(), - }; + opt => { + let arg = CStr::from_ptr(optarg) + .to_string_lossy() + .into_owned(); - Some(Ok(Opt { arg: Some(a), ind: optind, opt: optopt })) + Some(Ok(Opt { + arg: Some(arg), + ind: optind, + opt: opt.to_string(), + })) + }, + } } } }