Skip to content

Commit

Permalink
[feat] Use clap derive API to parse CLI arguments
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
InnocentZero committed Feb 29, 2024
1 parent 475ad90 commit 2b4fcb7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions swhks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ version = "1.3.0-dev"
authors = [
"Shinyzenith <[email protected]>\n",
"Angelo Fallaria <[email protected]>\n",
"EdenQwQ <[email protected]>\n"
"EdenQwQ <[email protected]>\n",
]

[dependencies]
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"
Expand Down
37 changes: 20 additions & 17 deletions swhks/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::arg;
use clap::Parser;
use environ::Env;
use nix::{
sys::stat::{umask, Mode},
Expand All @@ -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<PathBuf>,

/// 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 <FILE>).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 {
Expand All @@ -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(),
Expand All @@ -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);
Expand Down

0 comments on commit 2b4fcb7

Please sign in to comment.