Skip to content

Commit 7af285f

Browse files
committed
clap/locale: fix the colors for all programs (Closes: #8501)
1 parent 187d973 commit 7af285f

File tree

115 files changed

+2419
-1786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2419
-1786
lines changed

.vscode/cspell.dictionaries/workspace.wordlist.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,12 @@ getcwd
348348

349349
# * other
350350
algs
351+
352+
# translation tests
353+
CLICOLOR
354+
erreur
355+
Utilisation
356+
merror
357+
merreur
358+
verbo
359+
inattendu

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ chrono = { version = "0.4.41", default-features = false, features = [
301301
"alloc",
302302
"clock",
303303
] }
304-
clap = { version = "4.5", features = ["wrap_help", "cargo"] }
304+
clap = { version = "4.5", features = ["wrap_help", "cargo", "color"] }
305305
clap_complete = "4.4"
306306
clap_mangen = "0.2"
307307
compare = "0.1.0"

src/uu/arch/src/arch.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
use platform_info::*;
77

88
use clap::Command;
9-
use uucore::LocalizedCommand;
109
use uucore::error::{UResult, USimpleError};
1110
use uucore::translate;
1211

1312
#[uucore::main]
1413
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
15-
uu_app().get_matches_from_localized(args);
14+
uucore::clap_localization::handle_clap_result(uu_app(), args)?;
1615

1716
let uts =
1817
PlatformInfo::new().map_err(|_e| USimpleError::new(1, translate!("cannot-get-system")))?;

src/uu/base32/src/base_common.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::ffi::OsString;
1010
use std::fs::File;
1111
use std::io::{self, ErrorKind, Read, Seek, SeekFrom};
1212
use std::path::{Path, PathBuf};
13-
use uucore::LocalizedCommand;
1413
use uucore::display::Quotable;
1514
use uucore::encoding::{
1615
BASE2LSBF, BASE2MSBF, EncodingWrapper, Format, SupportsFastDecodeAndEncode, Z85Wrapper,
@@ -101,17 +100,17 @@ pub fn parse_base_cmd_args(
101100
usage: &str,
102101
) -> UResult<Config> {
103102
let command = base_app(about, usage);
104-
let matches = command.get_matches_from_localized(args);
103+
let matches = uucore::clap_localization::handle_clap_result(command, args)?;
105104
Config::from(&matches)
106105
}
107106

108107
pub fn base_app(about: &'static str, usage: &str) -> Command {
109-
Command::new(uucore::util_name())
108+
let cmd = Command::new(uucore::util_name())
110109
.version(uucore::crate_version!())
111-
.help_template(uucore::localized_help_template(uucore::util_name()))
112110
.about(about)
113111
.override_usage(format_usage(usage))
114-
.infer_long_args(true)
112+
.infer_long_args(true);
113+
uucore::clap_localization::configure_localized_command(cmd, uucore::util_name())
115114
// Format arguments.
116115
.arg(
117116
Arg::new(options::DECODE)

src/uu/basename/src/basename.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use uucore::error::{UResult, UUsageError};
1515
use uucore::format_usage;
1616
use uucore::line_ending::LineEnding;
1717

18-
use uucore::LocalizedCommand;
1918
use uucore::translate;
2019

2120
pub mod options {
@@ -30,7 +29,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3029
//
3130
// Argument parsing
3231
//
33-
let matches = uu_app().get_matches_from_localized(args);
32+
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
3433

3534
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO));
3635

src/uu/basenc/src/basenc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use clap::{Arg, ArgAction, Command};
99
use uu_base32::base_common::{self, BASE_CMD_PARSE_ERROR, Config};
10-
use uucore::error::UClapError;
1110
use uucore::translate;
1211
use uucore::{
1312
encoding::Format,
@@ -64,9 +63,7 @@ pub fn uu_app() -> Command {
6463
}
6564

6665
fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> {
67-
let matches = uu_app()
68-
.try_get_matches_from(args.collect_lossy())
69-
.with_exit_code(1)?;
66+
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args.collect_lossy())?;
7067

7168
let encodings = get_encodings();
7269
let format = encodings

src/uu/cat/src/cat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::os::unix::fs::FileTypeExt;
2323
#[cfg(unix)]
2424
use std::os::unix::net::UnixStream;
2525
use thiserror::Error;
26-
use uucore::LocalizedCommand;
2726
use uucore::display::Quotable;
2827
use uucore::error::UResult;
2928
#[cfg(not(target_os = "windows"))]
@@ -232,7 +231,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
232231
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
233232
}
234233

235-
let matches = uu_app().get_matches_from_localized(args);
234+
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;
236235

237236
let number_mode = if matches.get_flag(options::NUMBER_NONBLANK) {
238237
NumberingMode::NonEmpty

src/uu/chcon/src/chcon.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#![allow(clippy::upper_case_acronyms)]
88

99
use clap::builder::ValueParser;
10-
use uucore::LocalizedCommand;
1110
use uucore::error::{UResult, USimpleError, UUsageError};
1211
use uucore::translate;
1312
use uucore::{display::Quotable, format_usage, show_error, show_warning};
@@ -155,20 +154,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
155154
}
156155

157156
pub fn uu_app() -> Command {
158-
Command::new(uucore::util_name())
157+
let cmd = Command::new(uucore::util_name())
159158
.version(uucore::crate_version!())
160-
.help_template(uucore::localized_help_template(uucore::util_name()))
161159
.about(translate!("chcon-about"))
162160
.override_usage(format_usage(&translate!("chcon-usage")))
163-
.infer_long_args(true)
164-
.disable_help_flag(true)
161+
.infer_long_args(true);
162+
uucore::clap_localization::configure_localized_command(cmd, uucore::util_name())
165163
.args_override_self(true)
166-
.arg(
167-
Arg::new(options::HELP)
168-
.long(options::HELP)
169-
.help(translate!("chcon-help-help"))
170-
.action(ArgAction::Help),
171-
)
164+
.disable_help_flag(true)
172165
.arg(
173166
Arg::new(options::dereference::DEREFERENCE)
174167
.long(options::dereference::DEREFERENCE)
@@ -183,6 +176,12 @@ pub fn uu_app() -> Command {
183176
.help(translate!("chcon-help-no-dereference"))
184177
.action(ArgAction::SetTrue),
185178
)
179+
.arg(
180+
Arg::new("help")
181+
.long("help")
182+
.help(translate!("help"))
183+
.action(ArgAction::Help),
184+
)
186185
.arg(
187186
Arg::new(options::preserve_root::PRESERVE_ROOT)
188187
.long(options::preserve_root::PRESERVE_ROOT)
@@ -305,7 +304,18 @@ struct Options {
305304
}
306305

307306
fn parse_command_line(config: Command, args: impl uucore::Args) -> Result<Options> {
308-
let matches = config.get_matches_from_localized(args);
307+
let matches = match config.try_get_matches_from(args) {
308+
Ok(matches) => matches,
309+
Err(e) if e.exit_code() == 0 => {
310+
// Help/version - use the error's print method which respects the Command's color settings
311+
e.print().expect("Failed to print help");
312+
std::process::exit(0);
313+
}
314+
Err(e) => {
315+
// Other errors - use localized error handling
316+
uucore::clap_localization::handle_clap_error_with_exit_code(e, "chcon", 1);
317+
}
318+
};
309319

310320
let verbose = matches.get_flag(options::VERBOSE);
311321

src/uu/chgrp/src/chgrp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
9898
}
9999

100100
pub fn uu_app() -> Command {
101-
Command::new(uucore::util_name())
101+
let cmd = Command::new(uucore::util_name())
102102
.version(uucore::crate_version!())
103-
.help_template(uucore::localized_help_template(uucore::util_name()))
104103
.about(translate!("chgrp-about"))
105104
.override_usage(format_usage(&translate!("chgrp-usage")))
106-
.infer_long_args(true)
105+
.infer_long_args(true);
106+
uucore::clap_localization::configure_localized_command(cmd, uucore::util_name())
107107
.disable_help_flag(true)
108108
.arg(
109109
Arg::new(options::HELP)

src/uu/chmod/src/chmod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::fs;
1111
use std::os::unix::fs::{MetadataExt, PermissionsExt};
1212
use std::path::Path;
1313
use thiserror::Error;
14-
use uucore::LocalizedCommand;
1514
use uucore::display::Quotable;
1615
use uucore::error::{ExitCode, UError, UResult, USimpleError, UUsageError, set_exit_code};
1716
use uucore::fs::display_permissions_unix;
@@ -111,9 +110,10 @@ fn extract_negative_modes(mut args: impl uucore::Args) -> (Option<String>, Vec<O
111110
#[uucore::main]
112111
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
113112
let (parsed_cmode, args) = extract_negative_modes(args.skip(1)); // skip binary name
114-
let matches = uu_app()
115-
.after_help(translate!("chmod-after-help"))
116-
.get_matches_from_localized(args);
113+
let matches = uucore::clap_localization::handle_clap_result(
114+
uu_app().after_help(translate!("chmod-after-help")),
115+
args,
116+
)?;
117117

118118
let changes = matches.get_flag(options::CHANGES);
119119
let quiet = matches.get_flag(options::QUIET);
@@ -177,9 +177,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
177177
pub fn uu_app() -> Command {
178178
Command::new(uucore::util_name())
179179
.version(uucore::crate_version!())
180-
.help_template(uucore::localized_help_template(uucore::util_name()))
181180
.about(translate!("chmod-about"))
182181
.override_usage(format_usage(&translate!("chmod-usage")))
182+
.help_template(uucore::localized_help_template(uucore::util_name()))
183183
.args_override_self(true)
184184
.infer_long_args(true)
185185
.no_binary_name(true)

0 commit comments

Comments
 (0)