Skip to content

Commit 2ca1a10

Browse files
authored
numfmt: fix --to=auto to return with exitcode=1 (#11701)
* numfmt: fix --to=auto to return with exitcode=1 * numfmt: update direct variable use in format macro * numfmt_test: remove extra test * numfmt: replacing format in translate directly
1 parent 2ab8e25 commit 2ca1a10

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

src/uu/numfmt/locales/en-US.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ numfmt-error-invalid-number = invalid number: { $input }
6868
numfmt-error-missing-i-suffix = missing 'i' suffix in input: '{ $number }{ $suffix }' (e.g Ki/Mi/Gi)
6969
numfmt-error-rejecting-suffix = rejecting suffix in input: '{ $number }{ $suffix }' (consider using --from)
7070
numfmt-error-suffix-unsupported-for-unit = This suffix is unsupported for specified unit
71-
numfmt-error-unit-auto-not-supported-with-to = Unit 'auto' isn't supported with --to options
71+
numfmt-error-invalid-unit-argument = invalid argument '{$arg}' for '{$opt}'
7272
numfmt-error-number-too-big = Number is too big and unsupported
7373
numfmt-error-format-no-percent = format '{ $format }' has no % directive
7474
numfmt-error-format-ends-in-percent = format '{ $format }' ends in %

src/uu/numfmt/src/format.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,11 @@ fn consider_suffix(
475475
let (bases, with_i) = match u {
476476
Unit::Si => (si_bases_f64(), false),
477477
Unit::Iec(with_i) => (iec_bases_f64(), with_i),
478-
Unit::Auto => return Err(translate!("numfmt-error-unit-auto-not-supported-with-to")),
478+
Unit::Auto => {
479+
return Err(
480+
translate!("numfmt-error-invalid-unit-argument", "arg" => "auto", "opt" => "--to"),
481+
);
482+
}
479483
Unit::None => return Ok((n, None)),
480484
};
481485

src/uu/numfmt/src/numfmt.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,16 @@ fn handle_buffer<R: BufRead>(mut input: R, options: &NumfmtOptions) -> UResult<b
165165
Ok(saw_invalid)
166166
}
167167

168-
fn parse_unit(s: &str) -> Result<Unit> {
168+
fn parse_unit(s: &str, opt: &str) -> Result<Unit> {
169169
match s {
170-
"auto" => Ok(Unit::Auto),
170+
"auto" if opt != TO => Ok(Unit::Auto),
171171
"si" => Ok(Unit::Si),
172172
"iec" => Ok(Unit::Iec(false)),
173173
"iec-i" => Ok(Unit::Iec(true)),
174174
"none" => Ok(Unit::None),
175-
_ => Err(translate!("numfmt-error-unsupported-unit")),
175+
value => Err(
176+
translate!("numfmt-error-invalid-unit-argument", "arg" => value, "opt" => format!("--{opt}")),
177+
),
176178
}
177179
}
178180

@@ -237,8 +239,8 @@ fn parse_delimiter(arg: &OsString) -> Result<Vec<u8>> {
237239
}
238240

239241
fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
240-
let from = parse_unit(args.get_one::<String>(FROM).unwrap())?;
241-
let to = parse_unit(args.get_one::<String>(TO).unwrap())?;
242+
let from = parse_unit(args.get_one::<String>(FROM).unwrap(), FROM)?;
243+
let to = parse_unit(args.get_one::<String>(TO).unwrap(), TO)?;
242244
let from_unit = parse_unit_size(args.get_one::<String>(FROM_UNIT).unwrap())?;
243245
let to_unit = parse_unit_size(args.get_one::<String>(TO_UNIT).unwrap())?;
244246

tests/by-util/test_numfmt.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,12 +1411,8 @@ fn test_scientific_notation_rejected_by_gnu_issue_11655() {
14111411
.stderr_contains("invalid suffix in input");
14121412
}
14131413

1414-
// https://github.com/uutils/coreutils/issues/11662
1415-
// `--to=auto` is accepted at parse time by uutils then rejected at runtime
1416-
// with exit code 2; GNU rejects it in option parsing with exit code 1.
14171414
#[test]
1418-
#[ignore = "GNU compat: see uutils/coreutils#11662"]
1419-
fn test_to_auto_rejected_at_parse_time_issue_11662() {
1415+
fn test_to_auto_rejected_at_parse_time() {
14201416
new_ucmd!()
14211417
.args(&["--to=auto", "100"])
14221418
.fails_with_code(1)

0 commit comments

Comments
 (0)