diff --git a/src/lib.rs b/src/lib.rs index c9664a8..cd0787a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,17 +127,23 @@ pub fn parse_stdin_with_reader(reader: &mut R) -> HashMap HashMap { - vars.iter() - .filter_map(|arg| { - let parts: Vec<&str> = arg.splitn(2, '=').collect(); - if parts.len() == 2 { - Some((parts[0].to_string(), parts[1].to_string())) - } else { - None - } - }) - .collect() +pub fn parse_args(vars: &[String]) -> Result, String> { + vars.iter().try_fold(HashMap::new(), |mut acc, arg| { + let parts: Vec<&str> = arg.splitn(2, '=').collect(); + if parts.len() == 2 { + let key = parser::key_parser() + .parse(parts[0]) + .map_err(|_| format!("Invalid key format in argument: {}", arg.bold().red()))?; + acc.insert(key, parts[1].to_string()); + Ok(acc) + } else { + Err(format!( + "Invalid argument format {}. Expected format is {}", + arg.bold().red(), + "KEY=value".bold() + )) + } + }) } pub fn parse_env_content(content: &str) -> HashMap { diff --git a/src/main.rs b/src/main.rs index 4be14ee..2ef674c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -200,7 +200,13 @@ fn main() { if !atty::is(Stream::Stdin) { parse_stdin() } else { - parse_args(&cli.vars) + match parse_args(&cli.vars) { + Ok(vars) => vars, + Err(e) => { + eprintln!("Error parsing arguments: {}", e); + process::exit(1); + } + } } } else { HashMap::new() diff --git a/src/parser.rs b/src/parser.rs index 9b1ac42..9d61924 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -12,6 +12,11 @@ pub enum Line { }, } +// Parser for keys +pub fn key_parser() -> impl Parser> + Clone { + text::ident().padded() +} + pub fn parser() -> impl Parser, Error = Simple> + Clone { // Parser for comments let comment = just('#') @@ -19,8 +24,8 @@ pub fn parser() -> impl Parser, Error = Simple> + Clone { .map(|(chars, _)| chars.into_iter().collect::()) .map(Line::Comment); - // Parser for keys - let key = text::ident().padded(); + + let key = key_parser(); // Parser for single-quoted values let single_quoted_value = just('\'')