1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

printf: use clap default help and version

This commit is contained in:
ndd7xv 2022-02-04 21:43:21 -05:00
parent af8bcd8d52
commit 5e790918ef

View file

@ -11,22 +11,13 @@ const VERSION: &str = "version";
const HELP: &str = "help"; const HELP: &str = "help";
const USAGE: &str = "printf FORMATSTRING [ARGUMENT]..."; const USAGE: &str = "printf FORMATSTRING [ARGUMENT]...";
const ABOUT: &str = "Print output based off of the format string and proceeding arguments."; const ABOUT: &str = "Print output based off of the format string and proceeding arguments.";
static LONGHELP_LEAD: &str = "printf const AFTER_HELP: &str = "
USAGE: printf FORMATSTRING [ARGUMENT]...
basic anonymous string templating: basic anonymous string templating:
prints format string at least once, repeating as long as there are remaining arguments prints format string at least once, repeating as long as there are remaining arguments
output prints escaped literals in the format string as character literals output prints escaped literals in the format string as character literals
output replaces anonymous fields with the next unused argument, formatted according to the field. output replaces anonymous fields with the next unused argument, formatted according to the field.
Options:
--help display this help and exit
--version output version information and exit
";
static LONGHELP_BODY: &str = "
Prints the , replacing escaped character sequences with character literals Prints the , replacing escaped character sequences with character literals
and substitution field sequences with passed arguments and substitution field sequences with passed arguments
@ -273,32 +264,36 @@ COPYRIGHT :
"; ";
mod options {
pub const FORMATSTRING: &str = "FORMATSTRING";
pub const ARGUMENT: &str = "ARGUMENT";
}
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
let matches = uu_app().get_matches_from(args);
if args.len() <= 1 { let format_string = matches
return Err(UUsageError::new(1, "missing operand")); .value_of(options::FORMATSTRING)
} .ok_or_else(|| UUsageError::new(1, "missing operand"))?;
let formatstr = &args[1]; let values: Vec<String> = match matches.values_of(options::ARGUMENT) {
Some(s) => s.map(|s| s.to_string()).collect(),
None => vec![],
};
if formatstr == "--help" { memo::Memo::run_all(format_string, &values[..]);
print!("{} {}", LONGHELP_LEAD, LONGHELP_BODY);
} else if formatstr == "--version" {
println!("{} {}", uucore::util_name(), crate_version!());
} else {
let printf_args = &args[2..];
memo::Memo::run_all(formatstr, printf_args);
}
Ok(()) Ok(())
} }
pub fn uu_app<'a>() -> App<'a> { pub fn uu_app<'a>() -> App<'a> {
App::new(uucore::util_name()) App::new(uucore::util_name())
.setting(AppSettings::AllowHyphenValues)
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
.after_help(AFTER_HELP)
.override_usage(USAGE) .override_usage(USAGE)
.arg(Arg::new(HELP).long(HELP).help("Print help information")) .arg(Arg::new(HELP).long(HELP).help("Print help information"))
.arg( .arg(
@ -306,5 +301,6 @@ pub fn uu_app<'a>() -> App<'a> {
.long(VERSION) .long(VERSION)
.help("Print version information"), .help("Print version information"),
) )
.setting(AppSettings::InferLongArgs) .arg(Arg::new(options::FORMATSTRING))
.arg(Arg::new(options::ARGUMENT).multiple_occurrences(true))
} }