Skip to content

Commit 28c5e69

Browse files
Add clap for argument parsing
1 parent fce39e7 commit 28c5e69

File tree

3 files changed

+139
-13
lines changed

3 files changed

+139
-13
lines changed

Cargo.lock

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "1.0.0"
44
edition = "2024"
55

66
[dependencies]
7+
clap = "4.5.60"
78
# hyprland = "0.3.13"
89
hyprland = { git = "https://github.com/hyprland-community/hyprland-rs", branch = "master" }
910
procfs = "0.18.0"

src/main.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,39 @@ use hyprland::{
77
event_listener::{EventListener, WindowOpenEvent},
88
shared::HyprData,
99
};
10+
11+
use clap::Arg;
12+
use clap::Command as ClapCommand;
1013
use procfs::process::Process;
1114
use std::env;
1215

1316
fn main() {
14-
let args: Vec<String> = env::args().collect();
17+
let matches = ClapCommand::new(env!("CARGO_PKG_NAME"))
18+
.version(env!("CARGO_PKG_VERSION"))
19+
.author(env!("CARGO_PKG_AUTHORS"))
20+
.about("Automatically tag newly launched Steam games in Hyprland.")
21+
.arg(
22+
Arg::new("callback")
23+
.required(false)
24+
.help("A callback that will be called when a new window of a steam game appears")
25+
.index(1)
26+
)
27+
.arg(
28+
Arg::new("callback-arguments")
29+
.required(false)
30+
.index(2)
31+
.num_args(1..)
32+
.help("Arguments for the callback. The PID and Steam app ID will be appended to the arguments.")
33+
.allow_hyphen_values(true)
34+
.value_parser(clap::value_parser!(String)),
35+
).get_matches();
1536

16-
let script_path = if args.len() > 1 {
17-
Some(args[1].clone())
18-
} else {
19-
None
20-
};
21-
let script_args = if args.len() > 2 {
22-
args[2..].to_vec()
23-
} else {
24-
Vec::new()
25-
};
37+
let callback = matches.get_one::<String>("callback").cloned();
38+
let callback_args: Vec<String> = matches
39+
.get_many::<String>("callback-arguments")
40+
.unwrap_or_default()
41+
.cloned()
42+
.collect();
2643

2744
let mut listener = EventListener::new();
2845
listener.add_window_opened_handler(move |event| {
@@ -35,8 +52,8 @@ fn main() {
3552
true
3653
}
3754
Ok(GameResult::Game { pid, app_id }) => {
38-
if let Some(script_path) = &script_path {
39-
let mut new_args = script_args.clone();
55+
if let Some(script_path) = &callback {
56+
let mut new_args = callback_args.clone();
4057
new_args.push(pid.to_string());
4158
new_args.push(app_id);
4259
let mut child = Command::new(script_path).args(&new_args).spawn().unwrap();

0 commit comments

Comments
 (0)