1
2
3#[cfg(test)]
4mod tests {
5 use std::env;
6
7 #[test]
8 fn it_works() {
9 let result = 2 + 2;
10 assert_eq!(result, 4);
11 }
12
13
14 #[test]
15 fn get_proc_name() {
16
17 let result = crate::minparse::process_name();
18 let args: Vec<String> = env::args().collect();
19 assert_eq!(result, args[0]);
20 }
21}
22
23pub mod minparse {
24
25 use std::{env, collections::HashMap};
26
27 pub fn process_name() -> String {
28 let args = env::args();
29 let args: Vec<String> = args.collect();
30 let first = &args[0];
31 return first.to_string();
32 }
33
34 pub fn subcommands() -> Vec<String>{
35 let mut subcommands: Vec<String> = vec![];
36 for i in env::args() {
37 if i.starts_with("--") {
38 break;
39 }
40 subcommands.push(i);
41 }
42 return subcommands;
43 }
44
45 pub fn switches() -> Vec<String> {
46 let mut switches: Vec<String> = vec![];
47 let args: Vec<String> = env::args().collect();
48 let mut c_index: usize = 0;
49 for i in &args {
50 if c_index == 0 {
51 c_index = 1;
52 continue;
53 }
54 if i.starts_with("--"){
55 if !(args.len() <= c_index+1) {
56 if args[c_index+1].starts_with("--") || c_index + 1 == args.len() {
57 switches.push(i.to_owned());
58 }
59 }
60 if args.len() == c_index +1 {
61 switches.push(i.to_owned());
62 }
63
64 }
65 c_index = c_index+1;
66 }
67 return switches;
68 }
69
70 pub fn fields() -> HashMap<String, String> {
71 let mut fields: HashMap<String, String> = HashMap::new();
72 let args: Vec<String> = env::args().collect();
73 let mut c_index: usize = 0;
74 for i in &args {
75 if c_index == 0 {
76 c_index = 1;
77 continue;
78 }
79 if i.starts_with("--"){
80 if !(args.len() <= c_index+1) {
81 if !args[c_index+1].starts_with("--"){
82 fields.insert((&i.to_owned()).to_owned(), (&args[c_index+1]).to_owned());
83 }
84 }
85 }
86 c_index = c_index+1;
87 }
88 return fields;
89 }
90}