Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/uu/numfmt/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ numfmt-after-help = UNIT options:
Optional precision (%.1f) will override the input determined precision.

# Help messages
numfmt-help-debug = print warnings about invalid input
numfmt-help-delimiter = use X instead of whitespace for field delimiter
numfmt-help-field = replace the numbers in these input fields; see FIELDS below
numfmt-help-format = use printf style floating-point FORMAT; see FORMAT below for details
Expand Down Expand Up @@ -74,3 +75,7 @@ numfmt-error-invalid-format-width-overflow = invalid format '{ $format }' (width
numfmt-error-invalid-precision = invalid precision in format '{ $format }'
numfmt-error-format-too-many-percent = format '{ $format }' has too many % directives
numfmt-error-unknown-invalid-mode = Unknown invalid mode: { $mode }

# Debug messages
numfmt-debug-no-conversion = no conversion option specified
numfmt-debug-header-ignored = --header ignored with command-line input
29 changes: 29 additions & 0 deletions src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {

let zero_terminated = args.get_flag(ZERO_TERMINATED);

let debug = args.get_flag(DEBUG);

Ok(NumfmtOptions {
transform,
padding,
Expand All @@ -274,15 +276,35 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
format,
invalid,
zero_terminated,
debug,
})
}

fn print_debug_warnings(options: &NumfmtOptions, matches: &ArgMatches) {
// Warn if no conversion option is specified
if options.transform.from == Unit::None
&& options.transform.to == Unit::None
&& options.padding == 0
{
show_error!("{}", translate!("numfmt-debug-no-conversion"));
}

// Warn if --header is used with command-line input
if options.header > 0 && matches.get_many::<OsString>(NUMBER).is_some() {
show_error!("{}", translate!("numfmt-debug-header-ignored"));
}
}

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

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

if options.debug {
print_debug_warnings(&options, &matches);
}

let result = match matches.get_many::<OsString>(NUMBER) {
Some(values) => {
let byte_args: Vec<&[u8]> = values
Expand Down Expand Up @@ -316,6 +338,12 @@ pub fn uu_app() -> Command {
.override_usage(format_usage(&translate!("numfmt-usage")))
.allow_negative_numbers(true)
.infer_long_args(true)
.arg(
Arg::new(DEBUG)
.long(DEBUG)
.help(translate!("numfmt-help-debug"))
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(DELIMITER)
.short('d')
Expand Down Expand Up @@ -466,6 +494,7 @@ mod tests {
format: FormatOptions::default(),
invalid: InvalidModes::Abort,
zero_terminated: false,
debug: false,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/uu/numfmt/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::units::Unit;
use uucore::ranges::Range;
use uucore::translate;

pub const DEBUG: &str = "debug";
pub const DELIMITER: &str = "delimiter";
pub const FIELD: &str = "field";
pub const FIELD_DEFAULT: &str = "1";
Expand Down Expand Up @@ -57,6 +58,7 @@ pub struct NumfmtOptions {
pub format: FormatOptions,
pub invalid: InvalidModes,
pub zero_terminated: bool,
pub debug: bool,
}

#[derive(Clone, Copy)]
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,3 +1181,23 @@ fn test_unit_separator() {
new_ucmd!().args(args).succeeds().stdout_only(expected);
}
}

#[test]
fn test_debug_warnings() {
new_ucmd!()
.args(&["--debug", "4096"])
.succeeds()
.stdout_is("4096\n")
.stderr_is("numfmt: no conversion option specified\n");

new_ucmd!()
.args(&["--debug", "--padding=10", "4096"])
.succeeds()
.stdout_only(" 4096\n");

new_ucmd!()
.args(&["--debug", "--header", "--to=iec", "4096"])
.succeeds()
.stdout_is("4.0K\n")
.stderr_is("numfmt: --header ignored with command-line input\n");
}
Loading