Skip to content

Commit 2b8a3f7

Browse files
HugoDrakulix
authored andcommitted
Argument support with clap_lex
1 parent 956734a commit 2b8a3f7

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ rustix = { version = "0.38.32", features = ["process"] }
6060
smallvec = "1.13.2"
6161
rand = "0.8.5"
6262
reis = { version = "0.4", features = ["calloop"] }
63+
# CLI arguments
64+
clap_lex = "0.7"
6365

6466
[dependencies.id_tree]
6567
branch = "feature/copy_clone"

src/main.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ use wayland::protocols::overlap_notify::OverlapNotifyState;
2424

2525
use crate::wayland::handlers::compositor::client_compositor_state;
2626

27+
use clap_lex::RawArgs;
28+
29+
use std::error::Error;
30+
2731
pub mod backend;
2832
pub mod config;
2933
pub mod dbus;
@@ -95,7 +99,30 @@ impl State {
9599
}
96100
}
97101

98-
fn main() -> Result<()> {
102+
fn main() -> Result<(), Box<dyn Error>> {
103+
let raw_args = RawArgs::from_args();
104+
let mut cursor = raw_args.cursor();
105+
let git_hash = option_env!("GIT_HASH").unwrap_or("unknown");
106+
107+
// Parse the arguments
108+
while let Some(arg) = raw_args.next_os(&mut cursor) {
109+
match arg.to_str() {
110+
Some("--help") | Some("-h") => {
111+
print_help(env!("CARGO_PKG_VERSION"), git_hash);
112+
return Ok(());
113+
}
114+
Some("--version") | Some("-V") => {
115+
println!(
116+
"cosmic-comp {} (git commit {})",
117+
env!("CARGO_PKG_VERSION"),
118+
git_hash
119+
);
120+
return Ok(());
121+
}
122+
_ => {}
123+
}
124+
}
125+
99126
// setup logger
100127
logger::init_logger()?;
101128
info!("Cosmic starting up!");
@@ -193,6 +220,21 @@ fn main() -> Result<()> {
193220
Ok(())
194221
}
195222

223+
fn print_help(version: &str, git_rev: &str) {
224+
println!(
225+
r#"cosmic-comp {version} (git commit {git_rev})
226+
227+
228+
Designed for the COSMIC™ desktop environment, cosmic-comp is a Wayland Compositor.
229+
230+
Project home page: https://github.com/pop-os/cosmic-comp
231+
232+
Options:
233+
-h, --help Show this message
234+
-v, --version Show the version of cosmic-comp"#
235+
);
236+
}
237+
196238
fn init_wayland_display(
197239
event_loop: &mut EventLoop<state::State>,
198240
) -> Result<(DisplayHandle, OsString)> {

0 commit comments

Comments
 (0)