aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs52
3 files changed, 56 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0f067ec..57dc6d2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2,10 +2,16 @@
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -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"
diff --git a/src/main.rs b/src/main.rs
index b4c7781..60780fc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,19 @@
+extern crate getopts;
+
+use getopts::Options;
+
+use std::env;
use std::io::{self, Write};
use std::io::prelude::*;
use std::net::TcpListener;
use std::process::Command;
-fn open_stream() -> io::Result<()> {
- let listener = TcpListener::bind("127.0.0.1:34254")?;
+const DEFAULT_PORT: u16 = 37705;
+
+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 {
@@ -14,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
@@ -30,8 +39,43 @@ fn open_stream() -> io::Result<()> {
Ok(())
}
+fn print_usage(opts: Options) {
+ let brief = "Usage: hearurl -b BROWSER";
+ print!("{}", opts.usage(&brief));
+}
+
fn main() {
- open_stream().unwrap_or_else(|e| {
+ let args: Vec<String> = env::args().collect();
+
+ 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..]) {
+ Ok(m) => m,
+ Err(e) => panic!(e.to_string()),
+ };
+
+ if opt_matches.opt_present("h") {
+ print_usage(opts);
+ return
+ }
+
+ let browser = match opt_matches.opt_str("b") {
+ Some(b) => b,
+ None => {
+ print_usage(opts);
+ return
+ },
+ };
+
+ let port: u16 = match opt_matches.opt_str("p") {
+ Some(p) => p.parse().unwrap(),
+ None => DEFAULT_PORT,
+ };
+
+ open_stream(browser, port).unwrap_or_else(|e| {
writeln!(io::stderr(), "{}", e)
.expect("Failed printing to stderr");
});