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:
parent
8632e19b44
commit
f8509240df
3 changed files with 44 additions and 31 deletions
|
@ -1,36 +1,38 @@
|
|||
extern crate getopts;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct CoreOptions {
|
||||
pub options : getopts::Options,
|
||||
pkgname: &'static str,
|
||||
longhelp : Option<String>
|
||||
pub struct HelpText<'a> {
|
||||
pub name : &'a str,
|
||||
pub version : &'a str,
|
||||
pub syntax : &'a str,
|
||||
pub summary : &'a str,
|
||||
pub long_help : &'a str
|
||||
}
|
||||
|
||||
impl<'a> CoreOptions {
|
||||
pub fn new(name: &'static str) -> Self {
|
||||
pub struct CoreOptions<'a> {
|
||||
options : getopts::Options,
|
||||
help_text : HelpText<'a>
|
||||
}
|
||||
|
||||
impl<'a> CoreOptions<'a> {
|
||||
pub fn new(help_text: HelpText<'a>) -> Self {
|
||||
let mut ret = CoreOptions {
|
||||
options : getopts::Options::new(),
|
||||
pkgname: name,
|
||||
longhelp: None
|
||||
help_text : help_text
|
||||
};
|
||||
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 {
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -38,20 +40,39 @@ impl<'a> CoreOptions {
|
|||
let matches = match self.options.parse(&args[1..]) {
|
||||
Ok(m) => { Some(m) },
|
||||
Err(f) => {
|
||||
eprintln!("{}: {}", self.pkgname, f);
|
||||
eprintln!("Try '{} --help' for more information.", self.pkgname);
|
||||
exit!(1)
|
||||
crash!(1, "{}", f);
|
||||
}
|
||||
}.unwrap();
|
||||
if matches.opt_present("help") {
|
||||
exit!(match self.longhelp {
|
||||
Some(ref lhelp) => { println!("{}", lhelp); 0}
|
||||
None => 1
|
||||
});
|
||||
print!("
|
||||
{0} {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") {
|
||||
println!("{} {}", self.pkgname, env!("CARGO_PKG_VERSION"));
|
||||
print!("{} {}", self.help_text.name, self.help_text.version);
|
||||
exit!(0);
|
||||
}
|
||||
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
|
||||
})
|
||||
);
|
||||
}
|
|
@ -3,6 +3,7 @@ pub extern crate libc;
|
|||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
#[macro_use]
|
||||
pub mod coreopts;
|
||||
|
||||
#[cfg(feature = "fs")]
|
||||
|
|
|
@ -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
|
||||
|
||||
#[macro_export]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue