diff --git a/src/main.rs b/src/main.rs index 198b8e0..5a8b329 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ pub mod formats { mod notification; use notification::{to_notif, Notification}; use std::collections::HashMap; -// use std::env; pub mod optparse; use futures_util::stream::TryStreamExt; @@ -33,19 +32,27 @@ fn server_properties() -> HashMap { #[tokio::main] async fn main() -> Result<()> { + // Options let args: optparse::Cli = argh::from_env(); + + // Format: r|j|p let op_format = match args.format { - Some(value) => value.clone(), + Some(value) => { + // Reject invalid format option + if ["r", "j", "p"].contains(&value.as_str()) { + value.clone() + } else { + // Exit with error code + return Err(zbus::Error::from(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "Invalid output format", + ))); + } + } None => "j".to_string(), }; - if !["r", "j", "p"].contains(&op_format.as_str()) { - return Err(zbus::Error::from(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Invalid output format", - ))); - } - + // Verbose mode. Off/false by default let verbose = args.verbose; let connection = Connection::session().await?; @@ -131,12 +138,12 @@ async fn main() -> Result<()> { println!("{}\n", ¬if.plain()); // Print the plain version } _ => { - println!("Onkown output format."); + println!("Onkown output format."); // This is probably unreachable } } } "CloseNotification" => { - // Client sent a close signal. Extract notification ID of the notif to be closed from the message body + // Client sent a 'close' signal. Extract notification ID of the notif to be closed from the message body let notification_id: u32 = msg.body().deserialize()?; // This method has only one parameter, the id // Tracking notifications by their IDs, closing them, and other features may be implemented later