fix some last clap errors mgmt

This commit is contained in:
Sylvestre Ledru
2025-08-11 23:26:13 +02:00
parent ce941947b7
commit 026d378c0e
7 changed files with 61 additions and 40 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
//! library ~ (core/bundler file)
// #![deny(missing_docs)] //TODO: enable this
//
// spell-checker:ignore sigaction SIGBUS SIGSEGV extendedbigdecimal
// spell-checker:ignore sigaction SIGBUS SIGSEGV extendedbigdecimal myutil
// * feature-gated external crates (re-shared as public internal modules)
#[cfg(feature = "libc")]
+50 -19
View File
@@ -212,30 +212,61 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
let option = arg.to_string();
let value = value.to_string();
// Get localized error word
let error_word = translate!("common-error");
let colored_error_word = maybe_colorize(&error_word, Color::Red);
// Check if this is actually a missing value (empty string)
if value.is_empty() {
// This is the case where no value was provided for an option that requires one
eprintln!(
"error: a value is required for '{}' but none was supplied",
option
);
eprintln!();
eprintln!("For more information, try '--help'.");
std::process::exit(1);
} else {
// Get localized error word
let error_word = translate!("common-error");
let colored_error_word = maybe_colorize(&error_word, Color::Red);
// Apply color to value and option if colors are enabled
let colored_value = maybe_colorize(&value, Color::Yellow);
let colored_option = maybe_colorize(&option, Color::Green);
// Apply color to value and option if colors are enabled
let colored_value = maybe_colorize(&value, Color::Yellow);
let colored_option = maybe_colorize(&option, Color::Green);
// Print localized error message
let error_msg = translate!(
"clap-error-invalid-value",
"error_word" => colored_error_word,
"value" => colored_value,
"option" => colored_option
);
eprintln!("{error_msg}");
// For ValueValidation errors, include the validation error details
if matches!(kind, ErrorKind::ValueValidation) {
if let Some(source) = err.source() {
eprintln!(" {}", source);
// For ValueValidation errors, include the validation error in the message
if matches!(kind, ErrorKind::ValueValidation) {
if let Some(source) = err.source() {
// Print error with validation detail on same line
let error_msg = translate!(
"clap-error-invalid-value",
"error_word" => colored_error_word,
"value" => colored_value,
"option" => colored_option
);
eprintln!("{error_msg}: {}", source);
} else {
// Print localized error message
let error_msg = translate!(
"clap-error-invalid-value",
"error_word" => colored_error_word,
"value" => colored_value,
"option" => colored_option
);
eprintln!("{error_msg}");
}
} else {
// Print localized error message
let error_msg = translate!(
"clap-error-invalid-value",
"error_word" => colored_error_word,
"value" => colored_value,
"option" => colored_option
);
eprintln!("{error_msg}");
}
}
// For ValueValidation errors, include the validation error details
// Note: We don't print these separately anymore as they're part of the main message
// Show possible values if available (for InvalidValue errors)
if matches!(kind, ErrorKind::InvalidValue) {
if let Some(valid_values) = err.get(ContextKind::ValidValue) {
+2 -3
View File
@@ -112,13 +112,12 @@ fn test_wrap() {
#[test]
fn test_wrap_no_arg() {
let expected_stderr = "a value is required for '--wrap <COLS>' but none was supplied";
for wrap_param in ["-w", "--wrap"] {
new_ucmd!()
.arg(wrap_param)
.fails()
.stderr_contains(expected_stderr)
.stderr_contains("error: a value is required for '--wrap <COLS>' but none was supplied")
.stderr_contains("For more information, try '--help'.")
.no_stdout();
}
}
+2 -1
View File
@@ -147,7 +147,8 @@ fn test_wrap_no_arg() {
new_ucmd!()
.arg(wrap_param)
.fails()
.stderr_contains("a value is required for '--wrap <COLS>' but none was supplied")
.stderr_contains("error: a value is required for '--wrap <COLS>' but none was supplied")
.stderr_contains("For more information, try '--help'.")
.no_stdout();
}
}
+3 -1
View File
@@ -862,7 +862,9 @@ fn test_du_threshold_error_handling() {
new_ucmd!()
.arg("--threshold")
.fails()
.stderr_contains("a value is required for '--threshold <SIZE>' but none was supplied")
.stderr_contains(
"error: a value is required for '--threshold <SIZE>' but none was supplied",
)
.stderr_contains("For more information, try '--help'.");
}
+1 -12
View File
@@ -1684,19 +1684,8 @@ fn test_clap_localization_missing_required_argument() {
.fails();
let stderr_en = result_en.stderr_str();
assert!(stderr_en.contains("error:"));
assert!(stderr_en.contains(" a value is required for '--key <key>' but none was supplied"));
assert!(stderr_en.contains("-k"));
// Test in French
let result_fr = new_ucmd!()
.env("LANG", "fr_FR.UTF-8")
.env("LC_ALL", "fr_FR.UTF-8")
.arg("-k")
.fails();
let stderr_fr = result_fr.stderr_str();
// The main error message should contain French "erreur"
assert!(stderr_fr.contains("error:") || stderr_fr.contains("erreur"));
}
#[test]
+2 -3
View File
@@ -1935,9 +1935,8 @@ fn test_split_separator_no_value() {
.ignore_stdin_write_error()
.pipe_in("a\n")
.fails()
.stderr_contains(
"error: a value is required for '--separator <SEP>' but none was supplied",
);
.stderr_contains("error: a value is required for '--separator <SEP>' but none was supplied")
.stderr_contains("For more information, try '--help'.");
}
#[test]