From d7b7dfeb16742f9855155d9ea413f56b3758c5c6 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 24 Oct 2023 10:07:28 +0200 Subject: [PATCH 1/2] ls: use try_get_matches_from instead of get_matches_from to replace clap's exit code --- src/uu/ls/src/ls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 41d2f59b1..8ecf7982a 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -1027,7 +1027,7 @@ impl Config { pub fn uumain(args: impl uucore::Args) -> UResult<()> { let command = uu_app(); - let matches = command.get_matches_from(args); + let matches = command.try_get_matches_from(args)?; let config = Config::from(&matches)?; From fd18d2686f4d0a28dcf95a1db0f419cc82db699e Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 24 Oct 2023 14:48:24 +0200 Subject: [PATCH 2/2] ls: return exit code 2 for -l --dired --zero --- src/uu/ls/src/ls.rs | 18 ++++++++++++------ tests/by-util/test_ls.rs | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 8ecf7982a..db7977412 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -168,7 +168,8 @@ enum LsError { IOError(std::io::Error), IOErrorContext(std::io::Error, PathBuf, bool), BlockSizeParseError(String), - ConflictingArgumentDired(), + ConflictingArgumentDired, + DiredAndZeroAreIncompatible, AlreadyListedError(PathBuf), TimeStyleParseError(String, Vec), } @@ -181,7 +182,8 @@ impl UError for LsError { Self::IOErrorContext(_, _, false) => 1, Self::IOErrorContext(_, _, true) => 2, Self::BlockSizeParseError(_) => 1, - Self::ConflictingArgumentDired() => 1, + Self::ConflictingArgumentDired => 1, + Self::DiredAndZeroAreIncompatible => 2, Self::AlreadyListedError(_) => 2, Self::TimeStyleParseError(_, _) => 1, } @@ -196,10 +198,12 @@ impl Display for LsError { Self::BlockSizeParseError(s) => { write!(f, "invalid --block-size argument {}", s.quote()) } - Self::ConflictingArgumentDired() => { + Self::ConflictingArgumentDired => { write!(f, "--dired requires --format=long") } - + Self::DiredAndZeroAreIncompatible => { + write!(f, "--dired and --zero are incompatible") + } Self::TimeStyleParseError(s, possible_time_styles) => { write!( f, @@ -966,7 +970,10 @@ impl Config { let dired = options.get_flag(options::DIRED); if dired && format != Format::Long { - return Err(Box::new(LsError::ConflictingArgumentDired())); + return Err(Box::new(LsError::ConflictingArgumentDired)); + } + if dired && format == Format::Long && options.get_flag(options::ZERO) { + return Err(Box::new(LsError::DiredAndZeroAreIncompatible)); } let dereference = if options.get_flag(options::dereference::ALL) { @@ -1142,7 +1149,6 @@ pub fn uu_app() -> Command { .arg( Arg::new(options::ZERO) .long(options::ZERO) - .conflicts_with(options::DIRED) .overrides_with(options::ZERO) .help("List entries separated by ASCII NUL characters.") .action(ArgAction::SetTrue), diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 15893b0e2..181c385e7 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3564,6 +3564,20 @@ fn test_ls_dired_incompatible() { .stderr_contains("--dired requires --format=long"); } +#[test] +fn test_ls_dired_and_zero_are_incompatible() { + let scene = TestScenario::new(util_name!()); + + scene + .ucmd() + .arg("--dired") + .arg("-l") + .arg("--zero") + .fails() + .code_is(2) + .stderr_contains("--dired and --zero are incompatible"); +} + #[test] fn test_ls_dired_recursive() { let scene = TestScenario::new(util_name!());