This is the C++ equivalent of the old good C getopt library. This library parses command line options and has the ability to add handlers to each option.
#include<cassert>
#include<iostream>
#include<GetOptPP/ConsoleKeyOption.h>
#include<GetOptPP/ConsoleOptionsHandler.h>//Run arguments: -a --some --dummy dummy0 -d dummy1 --extended ext0 -e ext1intmain(int argc, char **argv) {
// Create the handler
GetOptPlusPlus::ConsoleOptionsHandler consoleHandler(argc, argv);
// Add handlers
consoleHandler.AddKey({nullptr, "a", 0}, [](constchar*) { std::cout << R"("-a" flag without long opt and param)" << std::endl; });
consoleHandler.AddKey({"some", nullptr, 0}, [](constchar*) { std::cout << R"("--some" opt without short arg and param)" << std::endl; });
consoleHandler.AddKey({"dummy", nullptr, 1}, [](constchar* p) { std::cout << R"("--dummy" opt without short arg and with param:)" << p << std::endl; });
consoleHandler.AddKey({nullptr, "d", 1}, [](constchar* p) { std::cout << R"("-d" opt without long arg and with param:)" << p << std::endl; });
consoleHandler.AddKey({"extended", "e", 1}, [](constchar* p) { std::cout << R"("extended" opt and "e" param:)" << p << std::endl; });
// Re-adding the handlerbool isAdded = consoleHandler.AddKey({"some", nullptr, 0}, [](constchar*) { std::cout << R"("--some" opt without short arg and param)" << std::endl; });
assert( isAdded == false );
// Process the args and their optionsif (auto argcnt = consoleHandler.ProcessCmdLine(); consoleHandler.HandlersCount() > 0) {
std::cout << "Successfully processed: " << std::to_string(argcnt) << std::endl;
}
return0;
}