add usage error

This commit is contained in:
Terts Diepraam 2021-07-02 19:31:16 +02:00
parent 2428a1ccfb
commit e46ce2947e
3 changed files with 56 additions and 0 deletions

View file

@ -27,6 +27,9 @@ macro_rules! show(
let e = $err;
uucore::error::set_exit_code(e.code());
eprintln!("{}: {}", executable!(), e);
if e.usage() {
eprintln!("Try '{} --help' for more information.", executable!());
}
})
);

View file

@ -144,6 +144,13 @@ impl UError {
UError::Custom(e) => e.code(),
}
}
pub fn usage(&self) -> bool {
match self {
UError::Common(e) => e.usage(),
UError::Custom(e) => e.usage(),
}
}
}
impl From<UCommonError> for UError {
@ -220,6 +227,10 @@ pub trait UCustomError: Error {
fn code(&self) -> i32 {
1
}
fn usage(&self) -> bool {
false
}
}
impl From<Box<dyn UCustomError>> for i32 {
@ -291,6 +302,37 @@ impl UCustomError for USimpleError {
}
}
#[derive(Debug)]
pub struct UUsageError {
pub code: i32,
pub message: String,
}
impl UUsageError {
#[allow(clippy::new_ret_no_self)]
pub fn new(code: i32, message: String) -> UError {
UError::Custom(Box::new(Self { code, message }))
}
}
impl Error for UUsageError {}
impl Display for UUsageError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
self.message.fmt(f)
}
}
impl UCustomError for UUsageError {
fn code(&self) -> i32 {
self.code
}
fn usage(&self) -> bool {
true
}
}
/// Wrapper type around [`std::io::Error`].
///
/// The messages displayed by [`UIoError`] should match the error messages displayed by GNU
@ -331,6 +373,10 @@ impl UIoError {
pub fn code(&self) -> i32 {
1
}
pub fn usage(&self) -> bool {
false
}
}
impl Error for UIoError {}
@ -427,6 +473,10 @@ impl UCommonError {
pub fn code(&self) -> i32 {
1
}
pub fn usage(&self) -> bool {
false
}
}
impl From<UCommonError> for i32 {

View file

@ -103,6 +103,9 @@ pub fn gen_uumain(_args: TokenStream, stream: TokenStream) -> TokenStream {
if s != "" {
show_error!("{}", s);
}
if e.usage() {
eprintln!("Try '{} --help' for more information.", executable!());
}
e.code()
}
}