Skip to content

Commit 6a85ee3

Browse files
oech3oech3
authored andcommitted
cksum: Move handle_tag_text_binary_flags to clap
1 parent 23f1a1e commit 6a85ee3

File tree

2 files changed

+16
-46
lines changed

2 files changed

+16
-46
lines changed

src/uu/cksum/src/cksum.rs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -74,42 +74,6 @@ mod options {
7474
/// Returns a pair of boolean. The first one indicates if we should use tagged
7575
/// output format, the second one indicates if we should use the binary flag in
7676
/// the untagged case.
77-
fn handle_tag_text_binary_flags<S: AsRef<OsStr>>(
78-
args: impl Iterator<Item = S>,
79-
) -> UResult<(bool, bool)> {
80-
let mut tag = true;
81-
let mut binary = false;
82-
let mut text = false;
83-
84-
// --binary, --tag and --untagged are tight together: none of them
85-
// conflicts with each other but --tag will reset "binary" and "text" and
86-
// set "tag".
87-
88-
for arg in args {
89-
let arg = arg.as_ref();
90-
if arg == "-b" || arg == "--binary" {
91-
text = false;
92-
binary = true;
93-
} else if arg == "--text" {
94-
text = true;
95-
binary = false;
96-
} else if arg == "--tag" {
97-
tag = true;
98-
binary = false;
99-
text = false;
100-
} else if arg == "--untagged" {
101-
tag = false;
102-
}
103-
}
104-
105-
// Specifying --text without ever mentioning --untagged fails.
106-
if text && tag {
107-
return Err(ChecksumError::TextWithoutUntagged.into());
108-
}
109-
110-
Ok((tag, binary))
111-
}
112-
11377
/// Sanitize the `--length` argument depending on `--algorithm` and `--length`.
11478
fn maybe_sanitize_length(
11579
algo_cli: Option<AlgoKind>,
@@ -208,7 +172,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
208172
// Set the default algorithm to CRC when not '--check'ing.
209173
let algo_kind = algo_cli.unwrap_or(AlgoKind::Crc);
210174

211-
let (tag, binary) = handle_tag_text_binary_flags(std::env::args_os())?;
175+
let tag = matches.get_flag(options::TAG);
176+
let binary = matches.get_flag(options::BINARY);
212177

213178
let algo = SizedAlgoKind::from_unsized(algo_kind, length)?;
214179
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
@@ -253,20 +218,24 @@ pub fn uu_app() -> Command {
253218
.value_name("ALGORITHM")
254219
.value_parser(SUPPORTED_ALGORITHMS),
255220
)
221+
.arg(
222+
Arg::new(options::TAG)
223+
.long(options::TAG)
224+
.help(translate!("cksum-help-tag"))
225+
.action(ArgAction::SetTrue)
226+
.overrides_with(options::UNTAGGED)
227+
.overrides_with(options::BINARY)
228+
.overrides_with(options::TEXT)
229+
.default_value("true")
230+
.overrides_with_all(&[options::UNTAGGED, options::BINARY, options::TEXT]),
231+
)
256232
.arg(
257233
Arg::new(options::UNTAGGED)
258234
.long(options::UNTAGGED)
259235
.help(translate!("cksum-help-untagged"))
260236
.action(ArgAction::SetTrue)
261237
.overrides_with(options::TAG),
262238
)
263-
.arg(
264-
Arg::new(options::TAG)
265-
.long(options::TAG)
266-
.help(translate!("cksum-help-tag"))
267-
.action(ArgAction::SetTrue)
268-
.overrides_with(options::UNTAGGED),
269-
)
270239
.arg(
271240
Arg::new(options::LENGTH)
272241
.long(options::LENGTH)
@@ -308,7 +277,8 @@ pub fn uu_app() -> Command {
308277
.short('t')
309278
.hide(true)
310279
.overrides_with(options::BINARY)
311-
.action(ArgAction::SetTrue),
280+
.action(ArgAction::SetTrue)
281+
.requires(options::UNTAGGED),
312282
)
313283
.arg(
314284
Arg::new(options::BINARY)

tests/by-util/test_cksum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ mod output_format {
10661066
.args(&["-a", "md5"])
10671067
.arg(at.subdir.join("f"))
10681068
.fails_with_code(1)
1069-
.stderr_contains("--text mode is only supported with --untagged");
1069+
.stderr_contains("the following required arguments were not provided"); //clap does not change the meaning
10701070
}
10711071

10721072
#[test]

0 commit comments

Comments
 (0)