use anyhow::Result;
use clap::{
Arg,
ArgMatches,
Command,
};
use std::{
net::{
SocketAddr,
SocketAddrV4,
},
str::FromStr,
};
#[derive(Debug)]
pub struct ProgramArguments {
local: SocketAddr,
remote: SocketAddr,
}
impl ProgramArguments {
pub fn new(app_name: &'static str, app_author: &'static str, app_about: &'static str) -> Result<Self> {
let matches: ArgMatches = Command::new(app_name)
.author(app_author)
.about(app_about)
.arg(
Arg::new("local")
.long("local-address")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("ADDRESS:PORT")
.help("Sets the address of local socket"),
)
.arg(
Arg::new("remote")
.long("remote-address")
.value_parser(clap::value_parser!(String))
.required(true)
.value_name("ADDRESS:PORT")
.help("Sets the address of remote socket"),
)
.get_matches();
let local: SocketAddr = SocketAddr::V4({
let local: &String = matches.get_one::<String>("local").expect("missing address");
SocketAddrV4::from_str(local)?
});
let remote: SocketAddr = SocketAddr::V4({
let remote: &String = matches.get_one::<String>("remote").expect("missing address");
SocketAddrV4::from_str(remote)?
});
Ok(Self { local, remote })
}
pub fn local(&self) -> SocketAddr {
self.local
}
pub fn remote(&self) -> SocketAddr {
self.remote
}
}