From 5b383f669b8824cf38996d3f4cec903c86bf8bac Mon Sep 17 00:00:00 2001 From: LinuxBoy-96 <128245725+LinuxBoy-96@users.noreply.github.com> Date: Mon, 24 Mar 2025 14:50:51 -0400 Subject: [PATCH] feat: add help and version command line arguments --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +++++ build.rs | 5 +++++ src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 build.rs diff --git a/Cargo.lock b/Cargo.lock index e729487..b4ce94f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,6 +195,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f" + [[package]] name = "apply" version = "0.3.0" @@ -1159,6 +1165,7 @@ dependencies = [ "greetd_ipc", "i18n-embed", "i18n-embed-fl", + "lexopt", "libcosmic", "log", "logind-zbus", @@ -1170,6 +1177,7 @@ dependencies = [ "shlex", "tokio", "upower_dbus", + "vergen", "wayland-client", "xdg", "xkb-data", @@ -3187,6 +3195,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" +[[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" @@ -3689,6 +3703,15 @@ dependencies = [ "syn 2.0.100", ] +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -5257,7 +5280,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d9c75b47bdff86fa3334a3db91356b8d7d86a9b839dab7d0bdc5c3d3a077618" dependencies = [ "deranged", + "itoa", + "libc", "num-conv", + "num_threads", "powerfmt", "serde", "time-core", @@ -5662,6 +5688,18 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "vergen" +version = "8.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2990d9ea5967266ea0ccf413a4aa5c42a93dbcfda9cb49a97de6931726b12566" +dependencies = [ + "anyhow", + "cfg-if", + "rustversion", + "time", +] + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 81a4d64..8367dc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "cosmic-greeter" version = "0.1.0" edition = "2024" +[build-dependencies] +vergen = { version = "8", features = ["git", "gitcl"] } + [dependencies] chrono = { version = "0.4", features = ["unstable-locales"] } cosmic-bg-config.workspace = true @@ -42,6 +45,8 @@ nix = { workspace = true, optional = true } upower_dbus = { git = "https://github.com/pop-os/dbus-settings-bindings", rev = "badfc6a", optional = true } # Required for some features zbus = { workspace = true, optional = true } +# CLI arguments +lexopt = "0.3.0" # Internationalization i18n-embed = { version = "0.14", features = [ "fluent-system", diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..bc2427f --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +use vergen::EmitBuilder; + +fn main() { + EmitBuilder::builder().git_sha(true).emit().unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 7986e20..0d3bb8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,30 @@ use cosmic_greeter::{greeter, locker}; +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-greeter {} (git commit {})", + env!("CARGO_PKG_VERSION"), + env!("VERGEN_GIT_SHA") + ); + return Ok(()); + } + _ => {} + } + } + match pwd::Passwd::current_user() { Some(current_user) => match current_user.name.as_str() { "cosmic-greeter" => greeter::main(), @@ -12,3 +35,19 @@ fn main() -> Result<(), Box> { _ => Err("failed to determine current user".into()), } } + +fn print_help(version: &str, git_rev: &str) { + println!( + r#"cosmic-greeter {version} (git commit {git_rev}) +System76 + +Designed for the COSMICâ„¢ desktop environment, cosmic-greeter is a libcosmic +frontend for greetd which can be run inside of cosmic-comp. + +Project home page: https://github.com/pop-os/cosmic-greeter + +Options: + -h, --help Show this message + -v, --version Show the version of cosmic-greeter"# + ); +}