I'm raising this as an enhancement request as I actually got some way to implementing it. This was an email sent some months ago...... I may have time to pick this up or perhaps others can suggest a better way.
Hi,
I'm adding a plugin system to my code. So some of the command line processing should be delegated to the plugin.
However the main command processing loads the plugin. So any plugin specific flags will cause an error.
So I can't see a way to do with with TCLAP right now.
What I think we need are wildcard flags that can be first ignored, then later processed.
Here is some example code:
in main()
...
TCLAP::CmdLine cmd("Command description message", ' ', "0.9");
TCLAP::ValueArg<std::string> nameArg("n","name","Name to print",true,"homer","string");
TCLAP::ValueArg<std::string> pluginArg("p","plugin","plugin to load",false,"","string");
cmd.add( nameArg );
TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);
cmd.wildcard_ignore("ext"); //Anything "-ext*" will be ignored
cmd.parse( argc, argv );</std::string></std::string>
std::string name = nameArg.getValue();
bool reverseName = reverseSwitch.getValue();
std::string pluginName = pluginArg.getValue();
plugin = plugin_init(cmd, argc, argv, "ext");
...
in plugin.cpp
...
void plugin_init(TCLAP::CmdLine & cmd, int argc, char** argv, const char * prefix)
{
...
TCLAP::ValueArg<std::string> emailArg("email","email","email address",false,"","string");</std::string>
cmd.wildcard_prefix(prefix) //Anything "-ext*" will be processed here
cmd.add(emailArg);
cmd.parse(argc, argv);
std::string emailAddress = emailArg.getValue();
sendEmail(emailAddress, content);
...
Hence the plugin specified by -p can update the command line processing to extend the command set.
i.e. Now we can invoke the following without TCLAP throwing an exception.
prog -n myname -p email.so -ext-email jason@test.com
Thanks for the feedback. I may try to poke around a bit in the weekend if I have time.
First thoughts on top of my mind:
1) This looks like a relatively special/narrow use-case, so any solution has to meet the criteria: a) don't complicate "normal" usage and b) not significantly increase the complexity of the code.
2) One simple way to approach this could be to have CmdLine not generate an error if there are unparsed arguments, but rather return the once that were not "consumed". That way it would be easy to chain multiple CmdLine parsers together (this is practically what you are suggesting except only certain patterns are ignored).
3) For your use-case, it may be sufficient to use the -- syntax (http://tclap.sourceforge.net/manual.html#IGNORE_ARGS) for this
Without additional input I'm considering implementing (2) as a reasonable and useful solution.
Doesn't appear to be any appetite for this. Closing as won't fix unless there are more concrete examples/requests for when this would be useful.