Jacobo de Vera
Jacobo de Vera

Reputation: 1933

Does any of the command line parsing libraries for C++ allow options with N arguments

I am porting a python script to C++ and have not found a way to express an argument such as:

--metadata KEY VALUE

In the C++ argument parsing libraries I've seen:

Maybe it exists as a possibility in one of those, maybe I have to explore others, maybe I just have to do this by hand.

Do you know of a C++ command line option parsing library that supports multiple arguments for options?

Upvotes: 0

Views: 223

Answers (1)

rici
rici

Reputation: 241901

Command-line flags which take more than one argument are discouraged by the standard shell utilities syntax guidelines. So I doubt whether you will find a standard library which handles it. It's more common to accept options in the format, for example:

--metadata KEY=VALUE

and you'll probably find standard libraries which will help you with that format (of course, strtok or std::string::find are straightforward to use, too).

If you really insist on the multiple-option argument design choice, you can do it with getopt_long if you tell it not to permute arguments (put a + as the first character of optstring); all you need to do is to manually increment optind as you read the arguments. (That is: the first argument will be in optarg, the next one at argv[optind++] and so on.) There are probably equivalent low-level hacks in the C++ libraries, too.

Upvotes: 1

Related Questions