options parsing

This commit is contained in:
Candifloss 2024-09-26 13:37:39 +05:30
parent 8c373462aa
commit 05833c5d99
4 changed files with 35 additions and 14 deletions

View File

@ -11,4 +11,5 @@ tokio = { version = "1.40.0", features = ["full"] }
futures-util = "0.3.30" futures-util = "0.3.30"
serde = { version = "1.0.210", features = ["derive"] } serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128" serde_json = "1.0.128"
rson_rs = "0.2.1" rson_rs = "0.2.1"
argh = "0.1.12"

View File

@ -7,7 +7,8 @@ pub mod formats {
mod notification; mod notification;
use notification::{to_notif, Notification}; use notification::{to_notif, Notification};
use std::collections::HashMap; use std::collections::HashMap;
use std::env; // use std::env;
pub mod optparse;
use futures_util::stream::TryStreamExt; use futures_util::stream::TryStreamExt;
use zbus::{message::Body, Connection, Result}; use zbus::{message::Body, Connection, Result};
@ -32,6 +33,7 @@ fn server_properties() -> HashMap<String, String> {
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
/*
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let op_format: &str = if args.len() == 1 || args[1] == "j" { let op_format: &str = if args.len() == 1 || args[1] == "j" {
"j" // Default value, json format "j" // Default value, json format
@ -41,9 +43,17 @@ async fn main() -> Result<()> {
"r" // rson format "r" // rson format
} else { } else {
"j" "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?; let connection = Connection::session().await?;
connection connection
@ -117,12 +127,12 @@ async fn main() -> Result<()> {
// Convert the msg body to a Notification object // Convert the msg body to a Notification object
let notif: Notification = to_notif(&msg_body)?; let notif: Notification = to_notif(&msg_body)?;
// Handle the notif // Handle the notif
match op_format { match op_format.as_str() {
"j" => { "j" => {
println!("{}", &notif.json()); // Print the json version println!("{}", &notif.json()); // Print the json version
} }
"r" => { "r" => {
println!("{}", &notif.rson()); // Print the plain version println!("{}", &notif.rson()); // Print the rson version
} }
"p" => { "p" => {
println!("{}\n", &notif.plain()); // Print the plain version println!("{}\n", &notif.plain()); // Print the plain version

View File

@ -51,14 +51,11 @@ impl Notification {
// Key:Val pairs of actions // Key:Val pairs of actions
pub fn actions(&self) -> Vec<(String, String)> { pub fn actions(&self) -> Vec<(String, String)> {
self.actions self.actions
.chunks(2) .chunks(2)
.map(|chunk| .map(|chunk| (chunk[0].clone(), chunk[1].clone()))
(chunk[0].clone(), .collect()
chunk[1].clone()) }
)
.collect()
}
// Hints // Hints
pub fn hints(&self) -> &HashMap<String, OwnedValue> { pub fn hints(&self) -> &HashMap<String, OwnedValue> {

13
src/optparse.rs Normal file
View File

@ -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<String>,
/// verbose mode
#[argh(switch, short = 'v')]
pub verbose: bool,
}