简体   繁体   中英

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(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(...)

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

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"). 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 . 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM