Add stdin reading capabilities.

This commit is contained in:
femsci 2022-12-28 17:19:20 +01:00
parent eb582e2f3d
commit 14e614edd9
Signed by: femsci
GPG key ID: 08F7911F0E650C67

View file

@ -1,12 +1,19 @@
use std::io::Read;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Missing arguments: <IP>.");
std::process::exit(1);
}
let ip_string = &args[1];
let mut input_buf = String::new();
let ip_string = if args.len() < 2 || args[1] == "-" {
if std::io::stdin().read_to_string(&mut input_buf).is_ok() {
&input_buf
} else {
eprintln!("Missing arguments: <IP>.");
std::process::exit(1);
}
} else {
&args[1]
};
println!(
"Result: \n{}",
@ -14,7 +21,7 @@ fn main() {
Ok(x) => {
int_to_ip(x)
}
Err(_) => ip_to_int(ip_string).to_string(),
Err(_) => ip_to_int(ip_string.trim()).to_string(),
}
);
}