2022-07-09 08:33:19 +00:00
|
|
|
use std::collections::HashMap;
|
2022-07-09 13:53:01 +00:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
2022-07-09 08:33:19 +00:00
|
|
|
|
2022-07-12 08:30:15 +00:00
|
|
|
pub struct ArgsResult {
|
|
|
|
pub map: HashMap<String, String>,
|
|
|
|
pub regex_cmds: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_args() -> ArgsResult {
|
2022-07-09 08:33:19 +00:00
|
|
|
let mut map = HashMap::new();
|
2022-07-12 08:30:15 +00:00
|
|
|
let mut regex_cmds = Vec::new();
|
2022-07-09 08:33:19 +00:00
|
|
|
|
2022-07-10 08:02:54 +00:00
|
|
|
let mut first = true;
|
2022-07-09 08:33:19 +00:00
|
|
|
for arg in std::env::args() {
|
2022-07-10 08:02:54 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
} else if arg.starts_with("--netdev=") {
|
2022-07-09 08:33:19 +00:00
|
|
|
let (_, back) = arg.split_at(9);
|
|
|
|
map.insert("netdev".into(), back.into());
|
2022-07-09 09:18:12 +00:00
|
|
|
} else if arg.starts_with("--interval-sec=") {
|
|
|
|
let (_, back) = arg.split_at(15);
|
|
|
|
map.insert("interval-sec".into(), back.into());
|
2022-07-26 12:12:29 +00:00
|
|
|
} else if arg == "--acpi-builtin" {
|
|
|
|
map.insert("acpi-builtin".into(), String::new());
|
2022-07-12 08:30:15 +00:00
|
|
|
} else if arg.starts_with("--regex-cmd=") {
|
|
|
|
let (_, back) = arg.split_at(12);
|
|
|
|
regex_cmds.push(back.to_owned());
|
2022-08-11 02:47:49 +00:00
|
|
|
} else if arg.starts_with("--time-format=") {
|
|
|
|
let (_, back) = arg.split_at(14);
|
|
|
|
map.insert("time-format".into(), back.to_owned());
|
2022-07-10 08:47:51 +00:00
|
|
|
} else if arg == "--help" || arg == "-h" {
|
2022-07-09 09:18:12 +00:00
|
|
|
map.insert("help".into(), "".into());
|
2022-07-10 08:02:54 +00:00
|
|
|
} else {
|
|
|
|
let mut stderr_handle = io::stderr().lock();
|
|
|
|
stderr_handle
|
|
|
|
.write_all(format!("WARNING: Got invalid arg \"{}\"!\n", arg).as_bytes())
|
|
|
|
.ok();
|
2022-07-09 08:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 08:30:15 +00:00
|
|
|
ArgsResult { map, regex_cmds }
|
2022-07-09 08:33:19 +00:00
|
|
|
}
|
2022-07-09 09:18:12 +00:00
|
|
|
|
|
|
|
pub fn print_usage() {
|
2022-07-09 13:53:01 +00:00
|
|
|
let mut stderr_handle = io::stderr().lock();
|
|
|
|
stderr_handle.write_all(b"Usage:\n").ok();
|
2022-07-10 08:47:51 +00:00
|
|
|
stderr_handle
|
2022-07-12 08:53:17 +00:00
|
|
|
.write_all(b" -h | --help\t\t\t\t\t\tPrints help\n")
|
2022-07-12 08:30:15 +00:00
|
|
|
.ok();
|
|
|
|
stderr_handle
|
2022-07-12 08:53:17 +00:00
|
|
|
.write_all(b" --netdev=<device_name>\t\t\t\tCheck network traffic on specified device\n")
|
2022-07-10 08:47:51 +00:00
|
|
|
.ok();
|
2022-07-09 13:53:01 +00:00
|
|
|
stderr_handle
|
2022-07-12 08:53:17 +00:00
|
|
|
.write_all(
|
|
|
|
b" --interval-sec=<seconds>\t\t\t\tOutput at intervals of <seconds> (default 5)\n",
|
|
|
|
)
|
2022-07-09 13:53:01 +00:00
|
|
|
.ok();
|
2022-07-26 12:12:29 +00:00
|
|
|
stderr_handle
|
|
|
|
.write_all(
|
2022-07-26 12:18:59 +00:00
|
|
|
b" --acpi-builtin\t\t\t\t\tUse \"acpi -b\" built-in fetching (battery info, with color)\n",
|
2022-07-26 12:12:29 +00:00
|
|
|
)
|
|
|
|
.ok();
|
2022-07-09 13:53:01 +00:00
|
|
|
stderr_handle
|
2022-07-12 08:30:15 +00:00
|
|
|
.write_all(
|
2022-07-12 08:53:17 +00:00
|
|
|
b" --regex-cmd=<cmd>[SPLIT]<args...>[SPLIT]<regex>\tUse an output of a command as a metric\n",
|
2022-07-12 08:30:15 +00:00
|
|
|
)
|
2022-07-09 13:53:01 +00:00
|
|
|
.ok();
|
2022-08-11 02:47:49 +00:00
|
|
|
stderr_handle
|
|
|
|
.write_all(
|
|
|
|
b" --date-format=<date format string>\t\t\tSet the format string for the date\n",
|
|
|
|
)
|
|
|
|
.ok();
|
2022-07-09 09:18:12 +00:00
|
|
|
}
|