Skip to content
Snippets Groups Projects
Commit f551f2b1 authored by David Flitney's avatar David Flitney
Browse files

Initial revision

parents
No related branches found
No related tags found
No related merge requests found
#include "options.h"
void string_to_T(bool &b, const string& s) {
if(s == "NO ARG")
b = !b;
else
b = (s == "true");
}
void string_to_T(string& d, const string& s) {
d = s;
}
void string_to_T(int& i, const string& s) {
i = atoi(s.c_str());
}
void string_to_T(float& v, const string& s) {
v = atof(s.c_str());
}
bool BaseOption::matches(const string& arg)
{
string::size_type pos = 0, np;
while((np = key_.find(",", pos)) != string::npos) {
if(arg == key_.substr(pos, np))
return true;
pos = np+1;
}
if(arg == key_.substr(pos, string::npos))
return true;
return false;
}
ostream& operator<<(ostream &os, const BaseOption& o) {
return os <<
"\t" << o.key() <<
"\t" << o.help_text();
}
void OptionParser::usage()
{
cerr << "Usage: " << endl << "\t" << progname_ << " " << example_ << endl;
cerr << endl << "Compulsory arguments:" << endl;
for(Options::iterator option = options_.begin(); option != options_.end();
option++)
{
if((*option)->compulsory())
cerr << **option << endl;
}
cerr << endl << "Optional arguments:" << endl;
for(Options::iterator option = options_.begin(); option != options_.end();
option++)
{
if(!(*option)->compulsory())
cerr << **option << endl;
}
cerr << endl;
cerr << endl;
}
// BaseOption * OptionParser::operator[](const string& key)
// {
// OptionMap::iterator option = options_.find(key);
// if(option != options_.end())
// return option->second;
// else // An unknown option!
// return NULL;
// }
unsigned int OptionParser::parse_command_line(unsigned int argc,
char **argv)
{
unsigned int argcount = 1;
bool usage_needed = false;
while(argcount < argc) {
unsigned int nmatches = 0;
unsigned int increments = 1;
string optstr(argv[argcount]), valstr;
if(argcount + 1 < argc)
valstr = string(argv[argcount+1]);
else
valstr = string("NO ARG");
if(optstr[0] != '-') // No more parsable options
break;
for(Options::iterator option = options_.begin();
option != options_.end();
option++) {
if((*option)->matches(optstr))
{
nmatches++;
if((*option)->required()) {
if(valstr != "NO ARG") {
(*option)->value(valstr);
increments = 2;
} else {
cerr << optstr << " : Missing argument!" << endl;
usage_needed = true;
}
} else if((*option)->optional()) {
if(valstr[0] != '-') {
(*option)->value(valstr);
increments = 2;
} else {
(*option)->value("NO ARG");
}
} else {
(*option)->value("NO ARG");
}
}
}
if(nmatches == 0) {
cerr << optstr << " : Unknown option!" << endl;
usage_needed = true;
}
argcount += increments;
}
// Now check that any compulsory options
// have been set
for(Options::iterator option = options_.begin();
option != options_.end();
option++) {
static bool banner = true;
if((*option)->compulsory() && (*option)->unset()) {
if(banner) {
cerr << "***************************************************" << endl;
cerr << "The following COMPULSORY options have not been set!" << endl;
banner = false;
usage_needed = true;
}
cerr << **option << endl;
}
}
if(usage_needed) {
cerr << "***************************************************" << endl;
usage();
exit(-1);
}
return argcount; // User should process any remaining args
}
//
// And now for the test stub...
//
#if defined(TESTING)
Option<bool> verbose(string("-V,--verbose"), false,
string("switch on diagnostic messages"),
false, BaseOption::no_argument);
Option<bool> help(string("-h,--help"), false,
string("display this message"),
false, BaseOption::no_argument);
Option<float> dof(string("-d,--dof"), 100.0,
string("number of degrees of freedom"),
true, BaseOption::requires_argument);
Option<string> mask(string("-m,--mask"), string("mask"),
string("brain mask volume"),
true, BaseOption::requires_argument);
Option<string> resid(string("-r,--res"), string("res4d"),
string("4d `residual-of-fit' image"),
true, BaseOption::requires_argument);
int main(unsigned int argc, char **argv) {
OptionParser options("options",
"-d <number> --mask <filename> --res <filename>");
options.add(&verbose);
options.add(&help);
options.add(&dof);
options.add(&mask);
options.add(&resid);
for(unsigned int a = options.parse_command_line(argc, argv); a < argc; a++)
cout << argv[a] << endl;
if(help.value())
options.usage();
if(verbose.value()) {
cout << "verbose = " << verbose.value() << endl;
cout << "help = " << help.value() << endl;
cout << "dof = " << dof.value() << endl;
cout << "mask = " << mask.value() << endl;
cout << "resid = " << resid.value() << endl;
}
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment