ArgMatches' get_one cannot downcast f64

Viewed 143

I have used clap crate to parsing arguments in my code. Minimal structure of my code about defining and parsing arguments are as follow.

use clap::builder::Command;
use clap::{Arg, ArgMatches};

let matches = Command::new("test")
                .arg(Arg::new("mass")
                    .short('m')
                    .takes_value(true))
                .get_matches()
let mass: f64 = *matches.get_one::<f64>("mass").unwrap();

But I face an error "thread 'main' panicked at 'Mismatch between definition and access of mass. Could not downcast to f64, need to downcast to alloc::string::String"

I can fix it by using parse() from String to f64.

let mass: f64 = *matches.get_one::<String>("mass").unwrap().parse().unwrap();

I want to know that why only f64 cannot be parsed by get_one function differ from the case of boolean or usize.

1 Answers

I found why the panic occurs, and how to prevent panic. The panic occurs because clap does not detect floating point type argument automatically. We should specify the value parser (or equivalently the type of argument) to clap (at the time we define command) for floating point or other custom types.

let matches = Command::new("test")
                .arg(Arg::new("mass")
                    .short('m')
                    .takes_value(true))
                    .value_parser(clap::value_parser!(f64))
                .get_matches()

Then, the panic is resolved. In the above code block, the result of clap::value_parser!(f64) is _AnonymousValueParser(ValueParser::other(f64))

Related