Libsmart's argument scanner

So you are all enthusiastic users of getopt() and think this is a good way to parse command line arguments? Wait, Libsmart provides you with a more C++‘ish way to specify command line parsing using the smart/util/argscan module.

Compared to getopt() the argscan module additionally supports:

  • automatic formatting and generation of a command line option summary (the one printed using —help)
  • simplified specification of options
  • controlled conversion of arguments to arbitrary types.
  • alias names
  • value triggers and option counters

A little example to get your attention:

#include 
#include 

using std::cout;
using std::cerr;
using std::endl;

using std::string;

int main(int argc, char* const argv[])
{ using namespace smart::util::ArgScan;

try { ArgumentScanner as; // some informal lines to support our option summary as << usage(”[OPTIONS] ARGS...”); as << group(“Meta options”); // our first option —help is a special one as << help(“help”, “show this summary of options”); // set a new group as << group(“Filename and conversion options”); // A more concrete option, pass a filename using -f, the argument is required. std::string filename; as << required(“filename”, filename, “The filename to use”, “FILE”); // additionally we want a shortcut to —filename as << alias(‘f’); // I’d like to set a verbose level, multiple ‘v’ options will increase the level, // so we use the counter() mechanism. size_t verboseLevel = 0; as << counter(‘v’, verboseLevel, “verbosity level, each -v increases the level by one”) << alias(“verbose”); // directly set a value if the option is detected. Note: you can use any type here. bool utfTranslation = false; as << value(“utf”, true, utfTranslation, “supports UTF8 translation”) << alias(‘u’); // So, that’s enough for a simple example, finally parse the options: if (!as(argc, argv)) { // —help seen and argument scanner printed help. return EXIT_SUCCESS; } // scan was ok, return the pending arguments cout << “Scan Ok, pending arguments: “ << as.pendingArguments().size() << endl; return EXIT_SUCCESS; } catch(const std::exception& e) { cerr << “Error: “ << e.what() << endl; return EXIT_FAILURE; } }

So, basically you create an ArgumentScanner class from the smart::util::ArgScan namespace, fill in the option parsing primitives using the << operator, and call the instance like a function expecting argc and argv parameters. On return you get the state of the parsing (Ok, or user typed in the --help option) or an exception if the parsing failed. Optionally you can access the remaining arguments using the pendingArguments() member function.

Well, besides, here is what this example will print when --help is passed as an option:


Usage: argscan [OPTIONS] ARGS...

Meta options: —help show this summary of options

Filename and conversion options: -f, —filename FILE The filename to use -v, —verbose verbosity level, each -v increases the level by one -u, —utf supports UTF8 translation

The summary of options: automatically grouped, aligned and formatted, neat.

The argscan module is part of Libsmart.