mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
uucore: CoreOptions error and version message templates
This commit is contained in:
parent
c91fd51197
commit
3eb9bbf4b5
3 changed files with 55 additions and 0 deletions
|
@ -6,6 +6,7 @@ authors = []
|
|||
[dependencies]
|
||||
libc = "*"
|
||||
winapi = "*"
|
||||
getopts = "*"
|
||||
data-encoding = "^1.1"
|
||||
|
||||
[lib]
|
||||
|
|
53
src/uucore/coreopts.rs
Normal file
53
src/uucore/coreopts.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
extern crate getopts;
|
||||
use std::io::Write;
|
||||
|
||||
pub struct CoreOptions {
|
||||
pub options : getopts::Options,
|
||||
longhelp : Option<String>
|
||||
}
|
||||
|
||||
impl<'a> CoreOptions {
|
||||
pub fn new() -> Self {
|
||||
let mut ret = CoreOptions {
|
||||
options : getopts::Options::new(),
|
||||
longhelp : None
|
||||
};
|
||||
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) => {
|
||||
crash!(1, "{}", msg_invalid_input!(format!("{}", f)));
|
||||
}
|
||||
}.unwrap();
|
||||
if matches.opt_present("help") {
|
||||
exit!(match self.longhelp {
|
||||
Some(ref lhelp) => { print!("{}", lhelp); 0}
|
||||
None => 1
|
||||
});
|
||||
} else if matches.opt_present("version") {
|
||||
print!("{}", msg_version!());
|
||||
exit!(0);
|
||||
}
|
||||
matches
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ pub mod fs;
|
|||
pub mod parse_time;
|
||||
pub mod utf8;
|
||||
pub mod encoding;
|
||||
pub mod coreopts;
|
||||
|
||||
#[cfg(unix)] pub mod c_types;
|
||||
#[cfg(unix)] pub mod process;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue