From 2b4fcb7a01940713845f0d83932ae21a9b646afe Mon Sep 17 00:00:00 2001 From: innocentzero Date: Thu, 29 Feb 2024 12:27:05 +0530 Subject: [PATCH] [feat] Use clap derive API to parse CLI arguments This patch follows the previous one to update the method of taking arguments for swhks. The derive API is the recommended method upstream. Signed-off-by: innocentzero --- swhks/Cargo.toml | 4 ++-- swhks/src/main.rs | 37 ++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/swhks/Cargo.toml b/swhks/Cargo.toml index 33720f2..2cf3d28 100644 --- a/swhks/Cargo.toml +++ b/swhks/Cargo.toml @@ -7,7 +7,7 @@ version = "1.3.0-dev" authors = [ "Shinyzenith \n", "Angelo Fallaria \n", - "EdenQwQ \n" + "EdenQwQ \n", ] [dependencies] @@ -15,7 +15,7 @@ env_logger = "0.9.0" log = "0.4.14" nix = "0.23.1" sysinfo = "0.23.5" -clap = "3.1.6" +clap = { version = "4.1.0", features = ["derive"] } [[bin]] name = "swhks" diff --git a/swhks/src/main.rs b/swhks/src/main.rs index 3de2eae..f66fc32 100644 --- a/swhks/src/main.rs +++ b/swhks/src/main.rs @@ -1,4 +1,4 @@ -use clap::arg; +use clap::Parser; use environ::Env; use nix::{ sys::stat::{umask, Mode}, @@ -10,26 +10,29 @@ use std::{ env, fs, fs::OpenOptions, os::unix::net::UnixListener, - path::Path, + path::{Path, PathBuf}, process::{exit, id, Command, Stdio}, }; use sysinfo::{ProcessExt, System, SystemExt}; mod environ; +/// IPC Server for swhkd +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Args { + /// Set a custom log file. (Defaults to ${XDG_DATA_HOME:-$HOME/.local/share}/swhks-current_unix_time.log) + #[arg(short, long, value_name = "FILE")] + log: Option, + + /// Enable Debug Mode + #[arg(short, long)] + debug: bool, +} + fn main() -> std::io::Result<()> { - let app = clap::Command::new("swhks") - .version(env!("CARGO_PKG_VERSION")) - .author(env!("CARGO_PKG_AUTHORS")) - .about("IPC Server for swhkd") - .arg(arg!(-l --log ).required(false).takes_value(true).help( - "Set a custom log file. (Defaults to ${XDG_DATA_HOME:-$HOME/.local/share}/swhks-current_unix_time.log)", - )) - .arg(arg!(-d --debug).required(false).takes_value(false).help( - "Enable debug mode." - )); - let args = app.get_matches(); - if args.is_present("debug") { + let args = Args::parse(); + if args.debug { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("swhks=trace")) .init(); } else { @@ -45,8 +48,8 @@ fn main() -> std::io::Result<()> { let (pid_file_path, sock_file_path) = get_file_paths(&environment); - let log_file_name = if let Some(val) = args.value_of("log") { - val.to_string() + let log_file_name = if let Some(val) = args.log { + val } else { let time = match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(n) => n.as_secs().to_string(), @@ -56,7 +59,7 @@ fn main() -> std::io::Result<()> { } }; - format!("{}/swhks/swhks-{}.log", environment.data_home.to_string_lossy(), time) + format!("{}/swhks/swhks-{}.log", environment.data_home.to_string_lossy(), time).into() }; let log_path = Path::new(&log_file_name);