diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index ee1ac25..2cbdd86 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -9,8 +9,8 @@ version = "2.1.0" [dependencies] anyhow = "1" -clap = "2.32" -itertools = "0.10.3" +clap = { version = "4.1.14", features = ["derive"] } +itertools = "0.11.0" rust-i18n-support = { path = "../support", version = "2.1.0" } rust-i18n-extract = { path = "../extract", version = "2.1.0" } serde = { version = "1", features = ["derive"] } diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 8704a77..7da0a96 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,67 +1,61 @@ use anyhow::Error; -use clap::{App, Arg, SubCommand}; +use clap::{Args, Parser}; use std::{collections::HashMap, path::Path}; use rust_i18n_extract::{extractor, generator, iter}; mod config; -const APP_NAME: &str = "rust-i18n"; -const ABOUT: &str = r#"Rust I18n command for help you simply to extract all untranslated texts from soruce code. - -It will iter all Rust files in and extract all untranslated texts that used `t!` macro. -And then generate a YAML file and merge for existing texts. +#[derive(Parser)] +#[command(name = "cargo")] +#[command(bin_name = "cargo")] +enum CargoCli { + I18n(I18nArgs), +} -https://github.com/longbridgeapp/rust-i18n -"#; +#[derive(Args)] +#[command(author, version)] +// #[command(propagate_version = true)] +/// Rust I18n command to help you extract all untranslated texts from source code. +/// +/// It will iterate all Rust files in the source directory and extract all untranslated texts +/// that used `t!` macro. +/// Then it will generate a YAML file and merge with the existing translations. +/// +/// https://github.com/longbridgeapp/rust-i18n +struct I18nArgs { + /// Extract all untranslated I18n texts from source code + #[arg(default_value = "./")] + source: Option, +} fn main() -> Result<(), Error> { - let extract_command = SubCommand::with_name("i18n") - .about("Extract all untranslated I18n texts from soruce code") - .version(clap::crate_version!()) - .arg( - Arg::with_name("source") - .help("Path of your Rust crate root and Cargo.toml") - .default_value("./"), - ); - - let app = App::new(APP_NAME) - .bin_name("cargo") - .about(ABOUT) - .subcommand(extract_command) - .get_matches(); + let CargoCli::I18n(args) = CargoCli::parse(); let mut results = HashMap::new(); - #[allow(clippy::single_match)] - match app.subcommand() { - ("i18n", Some(sub_m)) => { - let source_path = sub_m.value_of("source").expect("Missing source path"); + let source_path = args.source.expect("Missing source path"); - let cfg = config::load(std::path::Path::new(source_path))?; + let cfg = config::load(std::path::Path::new(&source_path))?; - iter::iter_crate(source_path, |path, source| { - extractor::extract(&mut results, path, source) - })?; + iter::iter_crate(&source_path, |path, source| { + extractor::extract(&mut results, path, source) + })?; - let mut messages: Vec<_> = results.values().collect(); - messages.sort_by_key(|m| m.index); + let mut messages: Vec<_> = results.values().collect(); + messages.sort_by_key(|m| m.index); - let mut has_error = false; + let mut has_error = false; - let output_path = Path::new(source_path).join(&cfg.load_path); + let output_path = Path::new(&source_path).join(&cfg.load_path); - let result = - generator::generate(&output_path, &cfg.available_locales, messages.clone()); - if result.is_err() { - has_error = true; - } + let result = generator::generate(&output_path, &cfg.available_locales, messages.clone()); + if result.is_err() { + has_error = true; + } - if has_error { - std::process::exit(1); - } - } - _ => {} + if has_error { + std::process::exit(1); } Ok(())