I am using getopt_long to pass arguments to a C++ program. Arguments are passed with a key and a value separated by a space of '=' sign. Some options do not need to assign a value to them.
Example:
./raytrac dist=0.3 --color
I have some classes that need to use some of these parameters ant created a class Parsing to handle the arguments passed from argv.
bool Parsing::get_int
bool Parsing::get_float
bool Parsing::get_vect2
bool Parsing::get_string
Then if a class (let us say Tomog) needs those parameters I call
Tomog::set_param(Pc)
Where Pc is of class Parsing. Pc is a list associating a key with the corresponding argument. If the key matches you get the value.
Example
float f;
if (Pc.get_string("--dist",f)) {
Hence if the user passed --dist=0.3, f will contain the value 0.3
I am wondering what people think about this system of passing command line variables. And suggestions if there are better ways to do this.
Some more details of Tomog::set_param is given below
void Tomog::set_param (
Parsing& Pc
) {
int i;
int nf;
String s;
String T;
if (Pc.get_string("--lglvl",s)) {
if ( !get_log_level(s, lglvl) ) {
error("invalid value for --lglvl");
}
cerr << "Tomog::set_param, --lglvl = " << s << endl;
} else {
lglvl = normal;
cerr << "Tomog::set_param, --lglvl = normal = " << lglvl << endl;
}
float f;
if (Pc.get_float("--dist", f)) {
nf = s.nfields(':');
if (nf < NLayers) {
error ("value for --nzs is less than the number of layers");
}
cerr << "Tomog::SetParam, nzs = " << s << endl;
for (i = 0; i < NLayers; i++) {
T = s.get_token(':');
T >> NzS;
}
} else {
NzS.fill(2);
}
ifstream ifs_base;
Parsing Pb;
if ( !Pc.get_string("--ifbase", s) ) {
error ("no value for --ifbase");
}
ifs_base.open(s);
if (ifs_base.bad()) {
error ("ifs_base failed to open");
}
Pb.parse_file(ifs_base);
if (lglvl >= medium) {
cerr << "Tomog::set_param, --ifbase = " << s << endl;
}
// get model boundaries from base file
Vect2 Xi;
Vect2 Xf;
Pb.get_vect2("XI", Xi);
Pb.get_vect2("XF", Xf);
cerr << "Tomog::set_param: Xi = " << Xi << endl;
cerr << "Tomog::set_param: Xf = " << Xf << endl;
// Read Travel Time Data File --------------------------------------------------
Parsing Pd;
// tdat ----------------------------------------------------------------------
// name of file containing travel times
ifstream ifs_tdat;
if ( !Pc.get_string("--iftdat", s) ) {
error ("no value for --iftdat");
}
ifs_tdat.open (s);
if (ifs_tdat.bad()) {
error ("ifs_tdat failed to open");
}
cerr << "Tomog::SetParam, ifs_tdat = " << s << endl;
Pd.parse_file (ifs_tdat);
Mod.set_data(Pd);
Mod.set_param(Pc); // calling ModMisfit::SetRealData