forked from bonsai/harakit
getopt.rs(3): adds comments & documentation
This commit is contained in:
parent
8f990ba515
commit
e1bf49c75a
@ -40,9 +40,9 @@ pub enum OptError {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Opt {
|
||||
arg: Option<String>,
|
||||
ind: *mut i32,
|
||||
opt: Result<String, OptError>,
|
||||
arg: Option<String>, /* option argument */
|
||||
ind: *mut i32, /* option index */
|
||||
opt: Result<String, OptError>, /* option option */
|
||||
}
|
||||
|
||||
impl Opt {
|
||||
@ -53,6 +53,7 @@ impl Opt {
|
||||
default.to_string()
|
||||
}
|
||||
|
||||
/* makes matching the output of this method more bearable */
|
||||
pub fn opt(&self) -> Result<&str, OptError> {
|
||||
self.opt.as_ref().map(|o| o.as_str()).map_err(OptError::clone)
|
||||
}
|
||||
@ -68,6 +69,7 @@ impl Opt {
|
||||
* options have already been processed. */
|
||||
pub fn ind(&self) -> usize { unsafe { *self.ind as usize } }
|
||||
|
||||
/* this is patently terrible and is only happening because I’m stubborn */
|
||||
pub fn set_ind(&self, ind: i32) { unsafe { *self.ind = ind; } }
|
||||
}
|
||||
|
||||
@ -84,11 +86,11 @@ impl GetOpt for Vec<String> {
|
||||
.map(|x| CString::new(x).unwrap().into_raw())
|
||||
.collect();
|
||||
|
||||
/* god knows what this does */
|
||||
let boxed = Box::into_raw(c_strings.into_boxed_slice());
|
||||
let argv = boxed as *const *mut i8;
|
||||
|
||||
/* these operations must be separated out into separate operations so
|
||||
* the CStrings can live long enough */
|
||||
/* operations are separated out so that everything lives long enough */
|
||||
let opts = CString::new(optstring).unwrap().into_raw();
|
||||
let len = self.len() as c_int;
|
||||
|
||||
@ -110,21 +112,17 @@ impl GetOpt for Vec<String> {
|
||||
*
|
||||
* Otherwise, getopt() shall return -1 when all command line
|
||||
* options are parsed. */
|
||||
58 => { /* numerical ASCII value for ':' */
|
||||
58 => { /* ASCII ':' */
|
||||
Some(Opt {
|
||||
/* opt argument */
|
||||
arg: None,
|
||||
/* opt index */
|
||||
ind: std::ptr::addr_of_mut!(optind),
|
||||
/* error containing option */
|
||||
opt: Err(OptError::MissingArg(optopt.to_string())),
|
||||
})
|
||||
},
|
||||
63 => { /* numerical ASCII value for '?' */
|
||||
63 => { /* ASCII '?' */
|
||||
Some(Opt {
|
||||
/* opt argument */
|
||||
arg: None,
|
||||
/* opt index */
|
||||
ind: std::ptr::addr_of_mut!(optind),
|
||||
/* error containing option */
|
||||
opt: Err(OptError::UnknownOpt(optopt.to_string())),
|
||||
@ -155,21 +153,25 @@ impl GetOpt for Vec<String> {
|
||||
}
|
||||
|
||||
Some(Opt {
|
||||
arg, /* opt argument */
|
||||
ind: std::ptr::addr_of_mut!(optind), /* opt index */
|
||||
/* option itself */
|
||||
arg,
|
||||
ind: std::ptr::addr_of_mut!(optind),
|
||||
/* I didn’t need to cast this before; I rewrote the
|
||||
* pointer logic and now I do
|
||||
*
|
||||
* I don’t know why this is */
|
||||
opt: Ok((opt as u8 as char).to_string()),
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
/* delloc argv */
|
||||
/* delloc argv (something online said I should do this) */
|
||||
let _ = Box::from_raw(boxed);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* tests (good) */
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use GetOpt;
|
||||
|
Loading…
Reference in New Issue
Block a user