command line argument parsing for recv
This commit is contained in:
parent
cb08126abd
commit
7855dde91e
|
@ -1,5 +1,28 @@
|
|||
extern crate hptp;
|
||||
|
||||
fn main() {
|
||||
println!("{}", hptp::RECV_GREETING);
|
||||
#[derive(Debug)]
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue