generated from maddoxwerts/Dockerized-Rust
69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
// Libraries
|
|
use std::env;
|
|
|
|
// Constants
|
|
|
|
// Structures
|
|
pub struct Argument {
|
|
pub key: String,
|
|
pub val: String,
|
|
}
|
|
pub struct Arguments {
|
|
arguments: Vec<Argument>,
|
|
}
|
|
|
|
// Implementations
|
|
impl Arguments {
|
|
// Constructor
|
|
pub fn new() -> Self {
|
|
// Result
|
|
let mut result: Vec<Argument> = Vec::new();
|
|
|
|
// Getting Arguments
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
// Going through all arguments
|
|
for i in 0..args.len() {
|
|
// Checking if it's an argument
|
|
if !args[i].starts_with("--") {
|
|
continue;
|
|
}
|
|
|
|
// Figuring out what kind of variable it is
|
|
if args[i + 1].starts_with("--") {
|
|
result.push(Argument {
|
|
key: args[i].clone(),
|
|
val: String::new(),
|
|
});
|
|
} else {
|
|
result.push(Argument {
|
|
key: args[i].clone(),
|
|
val: args[i + 1].clone(),
|
|
});
|
|
}
|
|
}
|
|
|
|
// Return Result
|
|
Self { arguments: result }
|
|
}
|
|
|
|
// Functions
|
|
pub fn find(&self, key: &str) -> Option<&Argument> {
|
|
// Storing Result
|
|
let mut result: Option<&Argument> = None;
|
|
|
|
// Looping through arguments to find the one we need
|
|
for arg in &self.arguments {
|
|
// Comparing the keys
|
|
if key != arg.key {
|
|
continue;
|
|
}
|
|
|
|
// This is it!
|
|
result = Some(arg);
|
|
}
|
|
|
|
// Could not find it...
|
|
result
|
|
}
|
|
}
|