From 04f3bc7902f0eba88213ef418cfdfb7f325ec297 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 25 Jan 2015 13:21:30 +0100 Subject: vtcol.rs: add usage and improve validity check --- vtcol.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/vtcol.rs b/vtcol.rs index be3081a..acf8db7 100644 --- a/vtcol.rs +++ b/vtcol.rs @@ -21,6 +21,7 @@ impl Job { -> Job { let argv = std::os::args(); + let this = argv[0].clone(); let opts = &[ getopts::optopt("s", "scheme", "predefined color scheme", "NAME"), getopts::optflag("h", "help", "print this message") @@ -31,21 +32,35 @@ impl Job { Err(f) => panic!(f.to_string()) }; let scheme = match matches.opt_str("s") { - None => Scheme::Default, + None => { + Job::usage(&this, opts); + panic!("no color scheme given, aborting") + }, Some (name) => { match name.as_slice() { "solarized" | "solarized_dark" | "sd" => Scheme::SolarizedDark, "solarized_light" | "sl" => Scheme::SolarizedLight, - _ => Scheme::Default + "default" | "normal" => Scheme::Default, + garbage => { + Job::usage(&this, opts); + panic!("unknown color scheme “{}”, aborting", garbage); + } } } }; Job { - this : argv[0].clone(), + this : this, scheme : scheme } } + fn + usage (this : &String, opts: &[getopts::OptGroup]) + { + let brief = format!("usage: {} [options]", this); + print!("{}", getopts::usage(brief.as_slice(), opts)); + } + } /* [impl Job] */ /* Rust appears to come with two wrappers for ``ioctl(2)``, but neither can be utilized for our -- cgit v1.2.3