mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
printf: show warning message in case of excess arguments
This commit is contained in:
parent
64476fd9da
commit
ae6cb8fed3
2 changed files with 14 additions and 2 deletions
|
@ -7,7 +7,7 @@ use std::io::stdout;
|
||||||
use std::ops::ControlFlow;
|
use std::ops::ControlFlow;
|
||||||
use uucore::error::{UResult, UUsageError};
|
use uucore::error::{UResult, UUsageError};
|
||||||
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
|
use uucore::format::{parse_spec_and_escape, FormatArgument, FormatItem};
|
||||||
use uucore::{format_usage, help_about, help_section, help_usage};
|
use uucore::{format_usage, help_about, help_section, help_usage, show_warning};
|
||||||
|
|
||||||
const VERSION: &str = "version";
|
const VERSION: &str = "version";
|
||||||
const HELP: &str = "help";
|
const HELP: &str = "help";
|
||||||
|
@ -48,6 +48,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
// Without format specs in the string, the iter would not consume any args,
|
// Without format specs in the string, the iter would not consume any args,
|
||||||
// leading to an infinite loop. Thus, we exit early.
|
// leading to an infinite loop. Thus, we exit early.
|
||||||
if !format_seen {
|
if !format_seen {
|
||||||
|
if let Some(arg) = args.next() {
|
||||||
|
use FormatArgument::*;
|
||||||
|
let Unparsed(arg_str) = arg else {
|
||||||
|
unreachable!("All args are transformed to Unparsed")
|
||||||
|
};
|
||||||
|
show_warning!("ignoring excess arguments, starting with '{arg_str}'");
|
||||||
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,6 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -679,7 +679,11 @@ fn char_as_byte() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_infinite_loop() {
|
fn no_infinite_loop() {
|
||||||
new_ucmd!().args(&["a", "b"]).succeeds().stdout_only("a");
|
new_ucmd!()
|
||||||
|
.args(&["a", "b"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("a")
|
||||||
|
.stderr_contains("warning: ignoring excess arguments, starting with 'b'");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue