use std::path::PathBuf;
use clap::Parser;
use humantime::Duration;
use crate::error::ParseError;
#[derive(Debug, Parser, Clone)]
pub struct CliArgs {
#[clap(short, long, default_value = "10m")]
#[arg(value_parser = parse_refresh_time)]
pub refresh_time: Duration,
#[clap(required = true)]
#[arg(value_parser = parse_directory)]
pub directory: PathBuf,
#[clap(short, long, default_value_t = 3)]
pub max_recursion_depth: usize,
}
impl Default for CliArgs {
fn default() -> Self {
Self {
refresh_time: parse_refresh_time("10m").unwrap(),
directory: Default::default(),
max_recursion_depth: 3,
}
}
}
fn parse_directory(arg: &str) -> Result<PathBuf, ParseError> {
let directory = PathBuf::from(arg);
if directory.exists() && directory.is_dir() {
Ok(PathBuf::from(arg))
} else {
Err(ParseError::new(
"path does not exist or is not a directory.",
))
}
}
fn parse_refresh_time(arg: &str) -> Result<humantime::Duration, ParseError> {
arg.parse::<humantime::Duration>().map_err(ParseError::from)
}