Skip to content

Commit

Permalink
Nicely report invalid speed and quality CLI args
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
kornelski committed May 6, 2023
1 parent 1e37f3f commit aff2bb2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ enum MaybePath {
Path(PathBuf),
}

fn parse_quality(arg: &str) -> Result<f32, String> {
let q = arg.parse::<f32>().map_err(|e| e.to_string())?;
if q < 1. || q > 100. {
return Err("quality must be in 1-100 range".into());
}
Ok(q)
}

fn parse_speed(arg: &str) -> Result<u8, String> {
let s = arg.parse::<u8>().map_err(|e| e.to_string())?;
if s < 1 || s > 100 {
return Err("speed must be in 1-10 range".into());
}
Ok(s)
}

fn run() -> Result<(), BoxError> {
let args = Command::new("cavif-rs")
.version(clap::crate_version!())
Expand All @@ -40,15 +56,15 @@ fn run() -> Result<(), BoxError> {
.short('Q')
.long("quality")
.value_name("n")
.value_parser(value_parser!(f32))
.value_parser(parse_quality)
.default_value("80")
.help("Quality from 1 (worst) to 100 (best)"))
.arg(Arg::new("speed")
.short('s')
.long("speed")
.value_name("n")
.default_value("4")
.value_parser(value_parser!(u8))
.value_parser(parse_speed)
.help("Encoding speed from 1 (best) to 10 (fast but ugly)"))
.arg(Arg::new("threads")
.short('j')
Expand Down

0 comments on commit aff2bb2

Please sign in to comment.