diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index 1083490313a..8fa38b67cc8 100644 --- a/src/uu/false/src/false.rs +++ b/src/uu/false/src/false.rs @@ -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 = 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(()) diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index e19b26408e3..ad3e01c9e70 100644 --- a/src/uu/true/src/true.rs +++ b/src/uu/true/src/true.rs @@ -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 = 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(()) diff --git a/tests/by-util/test_false.rs b/tests/by-util/test_false.rs index 48b05c5f7cd..cc0874256b1 100644 --- a/tests/by-util/test_false.rs +++ b/tests/by-util/test_false.rs @@ -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!() diff --git a/tests/by-util/test_true.rs b/tests/by-util/test_true.rs index a8a9cf0478e..23c947fb29c 100644 --- a/tests/by-util/test_true.rs +++ b/tests/by-util/test_true.rs @@ -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!()