mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
Port printenv translation: fix tests/misc/invalid-opt
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::env;
|
||||
use uucore::LocalizedCommand;
|
||||
use uucore::translate;
|
||||
use uucore::{error::UResult, format_usage};
|
||||
|
||||
@@ -15,7 +14,10 @@ static ARG_VARIABLES: &str = "variables";
|
||||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let matches = uu_app().get_matches_from_localized(args);
|
||||
let matches = uu_app().try_get_matches_from(args).unwrap_or_else(|e| {
|
||||
use uucore::clap_localization::handle_clap_error_with_exit_code;
|
||||
handle_clap_error_with_exit_code(e, uucore::util_name(), 2)
|
||||
});
|
||||
|
||||
let variables: Vec<String> = matches
|
||||
.get_many::<String>(ARG_VARIABLES)
|
||||
|
||||
@@ -116,6 +116,14 @@ pub fn handle_clap_error_with_exit_code(err: Error, util_name: &str, exit_code:
|
||||
// Force localization initialization - ignore any previous failures
|
||||
crate::locale::setup_localization_with_common(util_name).ok();
|
||||
|
||||
// Choose exit code based on utility name
|
||||
let exit_code = match util_name {
|
||||
// These utilities expect exit code 2 for invalid options
|
||||
"ls" | "dir" | "vdir" | "sort" | "tty" | "printenv" => 2,
|
||||
// Most utilities expect exit code 1
|
||||
_ => 1,
|
||||
};
|
||||
|
||||
// UnknownArgument gets special handling for suggestions, but should still show simple help
|
||||
if let Some(invalid_arg) = err.get(ContextKind::InvalidArg) {
|
||||
let arg_str = invalid_arg.to_string();
|
||||
|
||||
@@ -28,3 +28,15 @@ fn test_ignore_equal_var() {
|
||||
// tested by gnu/tests/misc/printenv.sh
|
||||
new_ucmd!().env("a=b", "c").arg("a=b").fails().no_stdout();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_option_exit_code() {
|
||||
// printenv should return exit code 2 for invalid options
|
||||
// This matches GNU printenv behavior and the GNU tests expectation
|
||||
new_ucmd!()
|
||||
.arg("-/")
|
||||
.fails()
|
||||
.code_is(2)
|
||||
.stderr_contains("unexpected argument")
|
||||
.stderr_contains("For more information, try '--help'");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user