Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flag parsing and address parsing
- Loading branch information
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,2 @@ | |||
/target | /target | ||
.version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | } |