Add stdin reading capabilities.
This commit is contained in:
parent
eb582e2f3d
commit
14e614edd9
1 changed files with 14 additions and 7 deletions
15
src/main.rs
15
src/main.rs
|
@ -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(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue