'C++ subcommand-like argument parsing

I'm writing a program in C++ that is expected to behave like SVN, or systemctl, or some other tools:

./a.out SUBCOMMAND [OPTION]... arguments-for-the-subcommand

An example would be something like the following:

./a.out remove --recursive directory1 directory2

The --recursive option is only valid for the remove subcommand.

I'm currently using Boost.ProgramOptions, and have had no luck in my tries (the only thing I have right now is a program that accepts arguments the GNU getopt way). Although I would prefer something wide-tested, there's no problem in switching to another library if it is portable and does its job properly.

Thanks in advance, and sorry if the question is not well explained.

Best regards, Kalrish.

P.S.: Not sure if this is of any help, but I'm currently using GCC 4.7.2.



Solution 1:[1]

It should actually pretty simple with Boost program-options, as you then easily can construct multiple option lists.

You create one for each sub-command, and then check argv[1] to see what the subcommand is and use the correct option list.

Solution 2:[2]

There is a header-only C++ library CLI11.
Basic usage on sub-command:

CLI::App* sub = app.add_subcommand("sub", "This is a subcommand");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Some programmer dude
Solution 2 Jasper Li