|
There is a lot of command line parsing libraries, classes and other stuff. I also contributed to this movement creating my own class TCommandLineParser, doing command line processing work. This is not for self advertisement but I successfully used TCommandLineParser with different compilers: Borland C++, Visual C++, Compaq C++ (former DEC C++), aCC (C++ for HP-UX) and found this tool really convenient. |
||||||||||||||||||||
|
Let start with simple example:
|
||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
|
Now, one more example from real life:
///////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
TCommandLineParser commandline;
commandline.AddChar("on");
commandline.AddPosInt("id", true);
commandline.AddChar("app", true);
commandline.AddVoid("trace");
commandline.AddVoid("authenticate");
commandline.AddBoundedInt("port", 12300, 12600);
commandline.AddChar("assoc");
commandline.AddArguments(argc, argv);
if(!commandline.CheckCommandLine())
{
printf("Invalid command line!\n");
Usage();
return 1;
}
if(strlen(commandline.GetChar("app")) > 11)
{
printf("Invalid application name %s (Too long)!\n", commandline.GetChar("app"));
return 1;
}
// if port number isn't specified in command line - set default value
if(!commandline.FindKey("port"))
commandline.AddArgument("/port=12300");
And example of valid command line for above code:
Download TCommandLineParser code and sample program.
Back