1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27:45 +00:00

uucore: require version; syntax; summary; longhelp

This commit is contained in:
Nathan Ross 2016-08-11 12:37:33 -04:00
parent 8632e19b44
commit f8509240df
3 changed files with 44 additions and 31 deletions

View file

@ -1,36 +1,38 @@
extern crate getopts; extern crate getopts;
use std::io::Write; use std::io::Write;
pub struct CoreOptions { pub struct HelpText<'a> {
pub options : getopts::Options, pub name : &'a str,
pkgname: &'static str, pub version : &'a str,
longhelp : Option<String> pub syntax : &'a str,
pub summary : &'a str,
pub long_help : &'a str
} }
impl<'a> CoreOptions { pub struct CoreOptions<'a> {
pub fn new(name: &'static str) -> Self { options : getopts::Options,
help_text : HelpText<'a>
}
impl<'a> CoreOptions<'a> {
pub fn new(help_text: HelpText<'a>) -> Self {
let mut ret = CoreOptions { let mut ret = CoreOptions {
options : getopts::Options::new(), options : getopts::Options::new(),
pkgname: name, help_text : help_text
longhelp: None
}; };
ret.options ret.options
.optflag("", "help", "print usage information") .optflag("", "help", "print usage information")
.optflag("", "version", "print name and version number"); .optflag("", "version", "print name and version number");
ret ret
} }
pub fn optopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions { pub fn optopt(&mut self, short_name: &str, long_name: &str, desc: &str, hint: &str) -> &mut CoreOptions<'a> {
self.options.optopt(short_name, long_name, desc, hint); self.options.optopt(short_name, long_name, desc, hint);
self self
} }
pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions { pub fn optflag(&mut self, short_name: &str, long_name: &str, desc: &str) -> &mut CoreOptions<'a> {
self.options.optflag(short_name, long_name, desc); self.options.optflag(short_name, long_name, desc);
self 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 { pub fn usage(&self, summary : &str) -> String {
self.options.usage(summary) self.options.usage(summary)
} }
@ -38,20 +40,39 @@ impl<'a> CoreOptions {
let matches = match self.options.parse(&args[1..]) { let matches = match self.options.parse(&args[1..]) {
Ok(m) => { Some(m) }, Ok(m) => { Some(m) },
Err(f) => { Err(f) => {
eprintln!("{}: {}", self.pkgname, f); crash!(1, "{}", f);
eprintln!("Try '{} --help' for more information.", self.pkgname);
exit!(1)
} }
}.unwrap(); }.unwrap();
if matches.opt_present("help") { if matches.opt_present("help") {
exit!(match self.longhelp { print!("
Some(ref lhelp) => { println!("{}", lhelp); 0} {0} {1}
None => 1
}); {0} {2}
{3}
Reference
{4}
",
self.help_text.name, self.help_text.version, self.help_text.syntax, self.options.usage(self.help_text.summary), self.help_text.long_help);
exit!(0);
} else if matches.opt_present("version") { } else if matches.opt_present("version") {
println!("{} {}", self.pkgname, env!("CARGO_PKG_VERSION")); print!("{} {}", self.help_text.name, self.help_text.version);
exit!(0); exit!(0);
} }
matches matches
} }
} }
#[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,
long_help: $long_help
})
);
}

View file

@ -3,6 +3,7 @@ pub extern crate libc;
#[macro_use] #[macro_use]
mod macros; mod macros;
#[macro_use]
pub mod coreopts; pub mod coreopts;
#[cfg(feature = "fs")] #[cfg(feature = "fs")]

View file

@ -264,15 +264,6 @@ macro_rules! snippet_list_join_or {
); );
} }
//-- message templates : help and version
#[macro_export]
macro_rules! msg_version {
() => (
format!("{} {}", executable!(), env!("CARGO_PKG_VERSION"))
)
}
//-- message templates : invalid input //-- message templates : invalid input
#[macro_export] #[macro_export]