mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 05:27:45 +00:00
Merge pull request #2831 from jfinkels/test-uresult
test: return UResult from uumain() function
This commit is contained in:
commit
46767952ce
1 changed files with 9 additions and 11 deletions
|
@ -13,7 +13,8 @@ mod parser;
|
||||||
use clap::{crate_version, App, AppSettings};
|
use clap::{crate_version, App, AppSettings};
|
||||||
use parser::{parse, Operator, Symbol, UnaryOperator};
|
use parser::{parse, Operator, Symbol, UnaryOperator};
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use uucore::{display::Quotable, show_error};
|
use uucore::display::Quotable;
|
||||||
|
use uucore::error::{UResult, USimpleError};
|
||||||
|
|
||||||
const USAGE: &str = "test EXPRESSION
|
const USAGE: &str = "test EXPRESSION
|
||||||
or: test
|
or: test
|
||||||
|
@ -91,7 +92,8 @@ pub fn uu_app() -> App<'static, 'static> {
|
||||||
.setting(AppSettings::DisableVersion)
|
.setting(AppSettings::DisableVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(mut args: impl uucore::Args) -> i32 {
|
#[uucore_procs::gen_uumain]
|
||||||
|
pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
|
||||||
let program = args.next().unwrap_or_else(|| OsString::from("test"));
|
let program = args.next().unwrap_or_else(|| OsString::from("test"));
|
||||||
let binary_name = uucore::util_name();
|
let binary_name = uucore::util_name();
|
||||||
let mut args: Vec<_> = args.collect();
|
let mut args: Vec<_> = args.collect();
|
||||||
|
@ -109,13 +111,12 @@ pub fn uumain(mut args: impl uucore::Args) -> i32 {
|
||||||
.setting(AppSettings::NeedsLongHelp)
|
.setting(AppSettings::NeedsLongHelp)
|
||||||
.setting(AppSettings::NeedsLongVersion)
|
.setting(AppSettings::NeedsLongVersion)
|
||||||
.get_matches_from(std::iter::once(program).chain(args.into_iter()));
|
.get_matches_from(std::iter::once(program).chain(args.into_iter()));
|
||||||
return 0;
|
return Ok(());
|
||||||
}
|
}
|
||||||
// If invoked via name '[', matching ']' must be in the last arg
|
// If invoked via name '[', matching ']' must be in the last arg
|
||||||
let last = args.pop();
|
let last = args.pop();
|
||||||
if last.as_deref() != Some(OsStr::new("]")) {
|
if last.as_deref() != Some(OsStr::new("]")) {
|
||||||
show_error!("missing ']'");
|
return Err(USimpleError::new(2, "missing ']'"));
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,15 +125,12 @@ pub fn uumain(mut args: impl uucore::Args) -> i32 {
|
||||||
match result {
|
match result {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if result {
|
if result {
|
||||||
0
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
1
|
Err(1.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(USimpleError::new(2, e)),
|
||||||
show_error!("{}", e);
|
|
||||||
2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue