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])
| 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). |
| 55 | pub 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 | } |