Grep-like argument parsing with clap

Hey there! I'm writing a program that works kind of like grep, so I'd like to parse the command-line arguments kind of like grep too, specifically the -e flag. But, I'm having trouble figuring out how to do this in clap. Basically, I want these rules to be true:

  • If you specify two arguments, they're taken as the language of a search and the query, e.g. ./tool rust '(_)'
  • If you specify other positional arguments after that, they're used as paths to search, e.g. ./tool rust '(_)' here/there/everywhere
  • If you want to specify multiple queries, you add -q before each, e.g. ./tool -q rust '(_)@q1' -q rust '(_)@q2'. This works with the files as well.

I'm having no luck doing this with Clap, even with the 3.0.0-beta.2 release. This seems like an unusual but not unheard-of pattern elsewhere. Is it too unusual for Clap? Am I going to have to drop down to parsing my own arguments out of argv?

For now I've just required -q for every argument, but I'd really like to not do that long term. Any other ideas?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.