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

Function parse_subcommand

nodedb/src/ctl/dispatch.rs:55–151  ·  view source on GitHub ↗

Parse `args` (the tail of `std::env::args()` after the binary name) into a subcommand. Returns `Ok(None)` when the first arg doesn't look like a subcommand (i.e. it's a config-file path and the caller should fall through to the server bootstrap path).

(args: &[String])

Source from the content-addressed store, hash-verified

53/// look like a subcommand (i.e. it's a config-file path and the
54/// caller should fall through to the server bootstrap path).
55pub fn parse_subcommand(args: &[String]) -> Result<Option<Subcommand>, String> {
56 let Some(first) = args.first() else {
57 return Ok(None);
58 };
59 // Known subcommand names — anything else is assumed to be a config
60 // file path so the `nodedb /etc/nodedb.toml` spelling keeps working.
61 let name = first.as_str();
62 if !matches!(
63 name,
64 "regen-certs"
65 | "rotate-ca"
66 | "join-token"
67 | "healthcheck"
68 | "help"
69 | "--help"
70 | "-h"
71 | "--version"
72 | "-V"
73 | "version"
74 | "migrate"
75 | "backup"
76 | "restore"
77 | "verify"
78 | "repair"
79 | "dump"
80 | "fsck"
81 ) {
82 return Ok(None);
83 }
84
85 if matches!(name, "help" | "--help" | "-h") {
86 print_usage();
87 std::process::exit(0);
88 }
89
90 if matches!(name, "--version" | "-V" | "version") {
91 return Ok(Some(Subcommand::PrintVersion));
92 }
93
94 if matches!(
95 name,
96 "migrate" | "backup" | "restore" | "verify" | "repair" | "dump" | "fsck"
97 ) {
98 return Ok(Some(Subcommand::NotImplemented {
99 name: name.to_string(),
100 }));
101 }
102
103 let tail = &args[1..];
104 match name {
105 "regen-certs" => {
106 let flags = parse_flags(tail)?;
107 let data_dir = PathBuf::from(super::args::required(&flags, "data-dir")?);
108 let node_id: u64 = super::args::required(&flags, "node-id")?
109 .parse()
110 .map_err(|_| "--node-id must be an integer".to_string())?;
111 Ok(Some(Subcommand::RegenCerts { data_dir, node_id }))
112 }

Callers 5

mainFunction · 0.85
regen_certs_parsesFunction · 0.85
rotate_ca_stage_parsesFunction · 0.85
join_token_parsesFunction · 0.85

Calls 11

print_usageFunction · 0.85
parse_flagsFunction · 0.85
requiredFunction · 0.85
parse_portFunction · 0.85
parse_durationFunction · 0.85
firstMethod · 0.80
to_stringMethod · 0.80
as_strMethod · 0.45
parseMethod · 0.45
cloneMethod · 0.45
getMethod · 0.45

Tested by 4

regen_certs_parsesFunction · 0.68
rotate_ca_stage_parsesFunction · 0.68
join_token_parsesFunction · 0.68