如何更改采用多个值的 clap 参数中值的名称?
How do I change the names of the values in a clap argument that takes multiple values?
作为我的 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(()),
}
)
)
作为我的 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 argumentNOTE: 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(()),
}
)
)