diff options
-rw-r--r-- | src/bin/mt-webface.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/bin/mt-webface.rs b/src/bin/mt-webface.rs index 0940ea0..6809f57 100644 --- a/src/bin/mt-webface.rs +++ b/src/bin/mt-webface.rs @@ -1,9 +1,13 @@ +extern crate getopts; extern crate pencil; #[macro_use] extern crate log; extern crate env_logger; +use std::env; use std::collections::BTreeMap; +use std::process::exit; +use getopts::Options; use pencil::Pencil; use pencil::{Request, PencilResult, Response, HTTPError}; @@ -43,8 +47,40 @@ fn server_error(_: HTTPError) -> PencilResult { Ok(response) } +fn print_usage(opts: Options) { + let brief = "usage:\tmt-webface [options]"; + println!(""); + print!("{}", opts.usage(&brief)); +} + fn main() { + let args: Vec<String> = env::args().collect(); + + let mut opts = Options::new(); + opts.optflag("h", "help", "print this help menu"); + opts.optflag("b", "bind", "local IP:port to bind to"); + opts.optflag("", "version", "print the version"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(f) => { + println!("{}\n", f.to_string()); + print_usage(opts); + exit(-1); + } + }; + + if matches.opt_present("help") { + print_usage(opts); + return; + } + + if matches.opt_present("version") { + println!("modelthing {}", env!("CARGO_PKG_VERSION")); + return; + } + env_logger::init().unwrap(); let mut app = Pencil::new("webface"); @@ -65,6 +101,8 @@ fn main() { app.register_template("model_view.html"); app.get("/model/<model_id:string>", "model", model_view); - info!("Running on http://localhost:5000/"); - app.run("127.0.0.1:5000"); + let bind = matches.opt_str("bind").unwrap_or("127.0.0.1:5000".to_string()); + let bind_str: &str = &bind; + info!("Running on {}", bind); + app.run(bind_str); } |