Merge pull request #5446 from cakebaker/ls_try_get_matches_from

ls: use try_get_matches_from instead of get_matches_from
This commit is contained in:
Terts Diepraam 2023-10-25 11:25:40 +02:00 committed by GitHub
commit 96d0830952
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View file

@ -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>),
}
@ -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,
}
@ -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) {
@ -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)?;
@ -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),

View file

@ -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!());