variables : possibilité de récupérer les arguments de la ligne de commande

This commit is contained in:
Ahurac 2024-04-17 16:08:29 +02:00
parent a441fc9cb3
commit 9d53bdba0c

View file

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::env;
use std::env::{self, args};
pub struct Variables {
variables: HashMap<String, String>,
@ -13,21 +13,21 @@ impl Variables {
}
pub fn get(&self, key: &str) -> String {
let var_from_map = self.variables.get(key);
let arg_index = String::from(key).parse::<usize>();
let value: String;
if var_from_map.is_none() {
let var_in_env = env::var(key);
value = match var_in_env {
Ok(value) => value,
Err(_e) => String::new(),
}
} else {
value = var_from_map.unwrap().clone();
match arg_index {
Ok(index) => match env::args().nth(index) {
Some(value) => value,
None => String::new(),
},
Err(_e) => match self.variables.get(key) {
Some(value) => value.clone(),
None => match env::var(key) {
Ok(value) => value,
Err(_e) => String::new(),
},
},
}
value
}
pub fn unset(&mut self, key: &str) {