From ff613b36df684a78138541a72c49ff9d2c48c386 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 05:08:48 +0200 Subject: main.rs: Add command line options parsing Use Getopts to parse command line options. Currently only has a 'help' option, but this lays the foundation for user-customisable browser and port values. --- src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b4c7781..7e971a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +extern crate getopts; + +use getopts::Options; + +use std::env; use std::io::{self, Write}; use std::io::prelude::*; use std::net::TcpListener; @@ -30,7 +35,27 @@ fn open_stream() -> io::Result<()> { Ok(()) } +fn print_usage(opts: Options) { + let brief = "Usage: hearurl -b BROWSER"; + print!("{}", opts.usage(&brief)); +} + fn main() { + let args: Vec = env::args().collect(); + + let mut opts = Options::new(); + opts.optflag("h", "help", "print this help menu"); + + let opt_matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => panic!(e.to_string()), + }; + + if opt_matches.opt_present("h") { + print_usage(opts); + return + } + open_stream().unwrap_or_else(|e| { writeln!(io::stderr(), "{}", e) .expect("Failed printing to stderr"); -- cgit v1.2.3 From e9f04e34f55c66890ad3d5351b36ad0cf914b7de Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 13:07:57 +0200 Subject: main.rs: Add browser option This will be used to set the browser to open URLs in. --- src/main.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 7e971a7..183079a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); + opts.optopt("b", "browser", "set browser", "BROWSER"); opts.optflag("h", "help", "print this help menu"); let opt_matches = match opts.parse(&args[1..]) { @@ -56,6 +57,13 @@ fn main() { return } + let browser = if opt_matches.opt_present("b") { + opt_matches.opt_str("b") + } else { + print_usage(opts); + return + }; + open_stream().unwrap_or_else(|e| { writeln!(io::stderr(), "{}", e) .expect("Failed printing to stderr"); -- cgit v1.2.3 From 09d7aa3f3dc1a2b9241bc0ec62bee029b8942dc1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 13:22:35 +0200 Subject: main.rs: Match `Option` when setting browser Instead of checking for the presence of the `-b` option before setting it, just use an `Option` match since `opt_str` returns an `Option`. --- src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 183079a..c6e7054 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,11 +57,12 @@ fn main() { return } - let browser = if opt_matches.opt_present("b") { - opt_matches.opt_str("b") - } else { - print_usage(opts); - return + let browser = match opt_matches.opt_str("b") { + Some(b) => b, + None => { + print_usage(opts); + return + }, }; open_stream().unwrap_or_else(|e| { -- cgit v1.2.3 From 1e4cbaa5f2285b673d5335612265e1fcd298ca82 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 13:48:27 +0200 Subject: main.rs: Add port command line option Provide users with an option to change the default port the program listens on. --- src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index c6e7054..098a8bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ fn main() { let mut opts = Options::new(); opts.optopt("b", "browser", "set browser", "BROWSER"); + opts.optopt("p", "port", "set port number", "PORT"); opts.optflag("h", "help", "print this help menu"); let opt_matches = match opts.parse(&args[1..]) { @@ -65,6 +66,11 @@ fn main() { }, }; + let port: u16 = match opt_matches.opt_str("p") { + Some(p) => p.parse().unwrap(), + None => 37705, + }; + open_stream().unwrap_or_else(|e| { writeln!(io::stderr(), "{}", e) .expect("Failed printing to stderr"); -- cgit v1.2.3 From 8043ad6e42611a68e3a572c4657285030d51e74e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 13:55:29 +0200 Subject: main.rs: Add a constant for default port Remove the magic number and replace it with a named value. --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 098a8bb..e6e7f61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use std::io::prelude::*; use std::net::TcpListener; use std::process::Command; +const DEFAULT_PORT: u16 = 37705; + fn open_stream() -> io::Result<()> { let listener = TcpListener::bind("127.0.0.1:34254")?; @@ -68,7 +70,7 @@ fn main() { let port: u16 = match opt_matches.opt_str("p") { Some(p) => p.parse().unwrap(), - None => 37705, + None => DEFAULT_PORT, }; open_stream().unwrap_or_else(|e| { -- cgit v1.2.3 From 69dd2a7a23d1d71c50a91f5779db6f0480106cd8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 7 May 2017 14:14:17 +0200 Subject: open_stream(): Remove hard-coded browser & port Instead get these from function arguments, which in turn get fed from the command line arguments passed by the user. --- src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e6e7f61..60780fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,10 @@ use std::process::Command; const DEFAULT_PORT: u16 = 37705; -fn open_stream() -> io::Result<()> { - let listener = TcpListener::bind("127.0.0.1:34254")?; +fn open_stream(browser: String, port: u16) -> io::Result<()> { + let listener = TcpListener::bind( + format!("127.0.0.1:{}", port) + )?; for stream in listener.incoming() { match stream { @@ -21,7 +23,7 @@ fn open_stream() -> io::Result<()> { Command::new("open") .arg("-a") - .arg("Opera") + .arg(&browser) // Trim the trailing newline, otherwise this doesn't // work @@ -73,7 +75,7 @@ fn main() { None => DEFAULT_PORT, }; - open_stream().unwrap_or_else(|e| { + open_stream(browser, port).unwrap_or_else(|e| { writeln!(io::stderr(), "{}", e) .expect("Failed printing to stderr"); }); -- cgit v1.2.3