diff --git a/src/main.rs b/src/main.rs index ae4a3a5..198b8e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,27 +33,20 @@ 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 - } else if args[1] == "p" { - "p" // Plain format - } else if args[1] == "r" { - "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 + Some(value) => value.clone(), + None => "j".to_string(), }; - - let verbose = args.verbose; - //let verbose: bool = (args.len() > 2) && (args[2] == "v"); + 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", + ))); + } + + let verbose = args.verbose; let connection = Connection::session().await?; connection @@ -85,9 +78,6 @@ async fn main() -> Result<()> { println!("GetAll request received for interface: {interface_name}"); } } else { - if verbose { - println!("Unknown interface requested: {interface_name}"); - } // Reply with an error connection .reply_error( @@ -96,6 +86,9 @@ async fn main() -> Result<()> { &"Unknown interface".to_string(), ) .await?; + if verbose { + println!("Unknown interface requested: {interface_name}"); + } } } "GetServerInformation" => { diff --git a/src/optparse.rs b/src/optparse.rs index f3ed7a1..e2a4d67 100644 --- a/src/optparse.rs +++ b/src/optparse.rs @@ -3,11 +3,11 @@ use argh::FromArgs; #[derive(FromArgs)] /// Print desktop notifications pub struct Cli { - /// select output format: j(json), r(rson), p(plain) + /// select output format: r(rson), j(json, default), p(plain) #[argh(option, short = 'f')] pub format: Option, /// verbose mode #[argh(switch, short = 'v')] pub verbose: bool, -} \ No newline at end of file +}