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

*** empty log message ***

parent 343602dc
No related branches found
No related tags found
No related merge requests found
#include "options.h" #include "options.h"
void string_to_T(bool &b, const string& s) { namespace Options {
if(s == "NO ARG")
b = !b;
else
b = (s == "true");
}
void string_to_T(string& d, const string& s) { void string_to_T(bool &b, const string& s) {
d = s; if(s == "NO ARG")
} b = !b;
else
b = (s == "true");
}
void string_to_T(int& i, const string& s) { void string_to_T(string& d, const string& s) {
i = atoi(s.c_str()); d = s;
} }
void string_to_T(float& v, const string& s) { void string_to_T(int& i, const string& s) {
v = atof(s.c_str()); 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) bool BaseOption::matches(const string& arg)
{ {
string::size_type pos = 0, np; string::size_type pos = 0, np;
while((np = key_.find(",", pos)) != string::npos) { while((np = key_.find(",", pos)) != string::npos) {
if(arg == key_.substr(pos, np - pos)) if(arg == key_.substr(pos, np - pos))
return true;
pos = np + 1;
}
if(arg == key_.substr(pos, string::npos))
return true; return true;
pos = np + 1; return false;
} }
if(arg == key_.substr(pos, string::npos))
return true;
return false;
}
ostream& operator<<(ostream &os, const BaseOption& o) { ostream& operator<<(ostream &os, const BaseOption& o) {
return os << return os <<
"\t" << o.key() << "\t" << o.key() <<
"\t" << o.help_text(); "\t" << o.help_text();
} }
void OptionParser::usage() void OptionParser::usage()
{ {
cerr << endl << progname_ << endl << endl; cerr << endl << progname_ << endl << endl;
cerr << "Usage: " << endl << example_ << endl; cerr << "Usage: " << endl << example_ << endl;
for(Options::iterator option = options_.begin(); option != options_.end(); for(Options::iterator option = options_.begin(); option != options_.end();
option++) option++)
{ {
if((*option)->compulsory()) { if((*option)->compulsory()) {
static bool banner = true; static bool banner = true;
if(banner) { if(banner) {
cerr << endl << "Compulsory arguments (You MUST set one or more of):" << endl; cerr << endl << "Compulsory arguments (You MUST set one or more of):" << endl;
banner = false; banner = false;
}
cerr << **option << endl;
} }
cerr << **option << endl;
} }
}
for(Options::iterator option = options_.begin(); option != options_.end(); for(Options::iterator option = options_.begin(); option != options_.end();
option++) option++)
{ {
if(!(*option)->compulsory()) { if(!(*option)->compulsory()) {
static bool banner = true; static bool banner = true;
if(banner) { if(banner) {
cerr << endl << "Optional arguments (You may optionally specify one or more of):" << endl; cerr << endl << "Optional arguments (You may optionally specify one or more of):" << endl;
banner = false; banner = false;
}
cerr << **option << endl;
} }
cerr << **option << endl;
} }
}
cerr << endl; cerr << endl;
cerr << endl; cerr << endl;
} }
bool OptionParser::check_compulsory_arguments(bool verbose) bool OptionParser::check_compulsory_arguments(bool verbose)
{ {
bool okay = true; bool okay = true;
for(Options::iterator option = options_.begin(); for(Options::iterator option = options_.begin();
option != options_.end(); option != options_.end();
option++) { option++) {
if((*option)->compulsory() && (*option)->unset()) { if((*option)->compulsory() && (*option)->unset()) {
if(okay) { if(okay) {
if(verbose) { if(verbose) {
cerr << "***************************************************" << endl; cerr << "***************************************************" << endl;
cerr << "The following COMPULSORY options have not been set:" << endl; cerr << "The following COMPULSORY options have not been set:" << endl;
}
okay = false;
} }
okay = false; if(verbose)
cerr << **option << endl;
} }
if(verbose)
cerr << **option << endl;
} }
} if(!okay && verbose)
if(!okay && verbose) cerr << "***************************************************" << endl;
cerr << "***************************************************" << endl;
return okay; return okay;
} }
unsigned int OptionParser::parse_command_line(unsigned int argc,
char **argv)
{
unsigned int argcount = 1;
bool usage_needed = false;
while(argcount < argc) { unsigned int OptionParser::parse_command_line(unsigned int argc,
unsigned int nmatches = 0; char **argv)
unsigned int increments = 1; throw(X_OptionError, X_UnknownOptions, X_MissingArguments)
{
unsigned int argcount = 1;
bool unknown_opts = false;
bool missing_args = false;
while(argcount < argc) {
unsigned int nmatches = 0;
unsigned int increments = 1;
string optstr(argv[argcount]), valstr; string optstr(argv[argcount]), valstr;
if(argcount + 1 < argc) if(argcount + 1 < argc)
valstr = string(argv[argcount+1]); valstr = string(argv[argcount+1]);
else else
valstr = string("NO ARG"); valstr = string("NO ARG");
if(optstr[0] != '-') // No more parsable options if(optstr[0] != '-') // No more parsable options
break; break;
for(Options::iterator option = options_.begin(); for(Options::iterator option = options_.begin();
option != options_.end(); option != options_.end();
option++) { option++) {
if((*option)->matches(optstr)) if((*option)->matches(optstr))
{ {
nmatches++; nmatches++;
if((*option)->required()) { if((*option)->required()) {
if(valstr != "NO ARG") { if(valstr != "NO ARG") {
(*option)->value(valstr); (*option)->value(valstr);
increments = 2; increments = 2;
} else { } else {
cerr << optstr << " : Missing argument!" << endl; cerr << optstr << ": missing argument!" << endl;
usage_needed = true; missing_args = true;
} }
} else if((*option)->optional()) { } else if((*option)->optional()) {
if(valstr[0] != '-') { if(valstr[0] != '-') {
(*option)->value(valstr); (*option)->value(valstr);
increments = 2; increments = 2;
} else {
(*option)->value("NO ARG");
}
} else { } else {
(*option)->value("NO ARG"); (*option)->value("NO ARG");
} }
} else {
(*option)->value("NO ARG");
} }
} }
} if(nmatches == 0) {
if(nmatches == 0) { cerr << optstr << ": unknown option!" << endl;
cerr << optstr << " : Unknown option!" << endl; unknown_opts = true;
usage_needed = true; }
argcount += increments;
} }
argcount += increments;
if(unknown_opts && missing_args)
throw X_OptionError();
else if(unknown_opts)
throw X_UnknownOptions();
else if(missing_args)
throw X_MissingArguments();
return argcount; // User should process any remaining args
} }
return argcount; // User should process any remaining args
} }
// //
// And now for the test stub... // And now for the test stub...
// //
#if defined(TESTING) #if defined(TESTING)
using namespace Opts;
Option<bool> verbose(string("-V,--verbose"), false, Option<bool> verbose(string("-V,--verbose"), false,
string("switch on diagnostic messages"), string("switch on diagnostic messages"),
false, BaseOption::no_argument); false, no_argument);
Option<bool> help(string("-h,--help"), false, Option<bool> help(string("-h,--help"), false,
string("display this message"), string("display this message"),
false, BaseOption::no_argument); false, no_argument);
Option<float> dof(string("-d,--dof"), 100.0, Option<float> dof(string("-d,--dof"), 100.0,
string("number of degrees of freedom"), string("number of degrees of freedom"),
true, BaseOption::requires_argument); true, requires_argument);
Option<string> mask(string("-m,--mask"), string("mask"), Option<string> mask(string("-m,--mask"), string("mask"),
string("brain mask volume"), string("brain mask volume"),
true, BaseOption::requires_argument); true, requires_argument);
Option<string> resid(string("-r,--res"), string("res4d"), Option<string> resid(string("-r,--res"), string("res4d"),
string("4d `residual-of-fit' image"), string("4d `residual-of-fit' image"),
true, BaseOption::requires_argument); true, requires_argument);
int main(unsigned int argc, char **argv) { int main(unsigned int argc, char **argv) {
OptionParser options("options", OptionParser options("options",
"-d <number> --mask <filename> --res <filename>"); "-d <number> --mask <filename> --res <filename>");
options.add(verbose); try {
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++) options.add(verbose);
cout << argv[a] << endl; options.add(help);
options.add(dof);
if(help.value()) options.add(mask);
options.usage(); options.add(resid);
if(verbose.value()) { for(unsigned int a = options.parse_command_line(argc, argv); a < argc; a++)
cout << "verbose = " << verbose.value() << endl; cout << argv[a] << endl;
cout << "help = " << help.value() << endl;
cout << "dof = " << dof.value() << endl; if(help.value())
cout << "mask = " << mask.value() << endl; options.usage();
cout << "resid = " << resid.value() << endl;
} 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;
}
} catch(X_OptionError& e) {
options.usage();
cerr << endl << e.what() << endl;
} catch(std::exception &e) {
cerr << e.what() << endl;
}
} }
#endif #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