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