This repository has been archived on 2026-03-28. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
eodev/eo/test/t-parser.cpp
1999-10-29 11:23:10 +00:00

165 lines
4.1 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
/* parser.cpp
example of use of Parser.h
(c) geneura team, 1999
-----------------------------------------------------------------------------*/
#include <eoParser.h>
#include <eoRNG.h>
#include <sys/time.h>
void GetOutputParam(Parser & parser,
string & _string) {
try {
parser.AddTitle("Separate parameter: the output file name");
_string = parser.getString("-O", "--OutputFile", "", "The output file name" );
} catch (UException & e) {
cout << e.what() << endl;
parser.printHelp();
exit(1);
} catch (exception & e) {
cout << e.what() << endl;
exit(1);
}
}
void sub(Parser & parser) {
int i;
cout << "Function sub:" << endl;
try {
parser.AddTitle("Private parameters of subroutine sub");
i = parser.getInt("-j", "--sint", "5", "private integer of subroutine" );
} catch (UException & e) {
cout << e.what() << endl;
parser.printHelp();
exit(1);
} catch (exception & e) {
cout << e.what() << endl;
exit(1);
}
cout << "Read " << i << endl;
}
/// Uses the parser and returns param values
void getParams( Parser & parser,
unsigned & _integer,
float & _floating,
string & _string,
vector<string> & _array,
bool & _boolean) {
try {
_integer = parser.getInt("-i", "--int", "2", "interger number" );
_floating = parser.getFloat("-f", "--float", "0.2", "floating point number" );
_string = parser.getString("-s", "--string", "string", "a string" );
_array = parser.getArray("-a", "--array", "a b", "an array enclosed within < >" );
_boolean = parser.getBool("-b","--bool", "a bool value" );
}
catch (UException & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
catch (exception & e)
{
cout << e.what() << endl;
exit(1);
}
}
/// Uses the parser and returns param values
void InitRandom( Parser & parser) {
unsigned long _seed;
try {
_seed = parser.getUnsignedLong("-S", "--seed", "0", "Seed for Random number generator" );
}
catch (UException & e)
{
cout << e.what() << endl;
parser.printHelp();
exit(1);
}
catch (exception & e)
{
cout << e.what() << endl;
exit(1);
}
if (_seed == 0) { // use clock to get a "random" seed
struct timeval tval;
struct timezone tzp;
gettimeofday (&tval, &tzp); // time since midnight January 1, 1970.
_seed = tval.tv_usec ; // micro seconds
char s[32];
sprintf(s,"%ld", _seed);
parser.setParamValue("--seed", s); // so it will be printed out in the status file, and canbe later re-used to re-run EXACTLY the same run
}
rng.reseed(_seed);
return;
}
int main( int argc, char* argv[]) {
unsigned in;
float f;
string s;
vector<string> a;
bool b;
// Create the command-line parser
Parser parser( argc, argv, "Parser example");
InitRandom(parser);
parser.AddTitle("General parameters");
getParams(parser, in, f, s, a, b);
cout << "\n integer: " << in << endl
<< " float: "<< f << endl
<< " string: /"<< s << "/" << endl
<< " boolean: "<< b << endl
<< " array: < ";
vector<string>::const_iterator i;
for (i=a.begin() ; i<a.end() ; i++) {
cout << *i << " ";
}
cout << ">" << endl << endl ;
// call to the subroutine that also needs some parameters
sub(parser);
// writing all parameters
//
// if programmer wishes, the name of the output file can be set as a parameter itself
// otherwise it will be argv[0].status
string OutputFileName;
GetOutputParam(parser, OutputFileName);
parser.outputParam(OutputFileName);
if( parser.getBool("-h" , "--help" , "Shows this help")) {
parser.printHelp();
exit(1);
}
// but progrmamer should be careful to write the parser parameters
// after the last bit that uses it has finished
// Now the main body of the program
for (int i=0; i<20; i++) {
cout << rng.normal() << endl;
}
cout << "C'est fini" << endl;
return 0;
}