Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0caf5fc

Browse files
committed
clap: improve the clap support + add tests
1 parent afd6390 commit 0caf5fc

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,27 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
115115
ErrorKind::UnknownArgument => {
116116
// Force localization initialization - ignore any previous failures
117117
crate::locale::setup_localization_with_common(util_name).ok();
118-
118+
119119
// UnknownArgument gets special handling for suggestions, but should still show simple help
120120
if let Some(invalid_arg) = err.get(ContextKind::InvalidArg) {
121121
let arg_str = invalid_arg.to_string();
122122

123123
// Get the uncolored words from common strings with fallbacks
124124
let error_word = {
125125
let translated = translate!("common-error");
126-
if translated == "common-error" { "error".to_string() } else { translated }
126+
if translated == "common-error" {
127+
"error".to_string()
128+
} else {
129+
translated
130+
}
127131
};
128132
let tip_word = {
129133
let translated = translate!("common-tip");
130-
if translated == "common-tip" { "tip".to_string() } else { translated }
134+
if translated == "common-tip" {
135+
"tip".to_string()
136+
} else {
137+
translated
138+
}
131139
};
132140

133141
let colored_arg = maybe_colorize(&arg_str, Color::Yellow);
@@ -142,15 +150,18 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
142150
"error_word" => colored_error_word.clone()
143151
);
144152
if translated.starts_with("clap-error-unexpected-argument") {
145-
format!("{}: unexpected argument '{}' found", colored_error_word, colored_arg)
153+
format!(
154+
"{}: unexpected argument '{}' found",
155+
colored_error_word, colored_arg
156+
)
146157
} else {
147158
translated
148159
}
149160
};
150161
eprintln!("{error_msg}");
151162
eprintln!();
152163

153-
// Show suggestion if available
164+
// Show suggestion if available
154165
let suggestion = err.get(ContextKind::SuggestedArg);
155166
if let Some(suggested_arg) = suggestion {
156167
let colored_suggestion =
@@ -162,7 +173,10 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
162173
"suggestion" => colored_suggestion.clone()
163174
);
164175
if translated.starts_with("clap-error-similar-argument") {
165-
format!(" {}: a similar argument exists: '{}'", colored_tip_word, colored_suggestion)
176+
format!(
177+
" {}: a similar argument exists: '{}'",
178+
colored_tip_word, colored_suggestion
179+
)
166180
} else {
167181
format!(" {}", translated)
168182
}
@@ -178,7 +192,11 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
178192
let formatted_usage = crate::format_usage(&usage_text);
179193
let usage_label = {
180194
let translated = translate!("common-usage");
181-
if translated == "common-usage" { "Usage".to_string() } else { translated }
195+
if translated == "common-usage" {
196+
"Usage".to_string()
197+
} else {
198+
translated
199+
}
182200
};
183201
eprintln!("{}: {}", usage_label, formatted_usage);
184202
eprintln!();
@@ -190,7 +208,11 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
190208
// Generic fallback case
191209
let error_word = {
192210
let translated = translate!("common-error");
193-
if translated == "common-error" { "error".to_string() } else { translated }
211+
if translated == "common-error" {
212+
"error".to_string()
213+
} else {
214+
translated
215+
}
194216
};
195217
let colored_error_word = maybe_colorize(&error_word, Color::Red);
196218
eprintln!("{colored_error_word}: unexpected argument");

src/uucore/src/lib/mods/locale.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,46 @@ invalid-syntax = This is { $missing
14021402
panic!("Expected LocalizationError::ParseResource with snippet");
14031403
}
14041404
}
1405+
1406+
#[test]
1407+
fn test_clap_localization_fallbacks() {
1408+
std::thread::spawn(|| {
1409+
// Test the scenario where localization isn't properly initialized
1410+
// and we need fallbacks for clap error handling
1411+
1412+
// First, test when localizer is not initialized
1413+
let error_msg = get_message("common-error");
1414+
assert_eq!(error_msg, "common-error"); // Should return key when not initialized
1415+
1416+
let tip_msg = get_message("common-tip");
1417+
assert_eq!(tip_msg, "common-tip"); // Should return key when not initialized
1418+
1419+
// Now initialize with setup_localization_with_common
1420+
let result = setup_localization_with_common("comm");
1421+
if result.is_err() {
1422+
// If setup fails (e.g., no embedded locales for comm), try with a known utility
1423+
let _ = setup_localization_with_common("test");
1424+
}
1425+
1426+
// Test that common strings are available after initialization
1427+
let error_after_init = get_message("common-error");
1428+
// Should either be translated or return the key (but not panic)
1429+
assert!(!error_after_init.is_empty());
1430+
1431+
let tip_after_init = get_message("common-tip");
1432+
assert!(!tip_after_init.is_empty());
1433+
1434+
// Test that clap error keys work with fallbacks
1435+
let unknown_arg_key = get_message("clap-error-unexpected-argument");
1436+
assert!(!unknown_arg_key.is_empty());
1437+
1438+
// Test usage key fallback
1439+
let usage_key = get_message("common-usage");
1440+
assert!(!usage_key.is_empty());
1441+
})
1442+
.join()
1443+
.unwrap();
1444+
}
14051445
}
14061446

14071447
#[cfg(all(test, not(debug_assertions)))]

0 commit comments

Comments
 (0)