From 730796a80d0eca520713c20971958e778ee06b67 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 12 Jun 2016 20:26:09 -0400 Subject: basic command line args --- Cargo.lock | 6 ++++++ Cargo.toml | 1 + src/main.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a18a400..10b995f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "exuberant-bovines" version = "0.1.0" dependencies = [ + "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "glium 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -202,6 +203,11 @@ dependencies = [ "winapi 0.2.7 (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 = "gif" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index 0cf2e19..b04a2f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ authors = ["bnewbold "] #glutin = { git = "https://github.com/bnewbold/glutin", branch = "feature-existing" } glium = "0.*" image = "0.*" +getopts = "^0.2" diff --git a/src/main.rs b/src/main.rs index f393f9d..e07c972 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ #[macro_use] extern crate glium; + extern crate image; +extern crate getopts; use std::env; use std::u64; +use std::process::exit; +use getopts::Options; use glium::glutin::os::unix::WindowBuilderExt; mod util; @@ -182,21 +186,54 @@ fn run(window_id: Option) { } +fn print_usage(program: &str, opts: Options) { + let brief = format!("Usage: {} [options]", program); + print!("{}", opts.usage(&brief)); +} + fn main() { let args: Vec = env::args().collect(); - - let window_id: Option = { - if args.len() >= 2 { - let parsed = if (&args[1]).starts_with("0x") { - u64::from_str_radix(args[1].trim_left_matches('0').trim_left_matches('x'), 16) - } else { - args[1].parse::() - }; - Some(parsed.expect("Failed to parse numerical arg")) - } else { - None + let program = args[0].clone(); + + // Turn, eg, "-root" into "--root" + let args = util::convert_xscreensaver_args(args); + + let mut opts = Options::new(); + opts.optflag("h", "help", "print this help menu"); + opts.optflag("", "window", "run in a window (IGNORED)"); + opts.optflag("", "root", "run in root window (IGNORED)"); + opts.optopt("", "window-id", "X window id number", "NUM"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => { m } + Err(f) => { + print_usage(&program, opts); + println!(""); + println!("{}", f.to_string()); + exit(-1); } }; + if matches.opt_present("help") { + print_usage(&program, opts); + exit(0); + } + + // if no "--window-id", try environment variable (arg has priority though) + let window_id_string: Option = + matches.opt_str("window-id") + .or(env::var("XSCREENSAVER_WINDOW").ok()); + + let window_id = window_id_string.map(|id| match util::dechex2u64(&id) { + Ok(y) => y, + Err(e) => { + println!("Couldn't parse numerical argument: {}", e); + exit(-1); }, + }); + + if window_id.is_some() { + println!("Drawing on existing X window: 0x{:07X}", window_id.unwrap()); + } + run(window_id); } -- cgit v1.2.3