Permalink
Browse files

Add flag parsing and address parsing

  • Loading branch information...
1 parent 592e934 commit 8f8634247726a259d23de2effdddcf497de13750 @andreastt andreastt committed Oct 21, 2014
Showing with 93 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 Cargo.toml
  3. +8 −0 Makefile
  4. +83 −4 src/main.rs
View
@@ -1 +1,2 @@
/target
+.version
View
@@ -3,6 +3,7 @@
name = "wires"
version = "0.0.1"
authors = ["James Graham <james@hoppipolla.co.uk>"]
+build = "make"
[dependencies.hyper]
git = "https://github.com/hyperium/hyper.git"
View
@@ -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
View
@@ -1,20 +1,99 @@
#![feature(slicing_syntax)]
+
+extern crate getopts;
extern crate hyper;
+extern crate regex;
extern crate serialize;
extern crate uuid;
-extern crate regex;
-use std::io::net::ip::Ipv4Addr;
+use getopts::{usage,optflag, getopts, OptGroup};
use httpserver::start;
+use std::fmt;
+use std::io::net::ip::SocketAddr;
+use std::io;
+use std::os;
-mod common;
mod command;
+mod common;
mod httpserver;
mod marionette;
mod messagebuilder;
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() {
- 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.