Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: use clap's value parser for goal & width #5333

Merged
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
30 changes: 8 additions & 22 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,8 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)
fmt_opts.use_anti_prefix = true;
};

if let Some(s) = matches.get_one::<String>(OPT_WIDTH) {
fmt_opts.width = match s.parse::<usize>() {
Ok(t) => t,
Err(e) => {
return Err(USimpleError::new(
1,
format!("Invalid WIDTH specification: {}: {}", s.quote(), e),
));
}
};
if let Some(width) = matches.get_one::<usize>(OPT_WIDTH) {
fmt_opts.width = *width;
if fmt_opts.width > MAX_WIDTH {
return Err(USimpleError::new(
1,
Expand All @@ -156,16 +148,8 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)
);
};

if let Some(s) = matches.get_one::<String>(OPT_GOAL) {
fmt_opts.goal = match s.parse::<usize>() {
Ok(t) => t,
Err(e) => {
return Err(USimpleError::new(
1,
format!("Invalid GOAL specification: {}: {}", s.quote(), e),
));
}
};
if let Some(goal) = matches.get_one::<usize>(OPT_GOAL) {
fmt_opts.goal = *goal;
if !matches.contains_id(OPT_WIDTH) {
fmt_opts.width = cmp::max(
fmt_opts.goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO,
Expand Down Expand Up @@ -372,14 +356,16 @@ pub fn uu_app() -> Command {
.short('w')
.long("width")
.help("Fill output lines up to a maximum of WIDTH columns, default 75.")
.value_name("WIDTH"),
.value_name("WIDTH")
.value_parser(clap::value_parser!(usize)),
)
.arg(
Arg::new(OPT_GOAL)
.short('g')
.long("goal")
.help("Goal width, default of 93% of WIDTH. Must be less than WIDTH.")
.value_name("GOAL"),
.value_name("GOAL")
.value_parser(clap::value_parser!(usize)),
)
.arg(
Arg::new(OPT_QUICK)
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ fn test_fmt_width_too_big() {
}
}

#[test]
fn test_fmt_invalid_width() {
for param in ["-w", "--width"] {
new_ucmd!()
.args(&["one-word-per-line.txt", param, "invalid"])
.fails()
.code_is(1)
.stderr_contains("invalid value 'invalid'");
}
}

#[ignore]
#[test]
fn test_fmt_goal() {
Expand All @@ -70,6 +81,17 @@ fn test_fmt_goal_too_big() {
}
}

#[test]
fn test_fmt_invalid_goal() {
for param in ["-g", "--goal"] {
new_ucmd!()
.args(&["one-word-per-line.txt", param, "invalid"])
.fails()
.code_is(1)
.stderr_contains("invalid value 'invalid'");
}
}

#[test]
fn test_fmt_set_goal_not_contain_width() {
for param in ["-g", "--goal"] {
Expand Down
Loading