gpt4 book ai didi

rust - 如何更改采用多个值的 clap 参数中值的名称?

转载 作者:行者123 更新时间:2023-11-29 07:59:22 24 4
gpt4 key购买 nike

作为我的 CLI 工具的一部分,我有一个 clap::Arg取多个值,表示 (x, y)协调。我希望使用能够将值作为 -p/--position 1 0 传递

.arg(
clap::Arg::with_name("position")
.help("The position for yada yada yada")
.long("position")
.short("p")
.number_of_values(2)
.validator(|p| match p.parse::<usize>() {
Err(_) => Err(String::from("Error string")),
Ok(_) => Ok(()),
}
)
)

虽然这适用于我想要的界面,但这会产生一些令人困惑的帮助消息:

... Help text ...

OPTIONS:
... other options ...
-p, --position <position> <position> The position for yada yada yada

这里困扰我的是 -p, --position <position> <position> ,这似乎表明 两个 位置正在传递给参数。有什么办法可以替换 <position> <position>用我选择的琴弦? (我的目标是在帮助消息中获得类似于 -p, --position <x> <y> 的内容。)

最佳答案

快速浏览文档给我们 value_names() :

Specify multiple names for values of option arguments. These names are cosmetic only, used for help and usage strings only. The names are not used to access arguments. The values of the arguments are accessed in numeric order (i.e. if you specify two names one and two one will be the first matched value, two will be the second).

NOTE: This implicitly sets Arg::number_of_values if the number of value names is greater than one. I.e. be aware that the number of "names" you set for the values, will be the exact number of values required to satisfy this argument

NOTE: implicitly sets Arg::takes_value(true)

.arg(
clap::Arg::with_name("position")
.help("The position for yada yada yada")
.long("position")
.short("p")
.value_names(&["x", "y"])
.validator(|p| match p.parse::<usize>() {
Err(_) => Err(String::from("Error string")),
Ok(_) => Ok(()),
}
)
)

关于rust - 如何更改采用多个值的 clap 参数中值的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com