diff --git a/src/server.rs b/src/server.rs index 3f7d2bc..9a37d93 100644 --- a/src/server.rs +++ b/src/server.rs @@ -27,6 +27,7 @@ fn main() { let mut watching = None; let mut command = None; let mut ignore = vec![]; + let mut port = None; let exit_code = loop { let arg = args.next(); @@ -37,50 +38,69 @@ fn main() { let watch_is_some = watching.is_some(); let command_is_some = command.is_some(); + let port_is_some = port.is_some(); - match (arg, next, watch_is_some, command_is_some) { - (None, _, _, _) => { + match (arg, next, watch_is_some, command_is_some, port_is_some) { + (None, _, _, _, _) => { break None; } - (Some("-w") | Some("--watch"), Some(value), false, _) => { + (Some("-w") | Some("--watch"), Some(value), false, _, _) => { watching = Some(value.to_string()); } - (Some("-w") | Some("--watch"), Some(_), true, _) => { + (Some("-w") | Some("--watch"), Some(_), true, _, _) => { error!("argument watch present multiple times"); break Some(1); } - (Some("-w") | Some("--watch"), None, _, _) => { + (Some("-w") | Some("--watch"), None, _, _, _) => { error!("argument watch without value"); break Some(1); } - (Some("-i") | Some("--ignore"), Some(value), _, _) => { + (Some("-i") | Some("--ignore"), Some(value), _, _, _) => { ignore.push(value.to_string()); } - (Some("-i") | Some("--ignore"), None, _, _) => { + (Some("-i") | Some("--ignore"), None, _, _, _) => { error!("argument ignore without value"); break Some(1); } - (Some("-c") | Some("--command"), Some(value), _, false) => { + (Some("-c") | Some("--command"), Some(value), _, false, _) => { command = Some(value.to_string()); } - (Some("-c") | Some("--command"), Some(_), _, true) => { + (Some("-c") | Some("--command"), Some(_), _, true, _) => { error!("argument command present multiple times"); break Some(1); } - (Some("-c") | Some("--command"), None, _, _) => { + (Some("-c") | Some("--command"), None, _, _, _) => { error!("argument command without value"); break Some(1); } - (Some(value), _, _, _) => { + (Some("-p") | Some("--port"), Some(value), _, _, false) => match value.parse::() { + Ok(v) => port = Some(v), + Err(e) => { + error!("couldn't parse port \"{}\", : {}", value, e); + break Some(1); + } + }, + + (Some("-p") | Some("--port"), Some(_), _, _, true) => { + error!("argument port present multiple times"); + break Some(1); + } + + (Some("-p") | Some("--port"), None, _, _, _) => { + error!("argument port without value"); + break Some(1); + } + + (Some(value), _, _, _, _) => { error!("argument \"{}\" unknown", value); break Some(1); } @@ -89,6 +109,8 @@ fn main() { args.next(); }; + let port = port.unwrap_or(1500); + if let Some(exit_code) = exit_code { exit(exit_code); } @@ -106,7 +128,7 @@ fn main() { let clone = tasks.clone(); thread::spawn(move || { - let addr = ([127, 0, 0, 1], 1500).into(); + let addr = ([127, 0, 0, 1], port).into(); let server = Server::bind(&addr) .serve(move || {