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