2016-08-08 07:27:06 +00:00
|
|
|
extern crate getopts;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2016-08-11 16:37:33 +00:00
|
|
|
pub struct HelpText<'a> {
|
|
|
|
pub name : &'a str,
|
|
|
|
pub version : &'a str,
|
|
|
|
pub syntax : &'a str,
|
|
|
|
pub summary : &'a str,
|
2016-08-12 03:20:39 +00:00
|
|
|
pub long_help : &'a str,
|
|
|
|
pub display_usage : bool
|
2016-08-08 07:27:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:37:33 +00:00
|
|
|
pub struct CoreOptions<'a> {
|
|
|
|
options : getopts::Options,
|
|
|
|
help_text : HelpText<'a>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CoreOptions<'a> {
|
|
|
|
pub fn new(help_text: HelpText<'a>) -> Self {
|
2016-08-08 07:27:06 +00:00
|
|
|
let mut ret = CoreOptions {
|
|
|
|
options : getopts::Options::new(),
|
2016-08-11 16:37:33 +00:00
|
|
|
help_text : help_text
|
2016-08-08 07:27:06 +00:00
|
|
|
};
|
|
|
|
ret.options
|
|
|
|
.optflag("", "help", "print usage information")
|
|
|
|
.optflag("", "version", "print name and version number");
|
|
|
|
ret
|
|
|
|
}
|
2016-08-11 16:37:33 +00:00
|
|
|
pub fn optopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions<'a> {
|
2016-08-08 07:27:06 +00:00
|
|
|
self.options.optopt(short_name, long_name, desc, hint);
|
|
|
|
self
|
|
|
|
}
|
2016-08-11 16:37:33 +00:00
|
|
|
pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions<'a> {
|
2016-08-08 07:27:06 +00:00
|
|
|
self.options.optflag(short_name, long_name, desc);
|
|
|
|
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-11 16:37:33 +00:00
|
|
|
crash!(1, "{}", f);
|
2016-08-08 07:27:06 +00:00
|
|
|
}
|
|
|
|
}.unwrap();
|
2016-08-12 03:20:39 +00:00
|
|
|
if matches.opt_present("help") {
|
|
|
|
let usage_str = if self.help_text.display_usage {
|
|
|
|
format!("\n {}\n\n Reference\n",
|
|
|
|
self.options.usage(self.help_text.summary)
|
|
|
|
).replace("Options:", " Options:")
|
|
|
|
} else { String::new() };
|
2016-08-11 16:37:33 +00:00
|
|
|
print!("
|
|
|
|
{0} {1}
|
|
|
|
|
|
|
|
{0} {2}
|
2016-08-12 03:20:39 +00:00
|
|
|
{3}{4}
|
|
|
|
", self.help_text.name, self.help_text.version, self.help_text.syntax, usage_str, self.help_text.long_help);
|
2016-08-11 16:37:33 +00:00
|
|
|
exit!(0);
|
2016-08-08 07:27:06 +00:00
|
|
|
} else if matches.opt_present("version") {
|
2016-08-12 03:20:39 +00:00
|
|
|
println!("{} {}", self.help_text.name, self.help_text.version);
|
2016-08-08 07:27:06 +00:00
|
|
|
exit!(0);
|
|
|
|
}
|
|
|
|
matches
|
|
|
|
}
|
2016-08-09 16:16:06 +00:00
|
|
|
}
|
2016-08-11 16:37:33 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! new_coreopts {
|
|
|
|
($syntax: expr, $summary: expr, $long_help: expr) => (
|
|
|
|
uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText {
|
|
|
|
name: executable!(),
|
|
|
|
version: env!("CARGO_PKG_VERSION"),
|
|
|
|
syntax: $syntax,
|
|
|
|
summary: $summary,
|
2016-08-12 03:20:39 +00:00
|
|
|
long_help: $long_help,
|
|
|
|
display_usage: true
|
|
|
|
})
|
|
|
|
);
|
|
|
|
($syntax: expr, $summary: expr, $long_help: expr, $display_usage: expr) => (
|
|
|
|
uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText {
|
|
|
|
name: executable!(),
|
|
|
|
version: env!("CARGO_PKG_VERSION"),
|
|
|
|
syntax: $syntax,
|
|
|
|
summary: $summary,
|
|
|
|
long_help: $long_help,
|
|
|
|
display_usage: $display_usage
|
2016-08-11 16:37:33 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|