2016-08-08 07:27:06 +00:00
|
|
|
extern crate getopts;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
pub struct CoreOptions {
|
|
|
|
pub options : getopts::Options,
|
2016-08-09 16:16:06 +00:00
|
|
|
pkgname: &'static str,
|
2016-08-08 07:27:06 +00:00
|
|
|
longhelp : Option<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CoreOptions {
|
2016-08-09 16:16:06 +00:00
|
|
|
pub fn new(name: &'static str) -> Self {
|
2016-08-08 07:27:06 +00:00
|
|
|
let mut ret = CoreOptions {
|
|
|
|
options : getopts::Options::new(),
|
2016-08-09 16:16:06 +00:00
|
|
|
pkgname: name,
|
|
|
|
longhelp: None
|
2016-08-08 07:27:06 +00:00
|
|
|
};
|
|
|
|
ret.options
|
|
|
|
.optflag("", "help", "print usage information")
|
|
|
|
.optflag("", "version", "print name and version number");
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
pub fn optopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions {
|
|
|
|
self.options.optopt(short_name, long_name, desc, hint);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions {
|
|
|
|
self.options.optflag(short_name, long_name, desc);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
pub fn help<T: Into<String>>(&mut self, longhelp : T) -> &mut CoreOptions {
|
|
|
|
self.longhelp = Some(longhelp.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
pub fn usage(&self, summary : &str) -> String {
|
|
|
|
self.options.usage(summary)
|
|
|
|
}
|
|
|
|
pub fn parse(&mut self, args : Vec<String>) -> getopts::Matches {
|
|
|
|
let matches = match self.options.parse(&args[1..]) {
|
|
|
|
Ok(m) => { Some(m) },
|
|
|
|
Err(f) => {
|
2016-08-09 16:16:06 +00:00
|
|
|
eprintln!("{}: {}", self.pkgname, f);
|
|
|
|
eprintln!("Try '{} --help' for more information.", self.pkgname);
|
|
|
|
exit!(1)
|
2016-08-08 07:27:06 +00:00
|
|
|
}
|
|
|
|
}.unwrap();
|
|
|
|
if matches.opt_present("help") {
|
|
|
|
exit!(match self.longhelp {
|
2016-08-09 16:16:06 +00:00
|
|
|
Some(ref lhelp) => { println!("{}", lhelp); 0}
|
2016-08-08 07:27:06 +00:00
|
|
|
None => 1
|
|
|
|
});
|
|
|
|
} else if matches.opt_present("version") {
|
2016-08-09 16:16:06 +00:00
|
|
|
println!("{} {}", self.pkgname, env!("CARGO_PKG_VERSION"));
|
2016-08-08 07:27:06 +00:00
|
|
|
exit!(0);
|
|
|
|
}
|
|
|
|
matches
|
|
|
|
}
|
2016-08-09 16:16:06 +00:00
|
|
|
}
|