Skip to content

Commit e4f68b8

Browse files
committed
feat(uucore): Add localized help function for type Arg
Support Arg variables with or without a default value. Only the word "Default" is localized and values are quoted for readability.
1 parent ea9b9b9 commit e4f68b8

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/uucore/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ common-usage = Usage
1010
common-arguments = Arguments
1111
common-options = Options
1212
common-subcommands = Subcommands
13+
common-default = Default
1314
1415
# Common clap error messages
1516
clap-error-unexpected-argument = { $error_word }: unexpected argument '{ $arg }' found

src/uucore/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ common-usage = Utilisation
1010
common-arguments = Arguments
1111
common-options = Options
1212
common-subcommands = Sous-commandes
13+
common-default = Défaut
1314
1415
# Messages d'erreur clap communs
1516
clap-error-unexpected-argument = { $error_word } : argument inattendu '{ $arg }' trouvé

src/uucore/src/lib/mods/clap_localization.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use crate::error::UResult;
1515
use crate::locale::translate;
1616

17+
use clap::builder::{IntoResettable, Resettable, StyledStr};
1718
use clap::error::{ContextKind, ErrorKind};
1819
use clap::{Arg, ArgAction, ArgMatches, Command, Error};
1920

@@ -556,6 +557,49 @@ impl CommandHelpLocalization for Command {
556557
}
557558
}
558559

560+
pub trait ArgHelpLocalization {
561+
/// Add translation of "default" keyword for options with defaults.
562+
/// Also support options without defaults with the same output as normal .help()
563+
fn help_localized(self, description: impl IntoResettable<StyledStr>) -> Self;
564+
}
565+
566+
impl ArgHelpLocalization for Arg {
567+
fn help_localized(self, description: impl IntoResettable<StyledStr>) -> Self {
568+
use std::fmt::Write;
569+
570+
let mut template = clap::builder::StyledStr::new();
571+
let mut has_description = false;
572+
573+
// Manually extract the Option from Resettable because ".into_option()" is private
574+
if let Resettable::Value(description_str) = description.into_resettable() {
575+
write!(template, "{}", description_str).unwrap();
576+
has_description = true;
577+
}
578+
579+
let defaults: Vec<String> = self
580+
.get_default_values()
581+
.iter()
582+
.filter_map(|v| v.to_str().map(String::from))
583+
.map(|s| format!("\"{}\"", s))
584+
.collect();
585+
if !defaults.is_empty() {
586+
if has_description {
587+
// Space between description and [Default: ...]
588+
write!(template, " ").unwrap();
589+
}
590+
591+
let default_label = translate!("common-default");
592+
let defaults_str = defaults.join(" ");
593+
write!(template, "[{default_label}: {defaults_str}]").unwrap();
594+
595+
// Only hide default value if replaced otherwise clap throw an error
596+
self.hide_default_value(true).help(template)
597+
} else {
598+
self.help(template)
599+
}
600+
}
601+
}
602+
559603
/* spell-checker: disable */
560604
#[cfg(test)]
561605
mod tests {

0 commit comments

Comments
 (0)