CLI ergonomy: timeout value

This commit is contained in:
Jörn-Michael Miehe 2025-05-28 13:42:31 +00:00
parent 763c8f689e
commit 5643ced9d1
2 changed files with 17 additions and 8 deletions

View file

@ -7,15 +7,19 @@ use super::sharry::File;
#[derive(Parser, Debug, Hash)] #[derive(Parser, Debug, Hash)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct Cli { pub struct Cli {
/// Timeout in seconds for HTTP actions /// Timeout in seconds for HTTP actions (set 0 or invalid to disable)
#[arg(short, long, value_name = "SECS", value_parser = parse_seconds)] #[arg(
pub timeout: Option<Duration>, short, long,
default_value = "10", value_name = "SECS",
value_parser = parse_seconds,
)]
timeout: Duration,
/// Protocol for Sharry instance /// Protocol for Sharry instance
#[arg( #[arg(
short, long, short, long,
default_value = "https", value_name = "VARIANT", default_value = "https", value_name = "VARIANT",
value_parser = PossibleValuesParser::new(["http", "https"]) value_parser = PossibleValuesParser::new(["http", "https"]),
)] )]
pub protocol: String, pub protocol: String,
@ -47,11 +51,15 @@ pub struct Cli {
} }
fn parse_seconds(data: &str) -> Result<Duration, String> { fn parse_seconds(data: &str) -> Result<Duration, String> {
data.parse() data.parse().or(Ok(0)).map(Duration::from_secs)
.map(Duration::from_secs)
.map_err(|e| e.to_string())
} }
fn parse_sharry_file(data: &str) -> Result<File, String> { fn parse_sharry_file(data: &str) -> Result<File, String> {
File::new(data).map_err(|e| e.to_string()) File::new(data).map_err(|e| e.to_string())
} }
impl Cli {
pub fn get_timeout(&self) -> Option<Duration> {
(!self.timeout.is_zero()).then_some(self.timeout)
}
}

View file

@ -13,9 +13,10 @@ fn main() {
let args = Cli::parse(); let args = Cli::parse();
info!("args: {args:?}"); info!("args: {args:?}");
info!("timeout: {:?}", args.get_timeout());
let agent: Agent = Agent::config_builder() let agent: Agent = Agent::config_builder()
.timeout_global(args.timeout) .timeout_global(args.get_timeout())
.build() .build()
.into(); .into();