From f40fecf86d9365d17af1f6703cde1f5cbd3b9b03 Mon Sep 17 00:00:00 2001 From: Sam Caldwell Date: Mon, 28 Feb 2022 23:30:29 -0700 Subject: [PATCH] Add UError impl for clap::Error --- src/uu/cp/src/cp.rs | 2 +- src/uucore/src/lib/mods/error.rs | 11 +++++++++++ tests/by-util/test_cp.rs | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index b94953fdf..edd4f1bf2 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -475,7 +475,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { LONG_HELP, backup_control::BACKUP_CONTROL_LONG_HELP )) - .get_matches_from(args); + .try_get_matches_from(args)?; let options = Options::from_matches(&matches)?; diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs index ba7722f1c..15d53233e 100644 --- a/src/uucore/src/lib/mods/error.rs +++ b/src/uucore/src/lib/mods/error.rs @@ -50,6 +50,7 @@ // spell-checker:ignore uioerror +use clap; use std::{ error::Error, fmt::{Display, Formatter}, @@ -615,3 +616,13 @@ impl From for Box { ExitCode::new(i) } } + +/// Implementations for clap::Error +impl UError for clap::Error { + fn code(&self) -> i32 { + match self.kind { + clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => 0, + _ => 1, + } + } +} diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 443e67557..11afa469e 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -190,7 +190,7 @@ fn test_cp_target_directory_is_file() { .arg(TEST_HOW_ARE_YOU_SOURCE) .arg(TEST_HELLO_WORLD_SOURCE) .fails() - .stderr_contains("must specify a directory"); + .stderr_contains(format!("'{}' is not a directory", TEST_HOW_ARE_YOU_SOURCE)); } #[test]