From 2b8a3f7894ed06f12b769824f77ef96914000a72 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 25 Mar 2025 16:44:55 -0400 Subject: [PATCH] Argument support with clap_lex --- Cargo.lock | 23 +++++++++++++++-------- Cargo.toml | 2 ++ src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0747f328..b5dab12a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "ab_glyph" @@ -609,6 +609,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + [[package]] name = "clipboard-win" version = "5.4.0" @@ -804,6 +810,7 @@ dependencies = [ "bitflags 2.8.0", "bytemuck", "calloop 0.14.2", + "clap_lex", "cosmic-comp-config", "cosmic-config", "cosmic-protocols", @@ -1467,7 +1474,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -2520,7 +2527,7 @@ dependencies = [ [[package]] name = "id_tree" version = "1.8.0" -source = "git+https://github.com/Drakulix/id-tree.git?branch=feature/copy_clone#632a57d6d49160e18d7300fa7edae52281ec5482" +source = "git+https://github.com/Drakulix/id-tree.git?branch=feature%2Fcopy_clone#632a57d6d49160e18d7300fa7edae52281ec5482" dependencies = [ "snowflake", ] @@ -2912,7 +2919,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -4386,7 +4393,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -4987,7 +4994,7 @@ dependencies = [ "getrandom", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -5280,7 +5287,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69fff37da548239c3bf9e64a12193d261e8b22b660991c6fd2df057c168f435f" dependencies = [ "cc", - "windows-targets 0.48.5", + "windows-targets 0.52.6", ] [[package]] @@ -5960,7 +5967,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 99dbadfc..85a3f0fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,8 @@ rustix = { version = "0.38.32", features = ["process"] } smallvec = "1.13.2" rand = "0.8.5" reis = { version = "0.4", features = ["calloop"] } +# CLI arguments +clap_lex = "0.7" [dependencies.id_tree] branch = "feature/copy_clone" diff --git a/src/main.rs b/src/main.rs index e86d9764..5d6d4364 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,10 @@ use wayland::protocols::overlap_notify::OverlapNotifyState; use crate::wayland::handlers::compositor::client_compositor_state; +use clap_lex::RawArgs; + +use std::error::Error; + pub mod backend; pub mod config; pub mod dbus; @@ -95,7 +99,30 @@ impl State { } } -fn main() -> Result<()> { +fn main() -> Result<(), Box> { + let raw_args = RawArgs::from_args(); + let mut cursor = raw_args.cursor(); + let git_hash = option_env!("GIT_HASH").unwrap_or("unknown"); + + // Parse the arguments + while let Some(arg) = raw_args.next_os(&mut cursor) { + match arg.to_str() { + Some("--help") | Some("-h") => { + print_help(env!("CARGO_PKG_VERSION"), git_hash); + return Ok(()); + } + Some("--version") | Some("-V") => { + println!( + "cosmic-comp {} (git commit {})", + env!("CARGO_PKG_VERSION"), + git_hash + ); + return Ok(()); + } + _ => {} + } + } + // setup logger logger::init_logger()?; info!("Cosmic starting up!"); @@ -193,6 +220,21 @@ fn main() -> Result<()> { Ok(()) } +fn print_help(version: &str, git_rev: &str) { + println!( + r#"cosmic-comp {version} (git commit {git_rev}) +System76 + +Designed for the COSMICâ„¢ desktop environment, cosmic-comp is a Wayland Compositor. + +Project home page: https://github.com/pop-os/cosmic-comp + +Options: + -h, --help Show this message + -v, --version Show the version of cosmic-comp"# + ); +} + fn init_wayland_display( event_loop: &mut EventLoop, ) -> Result<(DisplayHandle, OsString)> {