#[cfg(feature = "posix")]
pub fn posix(arg: impl IntoIterator<Item=impl AsRef<str>>, optstring: impl AsRef<str>) -> Result<getopt> {
let options_map = build_options_map(validate_optstring(optstring.as_ref())?);
let mut opts = HashMap::<char,String>::new();
let mut args = Vec::<String>::new();
let mut next_opt: Option<char> = None;
let mut stop_parsing = false;
for element in arg {
let element = element.as_ref();
if stop_parsing {
args.push(element.to_string());
} else
{
if next_opt.is_some() {
opts.insert(next_opt.unwrap(), element.to_string());
next_opt = None;
} else {
if element.starts_with('-') && element.len() == 2 &&
validate_optchar(element.chars().nth(1).unwrap()).is_ok()
{
let opt = element.chars().nth(1).unwrap();
if let Some(&value) = options_map.get(&opt) {
if value == true {
next_opt = Some(opt);
} else {
opts.insert(opt,"".to_string());
}
} else {
opts.insert(opt,"".to_string());
}
} else {
args.push(element.to_string());
stop_parsing = true;
}
}
}
}
if let Some(o) = next_opt {
opts.insert(o, String::new());
}
Ok ( getopt {options:opts, arguments:args, option_has_arg:options_map} )
}
#[cfg(feature = "posix")]
fn validate_optchar(optchar: char) -> Result<char> {
match optchar {
'a'..='z' => Ok(optchar),
'A'..='Z' => Ok(optchar),
'0'..='9' => Ok(optchar),
'?' => Ok(optchar),
_ => Err(Error::new(ErrorKind::InvalidInput, "unsupported option character"))
}
}
#[cfg(test)]
#[cfg(feature = "posix")]
#[path = "getopt_posix_tests.rs"]
mod posix;
#[cfg(test)]
#[cfg(feature = "posix")]
#[path = "getopt_posix_helpers_tests.rs"]
mod posix_helpers;