mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
add usage error
This commit is contained in:
parent
2428a1ccfb
commit
e46ce2947e
3 changed files with 56 additions and 0 deletions
|
@ -27,6 +27,9 @@ macro_rules! show(
|
||||||
let e = $err;
|
let e = $err;
|
||||||
uucore::error::set_exit_code(e.code());
|
uucore::error::set_exit_code(e.code());
|
||||||
eprintln!("{}: {}", executable!(), e);
|
eprintln!("{}: {}", executable!(), e);
|
||||||
|
if e.usage() {
|
||||||
|
eprintln!("Try '{} --help' for more information.", executable!());
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -144,6 +144,13 @@ impl UError {
|
||||||
UError::Custom(e) => e.code(),
|
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 {
|
impl From<UCommonError> for UError {
|
||||||
|
@ -220,6 +227,10 @@ pub trait UCustomError: Error {
|
||||||
fn code(&self) -> i32 {
|
fn code(&self) -> i32 {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Box<dyn UCustomError>> for i32 {
|
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`].
|
/// Wrapper type around [`std::io::Error`].
|
||||||
///
|
///
|
||||||
/// The messages displayed by [`UIoError`] should match the error messages displayed by GNU
|
/// The messages displayed by [`UIoError`] should match the error messages displayed by GNU
|
||||||
|
@ -331,6 +373,10 @@ impl UIoError {
|
||||||
pub fn code(&self) -> i32 {
|
pub fn code(&self) -> i32 {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn usage(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for UIoError {}
|
impl Error for UIoError {}
|
||||||
|
@ -427,6 +473,10 @@ impl UCommonError {
|
||||||
pub fn code(&self) -> i32 {
|
pub fn code(&self) -> i32 {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn usage(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UCommonError> for i32 {
|
impl From<UCommonError> for i32 {
|
||||||
|
|
|
@ -103,6 +103,9 @@ pub fn gen_uumain(_args: TokenStream, stream: TokenStream) -> TokenStream {
|
||||||
if s != "" {
|
if s != "" {
|
||||||
show_error!("{}", s);
|
show_error!("{}", s);
|
||||||
}
|
}
|
||||||
|
if e.usage() {
|
||||||
|
eprintln!("Try '{} --help' for more information.", executable!());
|
||||||
|
}
|
||||||
e.code()
|
e.code()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue