jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.16k stars 582 forks source link

Feature request: value pairs or tuples #394

Open maickrau opened 1 year ago

maickrau commented 1 year ago

Hi, I'd like an option to input two values for a parameter and take them as an input. This would differ from std::vector values in that the number of values given is constant. The use case I was trying to solve is optionally inputting a file with data and an another file with metadata describing the previous file, where the user can either include both or leave both out but including one without the other is invalid. Here's an example of what it would look like:

options.add_options()("file", "File and metadata", cxxopts::value<std::pair<std::string, std::string>>())
./main --file file1 metadata1

Ideally this could also be used with std::vector

options.add_options()("file", "File and metadata", cxxopts::value<std::vector<std::pair<std::string, std::string>>>())
./main --file file1 metadata1 --file file2 metadata2

and similarly with std::tuple for more than 2 values.

jarro2783 commented 1 year ago

The main difficulty would be parsing these together. It seems reasonable that you could split the values with a comma. Then I could support a pair or tuple and expect exactly the number of arguments in the type. For example

cxxopts::value<std::tuple<int, int, int>>

would accept --option 1,2,3