mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
fmt: use clap's value parser for goal & width
This commit is contained in:
parent
2789885648
commit
51eb20a15d
2 changed files with 30 additions and 22 deletions
|
@ -131,16 +131,8 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)
|
||||||
fmt_opts.use_anti_prefix = true;
|
fmt_opts.use_anti_prefix = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(s) = matches.get_one::<String>(OPT_WIDTH) {
|
if let Some(width) = matches.get_one::<usize>(OPT_WIDTH) {
|
||||||
fmt_opts.width = match s.parse::<usize>() {
|
fmt_opts.width = *width;
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("Invalid WIDTH specification: {}: {}", s.quote(), e),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if fmt_opts.width > MAX_WIDTH {
|
if fmt_opts.width > MAX_WIDTH {
|
||||||
return Err(USimpleError::new(
|
return Err(USimpleError::new(
|
||||||
1,
|
1,
|
||||||
|
@ -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) {
|
if let Some(goal) = matches.get_one::<usize>(OPT_GOAL) {
|
||||||
fmt_opts.goal = match s.parse::<usize>() {
|
fmt_opts.goal = *goal;
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("Invalid GOAL specification: {}: {}", s.quote(), e),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if !matches.contains_id(OPT_WIDTH) {
|
if !matches.contains_id(OPT_WIDTH) {
|
||||||
fmt_opts.width = cmp::max(
|
fmt_opts.width = cmp::max(
|
||||||
fmt_opts.goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO,
|
fmt_opts.goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO,
|
||||||
|
@ -372,14 +356,16 @@ pub fn uu_app() -> Command {
|
||||||
.short('w')
|
.short('w')
|
||||||
.long("width")
|
.long("width")
|
||||||
.help("Fill output lines up to a maximum of WIDTH columns, default 75.")
|
.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(
|
||||||
Arg::new(OPT_GOAL)
|
Arg::new(OPT_GOAL)
|
||||||
.short('g')
|
.short('g')
|
||||||
.long("goal")
|
.long("goal")
|
||||||
.help("Goal width, default of 93% of WIDTH. Must be less than WIDTH.")
|
.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(
|
||||||
Arg::new(OPT_QUICK)
|
Arg::new(OPT_QUICK)
|
||||||
|
|
|
@ -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]
|
#[ignore]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fmt_goal() {
|
fn test_fmt_goal() {
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn test_fmt_set_goal_not_contain_width() {
|
fn test_fmt_set_goal_not_contain_width() {
|
||||||
for param in ["-g", "--goal"] {
|
for param in ["-g", "--goal"] {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue