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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Opt {
|
pub struct Opt {
|
||||||
arg: Option<String>,
|
arg: Option<String>, /* option argument */
|
||||||
ind: *mut i32,
|
ind: *mut i32, /* option index */
|
||||||
opt: Result<String, OptError>,
|
opt: Result<String, OptError>, /* option option */
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Opt {
|
impl Opt {
|
||||||
@ -53,6 +53,7 @@ impl Opt {
|
|||||||
default.to_string()
|
default.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* makes matching the output of this method more bearable */
|
||||||
pub fn opt(&self) -> Result<&str, OptError> {
|
pub fn opt(&self) -> Result<&str, OptError> {
|
||||||
self.opt.as_ref().map(|o| o.as_str()).map_err(OptError::clone)
|
self.opt.as_ref().map(|o| o.as_str()).map_err(OptError::clone)
|
||||||
}
|
}
|
||||||
@ -68,6 +69,7 @@ impl Opt {
|
|||||||
* options have already been processed. */
|
* options have already been processed. */
|
||||||
pub fn ind(&self) -> usize { unsafe { *self.ind as usize } }
|
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; } }
|
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())
|
.map(|x| CString::new(x).unwrap().into_raw())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
/* god knows what this does */
|
||||||
let boxed = Box::into_raw(c_strings.into_boxed_slice());
|
let boxed = Box::into_raw(c_strings.into_boxed_slice());
|
||||||
let argv = boxed as *const *mut i8;
|
let argv = boxed as *const *mut i8;
|
||||||
|
|
||||||
/* these operations must be separated out into separate operations so
|
/* operations are separated out so that everything lives long enough */
|
||||||
* the CStrings can live long enough */
|
|
||||||
let opts = CString::new(optstring).unwrap().into_raw();
|
let opts = CString::new(optstring).unwrap().into_raw();
|
||||||
let len = self.len() as c_int;
|
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
|
* Otherwise, getopt() shall return -1 when all command line
|
||||||
* options are parsed. */
|
* options are parsed. */
|
||||||
58 => { /* numerical ASCII value for ':' */
|
58 => { /* ASCII ':' */
|
||||||
Some(Opt {
|
Some(Opt {
|
||||||
/* opt argument */
|
|
||||||
arg: None,
|
arg: None,
|
||||||
/* opt index */
|
|
||||||
ind: std::ptr::addr_of_mut!(optind),
|
ind: std::ptr::addr_of_mut!(optind),
|
||||||
/* error containing option */
|
/* error containing option */
|
||||||
opt: Err(OptError::MissingArg(optopt.to_string())),
|
opt: Err(OptError::MissingArg(optopt.to_string())),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
63 => { /* numerical ASCII value for '?' */
|
63 => { /* ASCII '?' */
|
||||||
Some(Opt {
|
Some(Opt {
|
||||||
/* opt argument */
|
|
||||||
arg: None,
|
arg: None,
|
||||||
/* opt index */
|
|
||||||
ind: std::ptr::addr_of_mut!(optind),
|
ind: std::ptr::addr_of_mut!(optind),
|
||||||
/* error containing option */
|
/* error containing option */
|
||||||
opt: Err(OptError::UnknownOpt(optopt.to_string())),
|
opt: Err(OptError::UnknownOpt(optopt.to_string())),
|
||||||
@ -155,21 +153,25 @@ impl GetOpt for Vec<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Some(Opt {
|
Some(Opt {
|
||||||
arg, /* opt argument */
|
arg,
|
||||||
ind: std::ptr::addr_of_mut!(optind), /* opt index */
|
ind: std::ptr::addr_of_mut!(optind),
|
||||||
/* option itself */
|
/* 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()),
|
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);
|
let _ = Box::from_raw(boxed);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* tests (good) */
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use GetOpt;
|
use GetOpt;
|
||||||
|
Loading…
Reference in New Issue
Block a user