ArgvParser is a single C++ class that eases handling of command line options and arguments.
The code is extensively documented and easy to understand. The tarball contains a number of test cases (tests.cpp) that show the usage of the parser.
Here is a simple example that shows how to use the parser:
#include <iostream>;
#include "argvparser.h"
using namespace std;
using namespace CommandLineProcessing;
int main(int argc, char** argv)
{
ArgvParser cmd;
// init
cmd.setIntroductoryDescription("This is foo written by bar.");
//define error codes
cmd.addErrorCode(0, "Success");
cmd.addErrorCode(1, "Error");
cmd.setHelpOption("h", "help", "Print this help page");
cmd.defineOption("version", ArgvParser::NoOptionAttribute,
"Be verbose");
cmd.defineOptionAlternative("verbose","v");
cmd.defineOption("foo", ArgvParser::OptionRequiresValue,
"Fooishness. Default value: 0");
// finally parse and handle return codes (display help etc...)
int result = cmd.parse(argc, argv);
if (result != ArgvParser::NoParserError)
{
cout << cmd.parseErrorDescription(result);
exit(1);
}
// now query the parsing results
if (cmd.foundOption("foo"))
{
string = cmd.optionValue("foo");
...
}
...
}
This code is licensed under the GNU Public License (GPL).
There are a lot of C++ command line parsers out there. Two examples are: