Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/uu/false/src/false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,31 @@ 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
// and unwind through the standard library allocation handling machinery.
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(())
Expand Down
35 changes: 16 additions & 19 deletions src/uu/true/src/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
7 changes: 7 additions & 0 deletions tests/by-util/test_false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
7 changes: 7 additions & 0 deletions tests/by-util/test_true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
Loading