From 05833c5d99e565f4f0d7b34b8f717d146c5550c4 Mon Sep 17 00:00:00 2001 From: candifloss Date: Thu, 26 Sep 2024 13:37:39 +0530 Subject: [PATCH] options parsing --- Cargo.toml | 3 ++- src/main.rs | 20 +++++++++++++++----- src/notification.rs | 13 +++++-------- src/optparse.rs | 13 +++++++++++++ 4 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/optparse.rs diff --git a/Cargo.toml b/Cargo.toml index 8da4a6d..5e6d566 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ tokio = { version = "1.40.0", features = ["full"] } futures-util = "0.3.30" serde = { version = "1.0.210", features = ["derive"] } serde_json = "1.0.128" -rson_rs = "0.2.1" \ No newline at end of file +rson_rs = "0.2.1" +argh = "0.1.12" diff --git a/src/main.rs b/src/main.rs index 29bd456..ae4a3a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ pub mod formats { mod notification; use notification::{to_notif, Notification}; use std::collections::HashMap; -use std::env; +// use std::env; +pub mod optparse; use futures_util::stream::TryStreamExt; use zbus::{message::Body, Connection, Result}; @@ -32,6 +33,7 @@ fn server_properties() -> HashMap { #[tokio::main] async fn main() -> Result<()> { + /* let args: Vec = env::args().collect(); let op_format: &str = if args.len() == 1 || args[1] == "j" { "j" // Default value, json format @@ -41,9 +43,17 @@ async fn main() -> Result<()> { "r" // rson format } else { "j" - }; + }; */ - let verbose: bool = (args.len() > 2) && (args[2] == "v"); + let args: optparse::Cli = argh::from_env(); + let op_format = match args.format { + Some(value) => value.clone(), // Cloning the owned String + None => "j".to_string(), // Using the default value as a String + }; + + let verbose = args.verbose; + + //let verbose: bool = (args.len() > 2) && (args[2] == "v"); let connection = Connection::session().await?; connection @@ -117,12 +127,12 @@ async fn main() -> Result<()> { // Convert the msg body to a Notification object let notif: Notification = to_notif(&msg_body)?; // Handle the notif - match op_format { + match op_format.as_str() { "j" => { println!("{}", ¬if.json()); // Print the json version } "r" => { - println!("{}", ¬if.rson()); // Print the plain version + println!("{}", ¬if.rson()); // Print the rson version } "p" => { println!("{}\n", ¬if.plain()); // Print the plain version diff --git a/src/notification.rs b/src/notification.rs index 2e92fbe..538faa6 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -51,14 +51,11 @@ impl Notification { // Key:Val pairs of actions pub fn actions(&self) -> Vec<(String, String)> { - self.actions - .chunks(2) - .map(|chunk| - (chunk[0].clone(), - chunk[1].clone()) - ) - .collect() - } + self.actions + .chunks(2) + .map(|chunk| (chunk[0].clone(), chunk[1].clone())) + .collect() + } // Hints pub fn hints(&self) -> &HashMap { diff --git a/src/optparse.rs b/src/optparse.rs new file mode 100644 index 0000000..f3ed7a1 --- /dev/null +++ b/src/optparse.rs @@ -0,0 +1,13 @@ +use argh::FromArgs; + +#[derive(FromArgs)] +/// Print desktop notifications +pub struct Cli { + /// select output format: j(json), r(rson), p(plain) + #[argh(option, short = 'f')] + pub format: Option, + + /// verbose mode + #[argh(switch, short = 'v')] + pub verbose: bool, +} \ No newline at end of file