diff --git a/Cargo.lock b/Cargo.lock index f655daf48d..4b69394d60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "trumpet" version = "0.0.2" dependencies = [ + "clap 1.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "hound 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -22,6 +23,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bitflags" version = "0.3.3" @@ -38,6 +44,17 @@ dependencies = [ "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "clap" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "conduit-mime-types" version = "0.7.3" @@ -358,6 +375,11 @@ dependencies = [ "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "tempdir" version = "0.3.4" @@ -454,6 +476,11 @@ dependencies = [ "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "vec_map" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.5" diff --git a/Cargo.toml b/Cargo.toml index 707ae29770..46d51e0a6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ name = "trumpet" [dependencies] + clap = "1.5.*" # CLI parsing. #cpal = "*" # Hardware audio output (for testing); works well with Hound. hound = "1.1.0" # WAV codec. hyper = "0.7.*" diff --git a/src/main.rs b/src/main.rs index f6ca1a3b3a..fd4a9a23e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // Copyright (c) 2015 Brandon Thomas +extern crate clap; extern crate crypto; extern crate hound; extern crate iron; @@ -14,6 +15,7 @@ pub mod handlers; pub mod logger; pub mod words; +use clap::{App, Arg, ArgMatches}; use iron::prelude::*; use router::Router; @@ -25,10 +27,34 @@ use logger::SimpleLogger; fn main() { SimpleLogger::init().unwrap(); - start_server(); + + let matches = App::new("trumpet") + .arg(Arg::with_name("PORT") + .short("p") + .long("port") + .help("Sets the port the server listens on.") + .takes_value(true) + .required(false)) + .get_matches(); + + let port = get_port(&matches, 9000); + + start_server(port); } -fn start_server() { +fn get_port(matches: &ArgMatches, default_port: u16) -> u16 { + match matches.value_of("PORT") { + None => default_port, + Some(port) => { + match port.parse::() { + Err(_) => default_port, + Ok(p) => p, + } + }, + } +} + +fn start_server(port: u16) { let audio_path = "./sounds/trump"; let file_path = "./web"; let index = "index.html"; @@ -44,7 +70,7 @@ fn start_server() { router.get("/", FileServerHandler::new(file_path, index)); router.get("/assets/:filename", FileServerHandler::new(file_path, index)); - info!("Starting server..."); - Iron::new(router).http("0.0.0.0:9000").unwrap(); + info!("Starting server on port {}...", port); + Iron::new(router).http(("0.0.0.0", port)).unwrap(); }