1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73


use std::collections::HashMap;
use colored::*;

///
///     #Example
///     
/// 
///     let Unconditionals = vec!["--Utestflag1","--Utestflag2"];
///     let Conditionals = vec!["-utestflag3"];
///     let args = parse(Conditionals, Unconditionals).expect("Arguments not properly formatted"); //Will return an error if unconditional arguments not given or input arguments not properly formatted.
///     let conds = args.0; // hashmap of conditionals as keys and data as values
///     let unconds = args.1; // same for unconditionals
/// 


pub fn parse<'a>(conditionals:Vec::<&str>,unconditionals:Vec::<&str>) -> Result<(HashMap<String,String>,HashMap<String,String>),String>{
    use std::env;
    let mut conditionals_map: HashMap<String,String> = HashMap::with_capacity(conditionals.len());
    let mut unconditionals_map: HashMap<String,String> = HashMap::with_capacity(unconditionals.len());

    for i in conditionals {
        conditionals_map.insert(i.to_string(),"".to_string());
    }
    for i in &unconditionals {
        unconditionals_map.insert(i.to_string(),"".to_string());
    }

    let args = env::args().collect::<Vec<String>>();

    for (n,i) in args.iter().enumerate() {
        if conditionals_map.contains_key(i) {
            if args.len() < n + 1 {
                println!("args not properly formated");
                return Err(String::from("Results not properly formatted"));
            }
            conditionals_map.insert(i.to_string(),args[n+1].clone());

        }
    }

    for (n,i) in args.iter().enumerate() {
        if unconditionals_map.contains_key(i) {
            if args.len() < n + 1 {
                println!("args not properly formated");
                return Err(String::from("Results not properly formatted"));
            }
           unconditionals_map.insert(i.to_string(),args[n+1].clone());

        }

    }
    
    for i in &unconditionals {
        if unconditionals_map.get_key_value(*i).unwrap().1 == ""{
            let error = format!("Mandatory flag {} not included",i.blue());
            print!("\n \n");
            print!("{}","mandatory flags ".red());
            for i in unconditionals {
                print!("{} ",i.green());
            }
            print!("{}"," were not included \n \n".red());
            return Err(error);
        }
    }

    

    let to_return = Ok((conditionals_map,unconditionals_map));
    return to_return;
}