cli port option and parsing to config

This commit is contained in:
debris 2016-09-30 16:25:28 +02:00
parent 0f73927e51
commit 5aacf6ea38
3 changed files with 32 additions and 7 deletions

View File

@ -16,6 +16,7 @@ pub struct Connection<A> where A: io::Read + io::Write {
pub struct Config {
magic: Magic,
port: u16,
services: ServiceFlags,
user_agent: String,
start_height: i32,

View File

@ -18,4 +18,9 @@ args:
value_name: IP
help: Connect to a node to retrieve peer addresses, and disconnect
takes_value: true
- port:
long: port
value_name: PORT
help: Listen for connections on PORT
takes_value: true

View File

@ -16,17 +16,36 @@ use net::common::Magic;
fn main() {
let yaml = load_yaml!("cli.yml");
let matches = clap::App::from_yaml(yaml).get_matches();
let _cfg = parse(&matches);
}
let _magic = match matches.is_present("testnet") {
struct Config<'a> {
magic: Magic,
port: u16,
connect: Option<&'a str>,
seednode: Option<&'a str>,
}
fn parse<'a>(matches: &'a clap::ArgMatches<'a>) -> Result<Config<'a>, String> {
let magic = match matches.is_present("testnet") {
true => Magic::Testnet,
false => Magic::Mainnet,
};
if let Some(ip) = matches.value_of("connect") {
println!("Connecting to ip: {}", ip);
}
let port = match matches.value_of("port") {
Some(port) => try!(port.parse().map_err(|_| "Invalid port".to_owned())),
None => match magic {
Magic::Mainnet => 8333,
Magic::Testnet => 18333,
},
};
if let Some(ip) = matches.value_of("seednode") {
println!("Seednode ip: {}", ip);
}
let config = Config {
magic: magic,
port: port,
connect: matches.value_of("connect"),
seednode: matches.value_of("seednode"),
};
Ok(config)
}