fixed option parsing

This commit is contained in:
Candifloss 2024-09-26 16:16:46 +05:30
parent 05833c5d99
commit 4a708bb6a0
2 changed files with 15 additions and 22 deletions

View File

@ -33,27 +33,20 @@ 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
} 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" => {

View File

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