ugly hacked in argument for port to remove unc0rr's excuse
authornemo
Fri, 12 Apr 2019 11:41:35 -0400
changeset 14797 38e66519e585
parent 14796 b889d9e1115f
child 14798 9de13d9a6312
ugly hacked in argument for port to remove unc0rr's excuse
rust/hedgewars-server/Cargo.toml
rust/hedgewars-server/src/main.rs
--- a/rust/hedgewars-server/Cargo.toml	Fri Apr 12 02:48:16 2019 +0300
+++ b/rust/hedgewars-server/Cargo.toml	Fri Apr 12 11:41:35 2019 -0400
@@ -10,6 +10,7 @@
 default = []
 
 [dependencies]
+getopts = "0.2.18"
 rand = "0.6"
 mio = "0.6"
 mio-extras = "2.0.5"
--- a/rust/hedgewars-server/src/main.rs	Fri Apr 12 02:48:16 2019 +0300
+++ b/rust/hedgewars-server/src/main.rs	Fri Apr 12 11:41:35 2019 -0400
@@ -1,6 +1,10 @@
+#![feature(self_struct_ctor)] 
 #![allow(unused_imports)]
 #![deny(bare_trait_objects)]
 
+extern crate getopts;
+use getopts::Options;
+use std::env;
 use log::*;
 use mio::net::*;
 use mio::*;
@@ -15,9 +19,32 @@
 fn main() {
     env_logger::init();
 
+    let args: Vec<String> = env::args().collect();
+    let mut opts = Options::new();
+
+    opts.optopt("p", "port", "port - defaults to 46631", "PORT");
+    opts.optflag("h", "help", "help");
+    let matches = match opts.parse(&args[1..]) {
+        Ok(m) => { m }
+        Err(f) => { panic!(f.to_string()) }
+    };
+    if matches.opt_present("h") {
+        println!("-p/--port - defaults to 46631");
+        return;
+    }
     info!("Hedgewars game server, protocol {}", utils::SERVER_VERSION);
 
-    let address = "0.0.0.0:46631".parse().unwrap();
+    let address;
+    if matches.opt_present("p") {
+        match matches.opt_str("p") {
+            Some(x) => address = format!("0.0.0.0:{}", x).parse().unwrap(),
+            None => address = "0.0.0.0:46631".parse().unwrap(),
+        }
+    }
+    else {
+        address = "0.0.0.0:46631".parse().unwrap();
+    }
+
     let listener = TcpListener::bind(&address).unwrap();
 
     let poll = Poll::new().unwrap();