TeXitoi / structopt

Parse command line arguments by defining a struct.
Other
2.71k stars 151 forks source link

RFC: parse_from_iter(iter: std::Vec::Drain) -> T #499

Closed EvanCarroll closed 2 years ago

EvanCarroll commented 3 years ago

Currently I can create a field that is a Vec<String> and it can take/gobble every argument in the command line. This is a really nice property of Vec<String>. It would be nice if either of these could do the same thing,

Currently, with types that are scalar you can write a custom a string parser that can provides the association needed to go from a String to a T in the StructOpt. Though nice, these custom-string parsers seem constricted to parsing single words (separated by whitespace). This is unfortunate because I can't write the functionality to recreate the Vec<String> parser.

Ideally, I could write a parser that took an std::vec::Drain and I could pull from creating my custom type until I was done. This would allow you to have any field of T be constructed with 0 or more arguments, leaving the rest for other arguments.

If this isn't possible, even a

parse_from_rest(&str) -> T; /// takes all words remaining in a string not just one.

would get me what I need.


The reason why I'm suggesting this is I can find a method to satisfactorily accomplish this. Feel free to suggest other methods if I am missing something.

TeXitoi commented 3 years ago

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.

EvanCarroll commented 3 years ago

@TeXitoi you may see it fit to file the bug on Clap if you think they can fix it and expose something structopt can use. I don't know enough about clap to do anything but point them here.

I just have a few use cases for this now so I figure someone else must find this useful. Note on the StackOverflow question there is someone who says there is a method of doing what I want https://stackoverflow.com/questions/69436974/can-you-build-a-structopt-parser-that-takes-the-rest-of-the-command-line-or-mul#comment122735563_69439424 but he hasn't responded to my inquiry yet.

TeXitoi commented 3 years ago

clap v2, that structopt uses, is feature frozen. New functionnalities is implemented in clap v3, that is not yet released, and that includes its own version of structopt (named clap derive).

EvanCarroll commented 3 years ago

Great information, well let's hope clap v3 has this. I think we can close RFC then. It sounds like the whole project is soon to be deprecated with clap derive.

TeXitoi commented 3 years ago

Well, that's 2 years that clap v3 is in alpha stage...

TeXitoi commented 2 years ago

As a workaround, you can impl a method on your Opt struct that "parse" the Vec<String>.