Skip to content

Commit

Permalink
Add flag parsing and address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
andreastt committed Oct 22, 2014
1 parent 592e934 commit 8f86342
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
/target /target
.version
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -3,6 +3,7 @@
name = "wires" name = "wires"
version = "0.0.1" version = "0.0.1"
authors = ["James Graham <james@hoppipolla.co.uk>"] authors = ["James Graham <james@hoppipolla.co.uk>"]
build = "make"


[dependencies.hyper] [dependencies.hyper]
git = "https://github.com/hyperium/hyper.git" git = "https://github.com/hyperium/hyper.git"
Expand Down
8 changes: 8 additions & 0 deletions Makefile
@@ -0,0 +1,8 @@
all: .version

.version: Cargo.lock
grep -E -m1 '^version\s*=\s*"[^"]*"$$' Cargo.lock | grep -Eo '[0-9\.]+' | tr -d '\n' > $@

.PHONY = clean
clean:
rm -f .version
87 changes: 83 additions & 4 deletions src/main.rs
@@ -1,20 +1,99 @@
#![feature(slicing_syntax)] #![feature(slicing_syntax)]

extern crate getopts;
extern crate hyper; extern crate hyper;
extern crate regex;
extern crate serialize; extern crate serialize;
extern crate uuid; extern crate uuid;
extern crate regex;


use std::io::net::ip::Ipv4Addr; use getopts::{usage,optflag, getopts, OptGroup};
use httpserver::start; use httpserver::start;
use std::fmt;
use std::io::net::ip::SocketAddr;
use std::io;
use std::os;


mod common;
mod command; mod command;
mod common;
mod httpserver; mod httpserver;
mod marionette; mod marionette;
mod messagebuilder; mod messagebuilder;
mod response; mod response;


static DEFAULT_ADDR: &'static str = "127.0.0.1:4444";
static VERSION: &'static str = include_str!("../.version");

fn err(msg: String) {
let prog = os::args()[0].clone();
io::stderr().write_line(format!("{}: error: {}", prog, msg).as_slice()).unwrap();
}

fn print_usage(opts: &[OptGroup]) {
let prog = os::args()[0].clone();
let shorts: Vec<_> = opts.iter().map(|opt| opt.short_name.clone()).collect();
let msg = format!("usage: {} [-{}] [ADDRESS]", prog, shorts.as_slice().concat());
io::stderr().write_line(usage(msg.as_slice(), opts).as_slice()).unwrap();
}

// Valid addresses to parse are "HOST:PORT" or ":PORT".
// If the host isn't specified, 127.0.0.1 will be assumed.
fn parse_addr(s: String) -> Result<SocketAddr, String> {
let mut parts: Vec<&str> = s.as_slice().splitn(1, ':').collect();
if parts.len() == 2 {
parts[0] = "127.0.0.1";
}
let full_addr = parts.connect(":");
match from_str::<SocketAddr>(full_addr.as_slice()) {
Some(addr) => Ok(addr),
None => Err(format!("illegal address: {}", s))
}
}

fn run(args: Vec<String>) -> int {
let opts = [
optflag("q", "", "make the program quiet, only printing warnings"),
optflag("v", "", "show version information"),
optflag("h", "", "show this message"),
];
let matches = match getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
err(format!("{}", f));
return 0;
}
};

if matches.opt_present("v") {
println!("wires version {}", VERSION);
return 0;
} else if matches.opt_present("h") {
print_usage(opts);
return 127;
}

let addr_str = if matches.free.len() == 1 {
matches.free[0].clone()
} else if matches.free.len() > 1 {
err(format!("got {} positional arguments, expected 1", matches.free.len()));
print_usage(opts);
return 1;
} else {
DEFAULT_ADDR.to_string()
};
let addr = match parse_addr(addr_str) {
Ok(x) => x,
Err(e) => {
err(format!("{}", e));
return 1;
}
};

start(addr.ip, addr.port);
return 0;
}


fn main() { fn main() {
start(Ipv4Addr(127, 0, 0, 1), 1337); let args = os::args();
let s = run(args);
os::set_exit_status(s);
} }

0 comments on commit 8f86342

Please sign in to comment.