From 59e7e19b3eab0ab9844cb3b885b57662161ea928 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 25 Mar 2025 04:26:54 -0400 Subject: [PATCH 1/5] Add --version and -- help support with lexopt --- Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index a3626bf1..530064d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1260,6 +1260,7 @@ dependencies = [ "icu_provider", "ignore", "image", + "lexopt", "libc", "libcosmic", "liblzma", @@ -3548,6 +3549,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" +[[package]] +name = "lexopt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baff4b617f7df3d896f97fe922b64817f6cd9a756bb81d40f8883f2f66dcb401" + [[package]] name = "libc" version = "0.2.171" diff --git a/Cargo.toml b/Cargo.toml index 65aecbe3..f43ea663 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,8 @@ bzip2 = { version = "0.5", optional = true } #TODO: replace with pure Rust cra flate2 = "1.0" liblzma = { version = "0.3", optional = true } #TODO: replace with pure Rust crate tar = "0.4.43" +# CLI arguments +lexopt = "0.3.0" # Internationalization i18n-embed = { version = "0.15", features = [ "fluent-system", diff --git a/src/main.rs b/src/main.rs index d4cb5bda..93d25e7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,44 @@ use tikv_jemallocator::Jemalloc; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; +use lexopt::{Arg, Parser}; + fn main() -> Result<(), Box> { + let mut parser = Parser::from_env(); + + // Parse the arguments + while let Some(arg) = parser.next()? { + match arg { + Arg::Short('h') | Arg::Long("help") => { + print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")); + return Ok(()); + } + Arg::Short('v') | Arg::Long("version") => { + println!( + "cosmic-files {} (git commit {})", + env!("CARGO_PKG_VERSION"), + env!("VERGEN_GIT_SHA") + ); + return Ok(()); + } + _ => {} + } + } cosmic_files::main() } + + +fn print_help(version: &str, git_rev: &str) { + println!( + r#"cosmic-files {version} (git commit {git_rev}) +System76 + +Designed for the COSMICâ„¢ desktop environment, cosmic-files is a libcosmic-based file manager. + +Project home page: https://github.com/pop-os/cosmic-files + +Options: + -h, --help Show this message + -v, --version Show the version of cosmic-files"# + ); +} From 9d2f1b70ac24a086a4f594100f5e66bbecac4f3c Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Tue, 25 Mar 2025 04:44:53 -0400 Subject: [PATCH 2/5] remove extra line --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 93d25e7a..cad69ca4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,6 @@ fn main() -> Result<(), Box> { cosmic_files::main() } - fn print_help(version: &str, git_rev: &str) { println!( r#"cosmic-files {version} (git commit {git_rev}) From 4f90dfa86493ace1d80db1b3923ae0687ce87261 Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Tue, 25 Mar 2025 11:33:31 -0400 Subject: [PATCH 3/5] Update Cargo.toml to clap_lex --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f43ea663..2aac20c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ flate2 = "1.0" liblzma = { version = "0.3", optional = true } #TODO: replace with pure Rust crate tar = "0.4.43" # CLI arguments -lexopt = "0.3.0" +clap_lex = "0.7" # Internationalization i18n-embed = { version = "0.15", features = [ "fluent-system", From 3f358d5a84b83a553c7b2213ccf85460f7e1361f Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Tue, 25 Mar 2025 11:35:40 -0400 Subject: [PATCH 4/5] Update main.rs to clap_lex Switch complete --- src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index cad69ca4..95802950 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,20 +5,23 @@ use tikv_jemallocator::Jemalloc; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -use lexopt::{Arg, Parser}; +use clap_lex::RawArgs; -fn main() -> Result<(), Box> { - let mut parser = Parser::from_env(); +use std::error::Error; + +fn main() -> Result<(), Box> { + let raw_args = RawArgs::from_args(); + let mut cursor = raw_args.cursor(); // Parse the arguments - while let Some(arg) = parser.next()? { - match arg { - Arg::Short('h') | Arg::Long("help") => { + while let Some(arg) = raw_args.next_os(&mut cursor) { + match arg.to_str() { + Some("--help") | Some("-h") => { print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")); return Ok(()); } - Arg::Short('v') | Arg::Long("version") => { - println!( + Some("--version") | Some("-v") => { + println!( "cosmic-files {} (git commit {})", env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA") From a95ad6fea52c8a99061a8f892d52a0895203217c Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Wed, 26 Mar 2025 16:01:30 -0400 Subject: [PATCH 5/5] -v to -V --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 95802950..439fe842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { print_help(env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_SHA")); return Ok(()); } - Some("--version") | Some("-v") => { + Some("--version") | Some("-V") => { println!( "cosmic-files {} (git commit {})", env!("CARGO_PKG_VERSION"),