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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
use std::env::{args}; use std::collections::HashMap; /** Parse the command line arguments for key / value pairs and present them as `HashMap<String, String>` The following argument formats are recognised: ```text --key value -key value --key=value -key=value key=value ``` # Example ``` for (key, val) in driscoll::io::get_args() { println!("Found this parameter: {} = {}", key, val); } ``` */ pub fn get_args() -> HashMap<String, String> { // Create a hash map to store key => val sets of arguments let mut my_args: HashMap<String, String> = HashMap::new(); // Store the key of a parameter as we work through the arguments let mut key: String = "".to_string(); for argument in args() { // Grab the current argument let arg = argument.to_string(); // If this is a key=val argument, just add this to the hash map if arg.contains("=") { let string_arg: String = argument.to_string(); let arg_vector: Vec<&str> = string_arg.split("=").collect(); // Add this argument to the hashmap my_args.insert(str::replace(arg_vector[0],"-","").to_string(), arg_vector[1].to_string()); // Because this was an argument by itself, wipe any previously stored key key = "".to_string(); continue; } // If this contains the dash character, assume it's an argument key if arg.contains("-") { key = arg; continue } // If we have stored a key already, this must be the value if key.len() > 0 { my_args.insert(str::replace(key.as_str(),"-",""), arg); key = "".to_string(); } } return my_args; } /** Parse the command line arguments for key / value pairs and return one if it has the supplied key The following argument formats are recognised: ```text --key value -key value --key=value -key=value key=value ``` # Example ``` match driscoll::io::get_arg("test".to_string()) { Ok(content) => println!("Found: {}", content), Err(_) => println!("Not found") } ``` */ pub fn get_arg(key: String) -> Result<String, ()> { // Start by getting the arguments let arguments = get_args(); // Check if the argument we want is in the hash map match arguments.get(key.as_str()) { Some(param) => return Ok(param.clone()), None => return Err(()) } } /** Parse the command line arguments for key / value pairs and check if one with the supplied key exists The following argument formats are recognised: ```text --key value -key value --key=value -key=value key=value ``` # Example ``` // Match against boolean result match driscoll::io::arg_exists("test".to_string()) { true => println!("key exists"), false => println!("key does not exist") } // Simple boolean test if driscoll::io::arg_exists("test".to_string()) { // Key exists } ``` */ pub fn arg_exists(arg: String) -> bool { match get_arg(arg) { Ok(_) => true, Err(_) => false } }