From fa811ee48c200667a71250f2a9cfbadcc5319949 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 23 Apr 2017 03:02:16 +0200 Subject: Cargo.*: Add 'getopts' crate For easier command line option parsing. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + 2 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 0f067ec..57dc6d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,9 +2,15 @@ name = "hearurl" version = "0.0.1" dependencies = [ + "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "getopts" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "idna" version = "0.1.1" @@ -43,6 +49,7 @@ dependencies = [ ] [metadata] +"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" "checksum idna 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ac85ec3f80c8e4e99d9325521337e14ec7555c458a14e377d189659a427f375" "checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1" "checksum unicode-bidi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a078ebdd62c0e71a709c3d53d2af693fe09fe93fbff8344aebe289b78f9032" diff --git a/Cargo.toml b/Cargo.toml index 3132e1d..75c36b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,5 @@ name = "hearurl" version = "0.0.1" [dependencies] +getopts = "0.2" url = "1.4.0" -- cgit v1.2.3 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(+) 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(+) 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(-) 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(+) 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(-) 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(-) 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