Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clap: locales #348

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions datetime/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use clap::Parser;
use gettextrs::{
bind_textdomain_codeset, bindtextdomain, gettext, setlocale, textdomain, LocaleCategory,
};
use plib::i18n::ClapLocale;

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -49,6 +50,8 @@ struct Args {
version: Option<bool>,
}

impl ClapLocale for Args {}

enum TimeError {
ExecCommand(String),
ExecTime,
Expand Down Expand Up @@ -136,7 +139,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
bindtextdomain(env!("PROJECT_NAME"), "locale")?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let args = Args::parse();
let args = Args::parse_with_locale();

if let Err(err) = time(args) {
match err {
Expand All @@ -155,5 +158,5 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}

Status::Ok.exit()
Status::Ok.exit();
}
2 changes: 2 additions & 0 deletions plib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ rust-version.workspace = true

[dependencies]
cfg-if = "1.0"
clap.workspace = true
gettext-rs.workspace = true
libc.workspace = true
errno.workspace = true

Expand Down
64 changes: 64 additions & 0 deletions plib/src/i18n.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

use clap::{
error::{ContextKind, ContextValue, ErrorKind},
Parser,
};
use gettextrs::gettext;
use std::process::exit;

pub trait ClapLocale: Parser {
fn parse_with_locale() -> Self {
Self::try_parse().unwrap_or_else(|err| {
match err.kind() {
ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
eprint!("{}", err);
exit(0);
}
ErrorKind::MissingRequiredArgument => {
eprintln!(
"{}",
gettext("error: the following required arguments were not provided:")
);
for (k, v) in err.context() {
match k {
ContextKind::InvalidArg => {
if let ContextValue::Strings(v) = v {
for i in v {
eprintln!(" {i}");
}
}
}
ContextKind::Usage => {} // ignore
_ => unreachable!(),
}
}
}
ErrorKind::UnknownArgument => {
for (k, v) in err.context() {
match k {
ContextKind::InvalidArg => {
if let ContextValue::String(v) = v {
eprintln!(
"{}",
gettext!("error: unexpected argument '{}' found", v)
);
}
}
ContextKind::Usage => {} // ignore
ContextKind::Suggested => {} // ignore
_ => unreachable!(),
}
}
}
_ => {
eprint!("{}", err);
exit(0);
}
};
eprintln!();
eprintln!("{}", gettext("For more information, try '--help'."));
exit(0);
})
}
}
1 change: 1 addition & 0 deletions plib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

pub mod curuser;
pub mod group;
pub mod i18n;
pub mod io;
pub mod lzw;
pub mod modestr;
Expand Down
Loading