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

@ -12,3 +12,4 @@ futures-util = "0.3.30"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
rson_rs = "0.2.1"
argh = "0.1.12"

View File

@ -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<String, String> {
#[tokio::main]
async fn main() -> Result<()> {
/*
let args: Vec<String> = 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 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: bool = (args.len() > 2) && (args[2] == "v");
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!("{}", &notif.json()); // Print the json version
}
"r" => {
println!("{}", &notif.rson()); // Print the plain version
println!("{}", &notif.rson()); // Print the rson version
}
"p" => {
println!("{}\n", &notif.plain()); // Print the plain version

View File

@ -53,10 +53,7 @@ impl Notification {
pub fn actions(&self) -> Vec<(String, String)> {
self.actions
.chunks(2)
.map(|chunk|
(chunk[0].clone(),
chunk[1].clone())
)
.map(|chunk| (chunk[0].clone(), chunk[1].clone()))
.collect()
}

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,
}