use clap::Parser;
use rsproperties::PropertyConfig;
#[derive(Parser, Debug)]
#[command(name = "getprop")]
#[command(about = "Android-compatible property getter")]
#[command(
long_about = "This tool mimics Android's getprop command functionality.\nIt can retrieve system properties with optional default values."
)]
struct Args {
#[arg(help = "Name of the property to retrieve")]
property_name: Option<String>,
#[arg(help = "Default value to return if property is not found")]
default_value: Option<String>,
#[arg(long, help = "Custom properties directory")]
properties_dir: Option<std::path::PathBuf>,
}
fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let args = Args::parse();
let config = if let Some(props_dir) = args.properties_dir {
PropertyConfig::with_properties_dir(props_dir)
} else {
PropertyConfig::default()
};
if config.properties_dir.is_some() || config.socket_dir.is_some() {
rsproperties::init(config);
}
match args.property_name {
Some(name) => {
let value = if let Some(default) = args.default_value {
rsproperties::get_or(&name, default)
} else {
let result: Result<String, _> = rsproperties::get(&name);
match result {
Ok(val) if !val.is_empty() => val,
_ => {
return;
}
}
};
println!("{value}");
}
None => {
println!("Listing all properties is not yet implemented.");
println!("Use 'getprop <property_name>' to get a specific property.");
std::process::exit(1);
}
}
}