Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 7 additions & 39 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,6 @@ mod options {
/// Returns a pair of boolean. The first one indicates if we should use tagged
/// output format, the second one indicates if we should use the binary flag in
/// the untagged case.
fn handle_tag_text_binary_flags<S: AsRef<OsStr>>(
args: impl Iterator<Item = S>,
) -> UResult<(bool, bool)> {
let mut tag = true;
let mut binary = false;
let mut text = false;

// --binary, --tag and --untagged are tight together: none of them
// conflicts with each other but --tag will reset "binary" and "text" and
// set "tag".

for arg in args {
let arg = arg.as_ref();
if arg == "-b" || arg == "--binary" {
text = false;
binary = true;
} else if arg == "--text" {
text = true;
binary = false;
} else if arg == "--tag" {
tag = true;
binary = false;
text = false;
} else if arg == "--untagged" {
tag = false;
}
}

// Specifying --text without ever mentioning --untagged fails.
if text && tag {
return Err(ChecksumError::TextWithoutUntagged.into());
}

Ok((tag, binary))
}

/// Sanitize the `--length` argument depending on `--algorithm` and `--length`.
fn maybe_sanitize_length(
algo_cli: Option<AlgoKind>,
Expand Down Expand Up @@ -208,7 +172,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Set the default algorithm to CRC when not '--check'ing.
let algo_kind = algo_cli.unwrap_or(AlgoKind::Crc);

let (tag, binary) = handle_tag_text_binary_flags(std::env::args_os())?;
let tag = matches.get_flag(options::TAG) || !matches.get_flag(options::UNTAGGED);
let binary = matches.get_flag(options::BINARY);

let algo = SizedAlgoKind::from_unsized(algo_kind, length)?;
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
Expand Down Expand Up @@ -265,7 +230,9 @@ pub fn uu_app() -> Command {
.long(options::TAG)
.help(translate!("cksum-help-tag"))
.action(ArgAction::SetTrue)
.overrides_with(options::UNTAGGED),
.overrides_with(options::UNTAGGED)
.overrides_with(options::BINARY)
.overrides_with(options::TEXT),
)
.arg(
Arg::new(options::LENGTH)
Expand Down Expand Up @@ -308,7 +275,8 @@ pub fn uu_app() -> Command {
.short('t')
.hide(true)
.overrides_with(options::BINARY)
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.requires(options::UNTAGGED),
)
.arg(
Arg::new(options::BINARY)
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ mod output_format {
.args(&["-a", "md5"])
.arg(at.subdir.join("f"))
.fails_with_code(1)
.stderr_contains("--text mode is only supported with --untagged");
.stderr_contains("the following required arguments were not provided"); //clap does not change the meaning
}

#[test]
Expand Down
Loading