argmatches' get_one不能击落f64

发布于 2025-02-12 09:04:23 字数 828 浏览 1 评论 0原文

我已经使用了 clap板条板在我的代码中解析参数。 关于定义和解析论点的代码的最小结构如下。

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();

但是我遇到了一个错误“线程'main'对'定义和访问质量的访问之间的不匹配感到惊慌。 /strong>

我可以通过使用parse()从字符串到f64修复它。

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

我想知道为什么只有f64不能被get_one函数解析与布尔或用户的情况不同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2025-02-19 09:04:24

我发现了为什么恐慌会发生,以及如何防止恐慌。
恐慌之所以发生,是因为拍手不会自动检测浮点类型参数。
我们应该指定浮点或其他自定义类型的值(或定义命令时)的值(或等效的参数类型)。

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

然后,恐慌解决了。
在上面的代码块中,clap :: value_parser!(f64)的结果_AnonyMONNOMNVALUEPARSER(valyeparser :: ether(f64))>

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))

挽心 2025-02-19 09:04:24

就我而言,它只是无法将值解析为所提供的类型(并显示不充分的错误消息)。 I类型试图按照&amp; str

matches.get_one::<&str>("arg");

这使程序感到恐慌,因为它未能将输入解析为&amp; str。我使用的解决方案是将其解析为String,然后将其转换为&amp; str

matches.get_one::<String>("arg").map(|s| s.as_str());

In my case, it was simply failing to parse the value as the provided type (and displaying a not-very-helpful error message). The type I was trying to parse it as was &str:

matches.get_one::<&str>("arg");

This was making the programme panic, as it fails to parse the input as &str. The solution I used was to parse as String and then convert it to a &str:

matches.get_one::<String>("arg").map(|s| s.as_str());
冰雪梦之恋 2025-02-19 09:04:24

它不是浮点或任何其他合并:

let in_file_name: PathBuf;
if let Some(infile) = matches.get_one::<PathBuf>("infile") {
    in_file_name = infile.to_owned().into();

工作时遇到相同的问题

let in_file_name: PathBuf;

if let Some(infile) = matches.get_one::<String>("infile") {
    in_file_name = infile.to_owned().into();


-Michael

It's not floating point or any other comnversion:

let in_file_name: PathBuf;
if let Some(infile) = matches.get_one::<PathBuf>("infile") {
    in_file_name = infile.to_owned().into();

has the same issue while

let in_file_name: PathBuf;

if let Some(infile) = matches.get_one::<String>("infile") {
    in_file_name = infile.to_owned().into();

works.
-Michael

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文