Parse a list of `--key [value]` arguments into a map. Each `--flag` is consumed as: - **key-value** when the next token exists and does not start with `--` - **boolean** (stored with value `""`) when the next token is absent or starts with `--` Repeated flags overwrite. Returns an error for unrecognised tokens (positional args, single-dash shortflags) so operators get a clear message instead of
(args: &[String])
| 26 | /// (positional args, single-dash shortflags) so operators get a clear |
| 27 | /// message instead of silent ignore. |
| 28 | pub fn parse_flags(args: &[String]) -> Result<HashMap<String, String>, String> { |
| 29 | let mut map = HashMap::new(); |
| 30 | let mut i = 0; |
| 31 | while i < args.len() { |
| 32 | let a = &args[i]; |
| 33 | if let Some(name) = a.strip_prefix("--") { |
| 34 | let next = args.get(i + 1); |
| 35 | let is_value = next.is_some_and(|n| !n.starts_with("--")); |
| 36 | if is_value { |
| 37 | map.insert(name.to_string(), next.unwrap().clone()); |
| 38 | i += 2; |
| 39 | } else { |
| 40 | // Boolean flag — no value token follows. |
| 41 | map.insert(name.to_string(), String::new()); |
| 42 | i += 1; |
| 43 | } |
| 44 | } else { |
| 45 | return Err(format!("unexpected argument: {a}")); |
| 46 | } |
| 47 | } |
| 48 | Ok(map) |
| 49 | } |
| 50 | |
| 51 | /// Return `true` if a boolean (value-less) flag is present in `args`. |
| 52 | /// |