#define COMMANDLINETEST_VERSION \
"COMMANDLINETEST V1.0 test commandline class"
#include <iostream>
#include <tfxx/commandline.h>
#include <tfxx/xcmdline.h>
using std::cout;
using std::cerr;
using std::endl;
int main(
int iargc,
char* argv[])
{
char usage_text[]=
{
"usage: commandlinetest argument [-v] [-o n] [-f name] [-q [r]]" "\n"
" [-longopt f] [-F] [value [value ...]]" "\n"
" or: commandlinetest --help|-h" "\n"
};
char help_text[]=
{
"argument one argument is mandatory" "\n"
"value additional arguments (optional)" "\n"
" " "\n"
"options:" "\n"
"-v set a flag (no argument is allowed)" "\n"
"-o n set an integer value (argument mandatory)" "\n"
"-f name set a string value (argument mandatory)" "\n"
"-q r set an integer value (argument optional)" "\n"
"-longopt f set the value for a long option name" "\n"
"-F evaluate file name options" "\n"
" availabe keys are: \"f\", \"dl\", \"k\"" "\n"
" " "\n"
"Long options may be abbreviated - test it!" "\n"
" " "\n"
"Notice that you have to use the syntax" "\n"
" " "\n"
" -q=argument" "\n"
" " "\n"
"with the optional argument for option -q. This syntax" "\n"
"is mandatory with optional arguments, but may also be used" "\n"
"with mandatory arguments." "\n"
" " "\n"
COMMANDLINETEST_CVSID
};
{
{NULL}
};
static const char* keys[]={"f", "dl", "k", 0};
if (iargc<2)
{
cerr << usage_text << endl;
exit(0);
}
{
cerr << usage_text << endl;
cerr << help_text << endl;
exit(0);
}
cout << "Here I tell you about the options you supplied:" << endl;
for (int iopt=0; iopt<6; iopt++)
{
cout << endl;
cout <<
"option: '" << options[iopt].
opt_string <<
"'" << endl;
if (cmdline.
optset(iopt)) { cout <<
" option was set"; }
else { cout << " option was not set"; }
cout << endl;
cout << " argument (string): '"
cout << " argument (int): '"
<< cmdline.
int_arg(iopt) <<
"'" << endl;
cout << " argument (long): '"
<< cmdline.
long_arg(iopt) <<
"'" << endl;
cout << " argument (float): '"
cout << " argument (double): '"
cout << " argument (bool): '";
{ cout << "true"; } else { cout << "false"; }
cout << "'" << endl;
}
cout << endl;
cout << "evaluating your choice:" << endl;
cout << " verbose mode switched ";
if (cmdline.
optset(1)) { cout <<
"ON"; }
else { cout <<
"OFF"; }
cout << endl;
if (cmdline.
optset(2)) { cout <<
" set:"; }
else { cout << " default:"; }
cout <<
" -o: '" << cmdline.
int_arg(2) <<
"'" << endl;
if (cmdline.
optset(3)) { cout <<
" set:"; }
else { cout << " default:"; }
cout <<
" -f: '" << cmdline.
string_arg(3) <<
"'" << endl;
if (cmdline.
optset(4)) { cout <<
" set:"; }
else { cout << " default:"; }
cout <<
" -q: '" << cmdline.
float_arg(4) <<
"'" << endl;
if (cmdline.
optset(5)) { cout <<
" set:"; }
else { cout << " default:"; }
cout <<
" -longopt: '" << cmdline.
string_arg(5) <<
"'" << endl;
{
cout << endl;
cout << "And here is the rest of your command line arguments:" << endl;
while (cmdline.
extra()) { cout << cmdline.
next() << endl; }
}
else
{
cout << endl;
cout << "Parse file names and options" << endl;
cout << "Supported options keys are:" << endl;
const char** thekeys=keys;
const char* key=*thekeys;
while (key != NULL)
{
std::cout << " next key is: \"" << key << "\"" << std::endl;
++thekeys;
key=*thekeys;
}
Tparsed::const_iterator i=result.begin();
while(i!=result.end())
{
std::cout << "filename: \"" << i->name << "\"" << std::endl;
Toptionmap::const_iterator j=i->options.begin();
while(j!=i->options.end())
{
std::cout << " " << j->first
<< " \"" << j->second << "\"" << std::endl;
++j;
}
const char ** k=keys;
while (*k != NULL)
{
if (i->haskey(*k))
{
std::cout << " key " << *k << ": found " << i->count(*k)
<< " entries" << std::endl;
std::cout << " first entry is \"" << i->value(*k) <<
"\"" << std::endl;
std::cout << " all entries are:" << std::endl;
Toptionmap::const_iterator p=e.begin();
while(p != e.end())
{
std::cout << " \"" << p->second << "\"" << std::endl;
++p;
}
}
++k;
key=*thekeys;
}
++i;
}
}
}