use std::collections::HashMap;
pub fn parse_options(argv: &Vec<String>) -> Vec<String> {
let mut options: Vec<String> = Vec::new();
for arg in argv {
if arg.starts_with("-") {
if is_double_hyphen_option(&arg[..]) {
break;
}
if is_definition_option(&arg[..]) {
let option_definition_vec = get_definition_parts(&arg[..]);
let option = &option_definition_vec[0];
options.push(option.to_string());
} else {
options.push(arg.clone());
}
}
}
options
}
pub fn parse_definitions(argv: &Vec<String>) -> HashMap<String, String> {
let mut definitions: HashMap<String, String> = HashMap::new();
for arg in argv {
if arg.starts_with("-") {
if is_double_hyphen_option(&arg[..]) {
break;
}
if is_definition_option(&arg[..]) {
let option_definition_vec = get_definition_parts(&arg[..]);
let option = &option_definition_vec[0];
let definition = &option_definition_vec[1];
definitions.insert(option.to_string(), definition.to_string());
}
}
}
definitions
}
pub fn is_definition_option(needle: &str) -> bool {
match needle.contains("=") {
true => true,
false => false,
}
}
pub fn get_definition_parts(needle: &str) -> Vec<String> {
let opt_def: Vec<_> = needle.split('=').collect();
vec![String::from(opt_def[0]), String::from(opt_def[1])]
}
pub fn is_double_hyphen_option(needle: &str) -> bool {
match needle == "--" {
true => true,
false => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn function_parse_options() {
let test_vec = vec![
String::from("tester"),
String::from("subcommand"),
String::from("-o"),
String::from("spacedefinition"),
String::from("--longoption"),
String::from("--defoption=equaldefinition"),
String::from("--"),
String::from("--afterdoublehyphen"), String::from("-x"), String::from("lastpos"),
];
let expected_vec = vec![
String::from("-o"),
String::from("--longoption"),
String::from("--defoption"),
];
assert!(parse_options(&test_vec) == expected_vec);
}
#[test]
fn function_parse_options_no_options() {
let test_vec = vec![
String::from("tester"),
String::from("subcommand"),
String::from("spacedefinition"),
String::from("--"),
String::from("--afterdoublehyphen"), String::from("-x"), String::from("lastpos"),
];
let expected_vec: Vec<String> = vec![];
assert!(parse_options(&test_vec) == expected_vec);
}
#[test]
fn function_parse_definitions_single_def() {
let test_vec = vec![
String::from("tester"),
String::from("subcommand"),
String::from("-o"),
String::from("spacedefinition"),
String::from("--longoption"),
String::from("--defoption=equaldefinition"),
String::from("--"),
String::from("--output=filepath"), String::from("--afterdoublehyphen"), String::from("-x"), String::from("lastpos"),
];
let mut expected_hm = HashMap::new();
expected_hm.insert("--defoption".to_string(), "equaldefinition".to_string());
assert_eq!(parse_definitions(&test_vec), expected_hm);
}
#[test]
fn function_parse_definitions_multi_def() {
let test_vec = vec![
String::from("tester"),
String::from("subcommand"),
String::from("-o"),
String::from("spacedefinition"),
String::from("--longoption"),
String::from("--defoption=equaldefinition"),
String::from("--another=anotherdef"),
String::from("--"),
String::from("--output=filepath"), String::from("--afterdoublehyphen"), String::from("-x"), String::from("lastpos"),
];
let mut expected_hm = HashMap::new();
expected_hm.insert("--defoption".to_string(), "equaldefinition".to_string());
expected_hm.insert("--another".to_string(), "anotherdef".to_string());
assert_eq!(parse_definitions(&test_vec), expected_hm);
}
#[test]
fn function_is_definition_option_true() {
let true_defintion = "--option=definition";
assert!(is_definition_option(true_defintion) == true);
}
#[test]
fn function_is_definition_option_false() {
let false_definition = "--option";
assert!(is_definition_option(false_definition) == false);
}
#[test]
fn function_get_definition_parts() {
let definition_string = "--option=definition";
let expected: Vec<String> = vec![String::from("--option"), String::from("definition")];
assert!(get_definition_parts(definition_string) == expected);
}
#[test]
fn function_is_double_hyphen_option_true() {
let true_definition = "--";
assert_eq!(is_double_hyphen_option(true_definition), true);
}
#[test]
fn function_is_double_hyphen_option_false() {
let false_definition_1 = "--help";
let false_definition_2 = "-s";
let false_definition_3 = "subcmd";
assert_eq!(is_double_hyphen_option(false_definition_1), false);
assert_eq!(is_double_hyphen_option(false_definition_2), false);
assert_eq!(is_double_hyphen_option(false_definition_3), false);
}
}