variables : possibilité de récupérer les arguments de la ligne de commande
This commit is contained in:
parent
a441fc9cb3
commit
9d53bdba0c
1 changed files with 14 additions and 14 deletions
|
@ -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 {
|
||||
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(),
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
value = var_from_map.unwrap().clone();
|
||||
}
|
||||
|
||||
value
|
||||
}
|
||||
|
||||
pub fn unset(&mut self, key: &str) {
|
||||
|
|
Loading…
Reference in a new issue