diff options
| author | Teddy Wing | 2017-05-07 05:08:48 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-05-07 05:08:48 +0200 | 
| commit | ff613b36df684a78138541a72c49ff9d2c48c386 (patch) | |
| tree | cc9af63cd135e324dd47926379766b4c9e11cde9 | |
| parent | fa811ee48c200667a71250f2a9cfbadcc5319949 (diff) | |
| download | HearURL-ff613b36df684a78138541a72c49ff9d2c48c386.tar.bz2 | |
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.
| -rw-r--r-- | src/main.rs | 25 | 
1 files changed, 25 insertions, 0 deletions
| 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<String> = 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"); | 
