I can't find an easy way to implement subcommands (similar to how it's done in git).
There is this question on stackoverflow but I don't know if the OP was able to contact you.
https://stackoverflow.com/questions/25083814/c-cli-subcommands-with-tclap
For now I'm achieving it in the following way:
I defined:
An UnlabeledValueArg
to recognize the subcommand
An then an UnlabeledMultiArg
to recognize the args of the subcommand
Then I parse the vector resulting of prepending the subcommand to the subcommand args with another subcommand-specific CmdLine
There is a problem if you want to have a help for the command and also one for the subcommand (as you can do in git), the --help
flag is parsed as part of the command even if it is after the UnlabeledValueArg
with which the subcommand is recognized. For now I fix this by:
Attaching an IgnoreRestVisitor
to the UnlabeledValueArg
that the subcommand was recognized with (see https://sourceforge.net/p/tclap/bugs/39/).
Then I require a function that allows not to ignore in the next parse, since it does not exist, I implement it in an analogous way to Arg::beginIgnoring()
Note: The last problem not only happens with --help
but it can happen with other flags that exist both in a command and in a subcommand.
Please let me know if you can think of a better way to do this. Or if I can help with something.
Thanks!
Thanks for the feedback, and sorry for the delay - I was traveling. I responded to the OP on SO, but here it is for posterity:
This isn't really part of the design of TCLAP. It's focused on providing "POSIX style" command line flags/arguments, which was the style of the day. It would probably not be too difficult to provide a git-style parser in the same spirit of TCLAP, but as of now TCLAP is probably to wrong tool for the job.