Skip to content

Commit 1f6512d

Browse files
oech3cakebaker
authored andcommitted
yes: Refactor by clap's default value
1 parent 01c8d6e commit 1f6512d

1 file changed

Lines changed: 10 additions & 21 deletions

File tree

src/uu/yes/src/yes.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2222
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
2323

2424
let mut buffer = Vec::with_capacity(BUF_SIZE);
25-
args_into_buffer(&mut buffer, matches.get_many::<OsString>("STRING")).unwrap();
25+
#[allow(clippy::unwrap_used, reason = "clap provides 'y' by default")]
26+
let _ = args_into_buffer(&mut buffer, matches.get_many::<OsString>("STRING").unwrap());
2627
prepare_buffer(&mut buffer);
2728

2829
match exec(&buffer) {
@@ -45,6 +46,7 @@ pub fn uu_app() -> Command {
4546
.override_usage(format_usage(&translate!("yes-usage")))
4647
.arg(
4748
Arg::new("STRING")
49+
.default_value("y")
4850
.value_parser(ValueParser::os_string())
4951
.action(ArgAction::Append),
5052
)
@@ -55,13 +57,8 @@ pub fn uu_app() -> Command {
5557
#[allow(clippy::unnecessary_wraps, reason = "needed on some platforms")]
5658
fn args_into_buffer<'a>(
5759
buf: &mut Vec<u8>,
58-
i: Option<impl Iterator<Item = &'a OsString>>,
60+
i: impl Iterator<Item = &'a OsString>,
5961
) -> Result<(), Box<dyn Error>> {
60-
let Some(i) = i else {
61-
buf.extend_from_slice(b"y\n");
62-
return Ok(());
63-
};
64-
6562
// On Unix (and wasi), OsStrs are just &[u8]'s underneath...
6663
#[cfg(any(unix, target_os = "wasi"))]
6764
{
@@ -155,30 +152,22 @@ mod tests {
155152
fn test_args_into_buf() {
156153
{
157154
let mut v = Vec::with_capacity(BUF_SIZE);
158-
args_into_buffer(&mut v, None::<std::slice::Iter<OsString>>).unwrap();
155+
let default_args = ["y".into()];
156+
args_into_buffer(&mut v, default_args.iter()).unwrap();
159157
assert_eq!(String::from_utf8(v).unwrap(), "y\n");
160158
}
161159

162160
{
163161
let mut v = Vec::with_capacity(BUF_SIZE);
164-
args_into_buffer(&mut v, Some([OsString::from("foo")].iter())).unwrap();
162+
let args = ["foo".into()];
163+
args_into_buffer(&mut v, args.iter()).unwrap();
165164
assert_eq!(String::from_utf8(v).unwrap(), "foo\n");
166165
}
167166

168167
{
169168
let mut v = Vec::with_capacity(BUF_SIZE);
170-
args_into_buffer(
171-
&mut v,
172-
Some(
173-
[
174-
OsString::from("foo"),
175-
OsString::from("bar baz"),
176-
OsString::from("qux"),
177-
]
178-
.iter(),
179-
),
180-
)
181-
.unwrap();
169+
let args = ["foo".into(), "bar baz".into(), "qux".into()];
170+
args_into_buffer(&mut v, args.iter()).unwrap();
182171
assert_eq!(String::from_utf8(v).unwrap(), "foo bar baz qux\n");
183172
}
184173
}

0 commit comments

Comments
 (0)