Skip to content

Commit fb6a2bb

Browse files
authored
numfmt: add --debug flag to print warnings about invalid input (#10110)
1 parent cb0ae79 commit fb6a2bb

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ numfmt-after-help = UNIT options:
3636
Optional precision (%.1f) will override the input determined precision.
3737
3838
# Help messages
39+
numfmt-help-debug = print warnings about invalid input
3940
numfmt-help-delimiter = use X instead of whitespace for field delimiter
4041
numfmt-help-field = replace the numbers in these input fields; see FIELDS below
4142
numfmt-help-format = use printf style floating-point FORMAT; see FORMAT below for details
@@ -74,3 +75,7 @@ numfmt-error-invalid-format-width-overflow = invalid format '{ $format }' (width
7475
numfmt-error-invalid-precision = invalid precision in format '{ $format }'
7576
numfmt-error-format-too-many-percent = format '{ $format }' has too many % directives
7677
numfmt-error-unknown-invalid-mode = Unknown invalid mode: { $mode }
78+
79+
# Debug messages
80+
numfmt-debug-no-conversion = no conversion option specified
81+
numfmt-debug-header-ignored = --header ignored with command-line input

src/uu/numfmt/src/numfmt.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
262262

263263
let zero_terminated = args.get_flag(ZERO_TERMINATED);
264264

265+
let debug = args.get_flag(DEBUG);
266+
265267
Ok(NumfmtOptions {
266268
transform,
267269
padding,
@@ -274,15 +276,35 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
274276
format,
275277
invalid,
276278
zero_terminated,
279+
debug,
277280
})
278281
}
279282

283+
fn print_debug_warnings(options: &NumfmtOptions, matches: &ArgMatches) {
284+
// Warn if no conversion option is specified
285+
if options.transform.from == Unit::None
286+
&& options.transform.to == Unit::None
287+
&& options.padding == 0
288+
{
289+
show_error!("{}", translate!("numfmt-debug-no-conversion"));
290+
}
291+
292+
// Warn if --header is used with command-line input
293+
if options.header > 0 && matches.get_many::<OsString>(NUMBER).is_some() {
294+
show_error!("{}", translate!("numfmt-debug-header-ignored"));
295+
}
296+
}
297+
280298
#[uucore::main]
281299
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
282300
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
283301

284302
let options = parse_options(&matches).map_err(NumfmtError::IllegalArgument)?;
285303

304+
if options.debug {
305+
print_debug_warnings(&options, &matches);
306+
}
307+
286308
let result = match matches.get_many::<OsString>(NUMBER) {
287309
Some(values) => {
288310
let byte_args: Vec<&[u8]> = values
@@ -316,6 +338,12 @@ pub fn uu_app() -> Command {
316338
.override_usage(format_usage(&translate!("numfmt-usage")))
317339
.allow_negative_numbers(true)
318340
.infer_long_args(true)
341+
.arg(
342+
Arg::new(DEBUG)
343+
.long(DEBUG)
344+
.help(translate!("numfmt-help-debug"))
345+
.action(ArgAction::SetTrue),
346+
)
319347
.arg(
320348
Arg::new(DELIMITER)
321349
.short('d')
@@ -466,6 +494,7 @@ mod tests {
466494
format: FormatOptions::default(),
467495
invalid: InvalidModes::Abort,
468496
zero_terminated: false,
497+
debug: false,
469498
}
470499
}
471500

src/uu/numfmt/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::units::Unit;
88
use uucore::ranges::Range;
99
use uucore::translate;
1010

11+
pub const DEBUG: &str = "debug";
1112
pub const DELIMITER: &str = "delimiter";
1213
pub const FIELD: &str = "field";
1314
pub const FIELD_DEFAULT: &str = "1";
@@ -57,6 +58,7 @@ pub struct NumfmtOptions {
5758
pub format: FormatOptions,
5859
pub invalid: InvalidModes,
5960
pub zero_terminated: bool,
61+
pub debug: bool,
6062
}
6163

6264
#[derive(Clone, Copy)]

tests/by-util/test_numfmt.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,3 +1181,23 @@ fn test_unit_separator() {
11811181
new_ucmd!().args(args).succeeds().stdout_only(expected);
11821182
}
11831183
}
1184+
1185+
#[test]
1186+
fn test_debug_warnings() {
1187+
new_ucmd!()
1188+
.args(&["--debug", "4096"])
1189+
.succeeds()
1190+
.stdout_is("4096\n")
1191+
.stderr_is("numfmt: no conversion option specified\n");
1192+
1193+
new_ucmd!()
1194+
.args(&["--debug", "--padding=10", "4096"])
1195+
.succeeds()
1196+
.stdout_only(" 4096\n");
1197+
1198+
new_ucmd!()
1199+
.args(&["--debug", "--header", "--to=iec", "4096"])
1200+
.succeeds()
1201+
.stdout_is("4.0K\n")
1202+
.stderr_is("numfmt: --header ignored with command-line input\n");
1203+
}

0 commit comments

Comments
 (0)