Tutorial:OptionParser

From MSL-Libraries

WARNING: Tutorial writing in progress, some errors may exist.


This is an example of how to add and use the MSL command-line parser ( OptionParser object ). This tutorial will also discuss the common conventions used when using OptionParser to write MSL programs. It is good to follow these conventions so that most MSL developers can easily read and help fix the option parsing code of any MSL program.


Complete source of example_option_parser.cpp

Complete source of example_option_parser.h


To compile

% make bin/example_option_parser

To run the program

Go to the main directory and run the command

% bin/example_option_parser

Program description

There are a couple of conventions for setting up a command-line parsing. One common one is to create a header file for each program (in this case example_option_parser.h), here we create an Options object which contains required and optional arguments, among other potential items. The rational for doing this, rather than placing directly in the cpp file is that it cleans up the program source file, so when people first read the program source code they can get right to the main function and start understanding the program, rather than understanding all the options first.


example_option_parser.h

#include <vector>
#include <string>
using namespace std;
struct Options {

	// Set up options here...
	Options(){

		required.push_back("arg1");
		required.push_back("arg2");
		required.push_back("arg3");
		required.push_back("flag1");

		optional.push_back("option1");
	}




	// Storage for the vales of each arg
       string arg1;
       int arg2;
       double arg3;
       bool flag1;
       string opt1;

       vector<string> required;
       vector<string> optional;
       vector<string> defaultArgs;
};


Options setupOptions(int theArgc, char * theArgv[]);

example_option_parser.cpp


#include "OptionParser.h"
#include "release.h"
#include "example_option_parser.h"

using namespace MSL;
using namespace std;
using namespace MslTools;

int main(int argc, char *argv[]) {

	Options opt = setupOptions(argc, argv);

}
	

Options setupOptions(int theArgc, char * theArgv[]){
	// Create the options
	Options opt;

	// Parse the options
	OptionParser OP;
	OP.readArgv(theArgc, theArgv);
	OP.setRequired(opt.required);	
	OP.setDefaultArguments(opt.defaultArgs); // the default argument is the --configfile option


	if (OP.countOptions() == 0){
		cout << "Usage: exampe_option_parser " << endl;
		cout << endl;
		cout << "\n";
		cout << "arg1 ARG1\n";
		cout << "arg2 ARG2\n";
		cout << "arg3 ARG3\n";
		cout << "flag1 TRUE\n";
		cout << "# option1 OPT1\n";
		cout << endl;
		exit(0);
	}

	opt.opt1 = OP.getString("arg1");
	if (OP.fail()){
		cerr << "ERROR 1111 no arg1 specified."<<endl;
		exit(1111);
	}


	opt.opt2 = OP.getInt("arg2");
	if (OP.fail()){
		cerr << "ERROR 1111 no arg2 specified."<<endl;
		exit(1111);
	}


	opt.opt3 = OP.getDouble("arg3");
	if (OP.fail()){
		cerr << "ERROR 1111 no arg3 specified."<<endl;
		exit(1111);
	}


	opt.flag1 = OP.getBool("flag1");
	if (OP.fail()){
		cerr << "WARNING 1111 no flag1 specified."<<endl;
	}

	opt.opt1 = OP.getString("option1");
	if (OP.fail()){
	  opt.opt1 = "defaultOption1";
	  cerr << "WARNING 1111 no option1 specified taking default value: "<<opt.opt1<<endl;
	}


	return opt;
}

Back to the tutorial page