简体   繁体   English

你能构建一个接受命令行 rest(或多个参数)的 structopt 解析器吗?

[英]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 than FromStr , you may provide a custom string parser using parse(...)如果字段类型没有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.我想知道是否有一种方法可以编写理解这些细微差别(如上所述)的解析器,或者可以重新排序向量以适应它们。

No

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 (which structopt 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 in structopt , 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.

相关问题 使用 clap 和 structopt 获取不同命令行选项的相对顺序 - Getting relative order of different command line options using clap & structopt 如何使用StructOpt将参数解析为Vec而不将其视为多个参数? - How to use StructOpt to parse an argument into a Vec without it being treated as multiple arguments? 是否可以在不解析命令行参数的情况下构建 StructOpt Args object 进行测试? - Is it possible to construct a StructOpt Args object without parsing command line params for testing? StructOpt:如何将所有参数组合在一个字符串中? - StructOpt: how to combine all arguments in a single string? 使用structopt定义自定义解析器,该解析器基于另一个标志进行更改 - Define a custom parser using structopt that changes based on another flag 在单独的文件中定义时,structopt 无法找到其 Args 解析器 - structopt unable to find its Args parser when defined in a separate file 如何在 structopt 中使用枚举? - How can I use enums in structopt? 如何验证命令行 arguments 可以在给定数组中找到? - How to verify that the command line arguments can be found in a given array? 可以使用切片模式在不克隆的情况下解析命令行参数吗? - Can a slice pattern be used to parse command line arguments without cloning? 如何创建所有子命令都是可选的 StructOpt 命令 - How to create a StructOpt command where all subcomands are optional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM