Handle CLI arguments as structured argument-parameter pairs

This commit is contained in:
Juno Takano 2025-12-14 15:08:55 -03:00
commit 2040854e82
7 changed files with 83 additions and 15 deletions

63
src/syntax/arguments.rs Normal file
View file

@ -0,0 +1,63 @@
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct Arguments {
pub hostname: String,
pub port: u16,
pub graph_path: PathBuf,
}
impl Arguments {
pub fn make_address(&self) -> String {
format!("{}:{}", self.hostname, self.port)
}
pub fn new() -> Arguments {
Arguments {
hostname: String::from("0.0.0.0"),
port: 0,
graph_path: PathBuf::from("./static/graph.toml"),
}
}
pub fn parse(&self) -> Arguments {
let args: Vec<String> = std::env::args().collect();
parse(self, &args)
}
}
fn parse(defaults: &Arguments, args: &[String]) -> Arguments {
let mut out_args = defaults.clone();
let filtered_args = if let Some((head, tail)) = args.split_first() {
if head.starts_with('-') { args } else { tail }
} else {
args
};
for arg in filtered_args.chunks(2) {
if let Some(argument) = arg.first()
&& let Some(parameter) = arg.last()
{
if argument.eq("-h") || argument.eq("--hostname") {
out_args.hostname = String::from(parameter);
} else if argument.eq("-p") || argument.eq("--port") {
out_args.port = parameter.parse().unwrap_or(out_args.port);
} else if argument.eq("-g") || argument.eq("--graph") {
out_args.graph_path = PathBuf::from(parameter);
} else {
crate::dev::log(
&parse,
&format!("Dropped unrecognized argument {argument}"),
);
}
} else {
crate::dev::log(
&parse,
"Dropped: Couldn't pair either one of or
both argument \"{argument}\", parameter \"{parameter}\"",
);
}
}
out_args
}