diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index 685363f8f..242dd416a 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -115,7 +115,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app() .override_usage(&usage[..]) .after_help(&long_usage[..]) - .get_matches_from(args); + .try_get_matches_from(args) + .map_err(|e| { + e.print().expect("Error writing clap::Error"); + match e.kind { + clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => 0, + _ => 1, + } + })?; let files: Vec = matches .values_of(options::ARG_FILES) diff --git a/tests/by-util/test_truncate.rs b/tests/by-util/test_truncate.rs index 0ef65ec16..c1e44f605 100644 --- a/tests/by-util/test_truncate.rs +++ b/tests/by-util/test_truncate.rs @@ -250,11 +250,24 @@ fn test_size_and_reference() { #[test] fn test_error_filename_only() { // truncate: you must specify either '--size' or '--reference' - new_ucmd!().args(&["file"]).fails().stderr_contains( - "error: The following required arguments were not provided: + new_ucmd!() + .args(&["file"]) + .fails() + .code_is(1) + .stderr_contains( + "error: The following required arguments were not provided: --reference --size ", - ); + ); +} + +#[test] +fn test_invalid_option() { + // truncate: cli parsing error returns 1 + new_ucmd!() + .args(&["--this-arg-does-not-exist"]) + .fails() + .code_is(1); } #[test]