【发布时间】:2019-06-24 01:03:29
【问题描述】:
我使用 structopt 来解析我的 rust 应用程序的命令行参数。有问题的标志如下:query(位置)和case_sensitive(可选)。
#[derive(StructOpt, Debug)]
pub struct Config {
/// Query to search for.
#[structopt(parse(try_from_str = "parse_regex"))]
query: Regex,
/// Specify whether or not the query is case sensitive.
#[structopt(long)]
case_sensitive: bool,
}
我最终想做的是写parse_regex,它从查询字符串参数构建一个正则表达式。
fn parse_regex(src: &str) -> Result<Regex, Error> {
let case_sensitive = true; // !!! problem here: how to grab the value of the `case_sensitive` flag?
RegexBuilder::new(src).case_insensitive(!case_sensitive).build()
}
我想知道的是自定义解析函数是否可以获取另一个标志的值(在本例中为 case_sensitive),以便动态解析自己的标志。
【问题讨论】:
-
标志通常以任意顺序传递,你建议怎么做?