[英]Can you build a structopt parser that takes the rest of the command line (or multiple arguments)?
structopt
has a neat feature where it can it accept a typed argument for Vec<T>
that will gobble the rest of the command line. structopt
有一个巧妙的功能,它可以接受Vec<T>
的类型化参数,这将吞噬命令行的 rest。
#[structopt(long, short)]
values: Vec<u32>,
It also has the ability to accept a type you create ,它还具有接受您创建的类型的能力,
If the field type does not have a
FromStr
implementation, or you would like to provide a custom parsing scheme other thanFromStr
, you may provide a custom string parser usingparse(...)
如果字段类型没有FromStr
实现,或者您想提供FromStr
以外的自定义解析方案,您可以使用parse(...)
提供自定义字符串解析器
Can the type that you create not be a scalar and either have multiple values or wrap a Vec<T>
is there a method to create a custom parser for a type that wraps Vec<T>
but reorders it's arguments or a struct that includes more than one field, for example like this您创建的类型是否可以不是标量并且具有多个值或包装Vec<T>
是否有一种方法可以为包装Vec<T>
但对其重新排序的类型创建自定义解析器arguments 或包含更多的结构不止一个字段,例如像这样
struct Positionals {
first: String,
increment: String,
last: String
}
I need a different structure because the arguments name is contingent on the ordering, (if there is one argument it's just "last" if there is two arguments the first argument is not last, it's "first" and the second argument is "last").我需要一个不同的结构,因为 arguments 名称取决于顺序,(如果有一个参数,它只是“最后一个”,如果有两个 arguments 第一个参数不是最后一个,它是“第一个”,第二个参数是“最后一个” ). I was wondering if there was a way to write a parser that understands these nuances (as above) or can reorder a vector to accommodate for them.我想知道是否有一种方法可以编写理解这些细微差别(如上所述)的解析器,或者可以重新排序向量以适应它们。
I don't believe this is currently possible.我不相信这是目前可能的。 I opened up an RFC for a parser that gets a vec::Drain
, or a parser that get the rest of the command line .我为获取vec::Drain
的解析器或获取命令行的 rest 的解析器打开了一个 RFC 。 I also asked for alternative ways to accomplish this.我还询问了其他方法来实现这一点。 The response was,回应是,
The
clap
infrastructure (whichstructopt
only expose on top of struct attribute) doesn't support this as far as I know.据我所知,clap
基础结构(structopt
仅在 struct 属性之上公开)不支持这一点。 Validators in clap (that is named parser instructopt
, as they validate and parse) are implemented on each argument, not on a set of arguments. clap 中的验证器(在structopt
中被命名为解析器,因为它们验证和解析)是在每个参数上实现的,而不是在一组 arguments 上实现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.