true/false: remove large clap call

This commit is contained in:
Michael Yang
2026-02-03 14:44:13 +11:00
parent 3bab3a843b
commit 7b4373b6d8
4 changed files with 43 additions and 36 deletions
+13 -17
View File
@@ -10,8 +10,6 @@ use uucore::translate;
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut command = uu_app();
// Mirror GNU options, always return `1`. In particular even the 'successful' cases of no-op,
// and the interrupted display of help and version should return `1`. Also, we return Ok in all
// paths to avoid the allocation of an error object, an operation that could, in theory, fail
@@ -19,26 +17,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
set_exit_code(1);
let args: Vec<OsString> = args.collect();
if args.len() > 2 {
if args.len() != 2 {
return Ok(());
}
if let Err(e) = command.try_get_matches_from_mut(args) {
// For the false command, we don't want to show any error messages for UnknownArgument
// since false should produce no output and just exit with code 1
let error = match e.kind() {
clap::error::ErrorKind::DisplayHelp => command.print_help(),
clap::error::ErrorKind::DisplayVersion => {
write!(std::io::stdout(), "{}", command.render_version())
}
_ => Ok(()),
};
// args[0] is the name of the binary.
let error = if args[1] == "--help" {
uu_app().print_help()
} else if args[1] == "--version" {
write!(std::io::stdout(), "{}", uu_app().render_version())
} else {
Ok(())
};
if let Err(print_fail) = error {
// Try to display this error.
if let Err(print_fail) = error {
// Completely ignore any error here, no more failover and we will fail in any case.
let _ = writeln!(std::io::stderr(), "{}: {print_fail}", uucore::util_name());
}
let _ = writeln!(std::io::stderr(), "{}: {print_fail}", uucore::util_name());
// Completely ignore any error here, no more failover and we will fail in any case.
set_exit_code(1);
}
Ok(())
+16 -19
View File
@@ -10,30 +10,27 @@ use uucore::translate;
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut command = uu_app();
let args: Vec<OsString> = args.collect();
if args.len() > 2 {
if args.len() != 2 {
return Ok(());
}
if let Err(e) = command.try_get_matches_from_mut(args) {
let error = match e.kind() {
clap::error::ErrorKind::DisplayHelp => command.print_help(),
clap::error::ErrorKind::DisplayVersion => {
write!(std::io::stdout(), "{}", command.render_version())
}
_ => Ok(()),
};
// args[0] is the name of the binary.
let error = if args[1] == "--help" {
uu_app().print_help()
} else if args[1] == "--version" {
write!(std::io::stdout(), "{}", uu_app().render_version())
} else {
Ok(())
};
if let Err(print_fail) = error {
// Try to display this error.
let _ = writeln!(std::io::stderr(), "{}: {print_fail}", uucore::util_name());
// Mirror GNU options. When failing to print warnings or version flags, then we exit
// with FAIL. This avoids allocation some error information which may result in yet
// other types of failure.
set_exit_code(1);
}
if let Err(print_fail) = error {
// Try to display this error.
let _ = writeln!(std::io::stderr(), "{}: {print_fail}", uucore::util_name());
// Mirror GNU options. When failing to print warnings or version flags, then we exit
// with FAIL. This avoids allocation some error information which may result in yet
// other types of failure.
set_exit_code(1);
}
Ok(())
+7
View File
@@ -34,6 +34,13 @@ fn test_short_options() {
}
}
#[test]
fn test_extra_args() {
for option in ["--help", "--version"] {
new_ucmd!().args(&[option, "test"]).fails().no_output();
}
}
#[test]
fn test_conflict() {
new_ucmd!()
+7
View File
@@ -37,6 +37,13 @@ fn test_short_options() {
}
}
#[test]
fn test_extra_args() {
for option in ["--help", "--version"] {
new_ucmd!().args(&[option, "test"]).succeeds().no_output();
}
}
#[test]
fn test_conflict() {
new_ucmd!()