Skip to content

Commit

Permalink
Merge pull request uutils#5446 from cakebaker/ls_try_get_matches_from
Browse files Browse the repository at this point in the history
ls: use try_get_matches_from instead of get_matches_from
  • Loading branch information
tertsdiepraam authored Oct 25, 2023
2 parents f76522e + fd18d26 commit 96d0830
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>),
}
Expand All @@ -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(_, _) => 2,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1027,7 +1034,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)?;

Expand Down Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3569,6 +3569,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!());
Expand Down

0 comments on commit 96d0830

Please sign in to comment.