MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / parse_flags

Function parse_flags

nodedb/src/ctl/args.rs:28–49  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

26/// (positional args, single-dash shortflags) so operators get a clear
27/// message instead of silent ignore.
28pub 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///

Callers 5

parse_subcommandFunction · 0.85
parse_portFunction · 0.85
parses_kv_flagsFunction · 0.85
lone_flag_is_booleanFunction · 0.85

Calls 5

to_stringMethod · 0.80
lenMethod · 0.45
getMethod · 0.45
insertMethod · 0.45
cloneMethod · 0.45

Tested by 3

parses_kv_flagsFunction · 0.68
lone_flag_is_booleanFunction · 0.68