command line argument parsing for recv

This commit is contained in:
Milo Turner 2020-03-02 16:06:39 -05:00
parent cb08126abd
commit 7855dde91e
1 changed files with 25 additions and 2 deletions

View File

@ -1,5 +1,28 @@
extern crate hptp; extern crate hptp;
fn main() { #[derive(Debug)]
println!("{}", hptp::RECV_GREETING); struct Hostname(String, u16);
fn go(hn: Hostname) {
println!("{:?}", hn)
}
fn usage() {
print!("usage:\n./3700recv <host-ip>:<host-port>\n")
}
fn parse_args() -> Result<Hostname, ()> {
let mut args = std::env::args();
let arg = args.nth(1).ok_or(())?;
let i = arg.find(':').ok_or(())?;
let ip: String = arg[..i].into();
let port: u16 = arg[i + 1..].parse::<u16>().map_err(|_| ())?;
Ok(Hostname(ip, port))
}
fn main() {
match parse_args() {
Ok(x) => go(x),
Err(_) => usage(),
}
} }