Skip to content

Commit 58f146a

Browse files
committed
feat(uucore): Add localization for options with default
Reproduces the same behavior as `.help()` with the "default" keyword localized. I also chosen to add brackets between default values for lisibility.
1 parent 8c5f2cc commit 58f146a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
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: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use crate::error::{UResult, USimpleError};
1515
use crate::locale::translate;
1616

17-
use clap::builder::StyledStr;
17+
use clap::builder::{IntoResettable, Resettable, StyledStr};
1818
use clap::error::{ContextKind, ErrorKind};
1919
use clap::{Arg, ArgAction, ArgMatches, Command, Error};
2020

@@ -655,6 +655,47 @@ impl CommandHelpLocalization for Command {
655655
}
656656
}
657657

658+
pub trait ArgHelpLocalization {
659+
/// Add translation of "default" keyword for options with defaults.
660+
/// Also support options without defaults with the same output as normal .help()
661+
fn help_with_localized_default(self, description: impl IntoResettable<StyledStr>) -> Self;
662+
}
663+
664+
impl ArgHelpLocalization for Arg {
665+
fn help_with_localized_default(self, description: impl IntoResettable<StyledStr>) -> Self {
666+
use std::fmt::Write;
667+
668+
let mut template = clap::builder::StyledStr::new();
669+
let mut has_description = false;
670+
671+
// Manually extract the Option from Resettable because ".into_option()" is private
672+
if let Resettable::Value(description_str) = description.into_resettable() {
673+
write!(template, "{description_str}").unwrap();
674+
has_description = true;
675+
}
676+
677+
let defaults: Vec<String> = self
678+
.get_default_values()
679+
.iter()
680+
.filter_map(|v| v.to_str().map(String::from))
681+
.map(|s| format!("\"{s}\""))
682+
.collect();
683+
if defaults.is_empty() {
684+
self.help(template)
685+
} else {
686+
if has_description {
687+
write!(template, " ").unwrap();
688+
}
689+
690+
let default_label = translate!("common-default");
691+
let defaults_str = defaults.join(" ");
692+
write!(template, "[{default_label}: {defaults_str}]").unwrap();
693+
// Only hide default value if replaced otherwise clap throw an error
694+
self.hide_default_value(true).help(template)
695+
}
696+
}
697+
}
698+
658699
/* spell-checker: disable */
659700
#[cfg(test)]
660701
mod tests {

0 commit comments

Comments
 (0)