proj home

Files   Classes   Functions   Hierarchy  

commandline Class Reference

Process the command line arguments to assign values to variables. More...

#include <commandline.h>

Collaboration diagram for commandline:

List of all members.

Public Member Functions

 commandline (int argc, char **argv)
 Supply the command line arguments to the constructor.
 commandline (istream &is)
 Construct the command line from a input stream.
 commandline (stringc &arg)
 Read the stream for command line arguments.
void read (stringc &arg)
 Read the string as a command line.
void read (istream &is)
void readfile (stringc var)
 var=filename , reading the file in.
template<typename T >
boolc mapvar (T &var, stringc &key)
 Read a string into a variable.
template<typename T >
boolc mapvar (T &var, stringc &key, T const &init)
 Read a variable, if it does not exist then write init to var.
template<typename T >
boolc mapcallback (T &x, void(T::*p)(), stringc &token)
 If the token is present in the command line then the member function is called.
boolc hastoken (stringc &token) const
 Has the input included the token?
ostreamenablehelp (ostream &os=cout) const
 Place after mapvar calls.

Public Attributes

vector< string > args
 Sequential list of arguments.
multiset< string > tokens
 Input strings having no = sign are defined as tokens.
vector< string > bindvar
 List of attempted binds.


Detailed Description

Process the command line arguments to assign values to variables.

Acknowledgement: Thanks to Nigel Stewart for ideas.

Example
int main(int argc, char** argv)
{
  commandline cmd(argc,argv);
  
  uint numPoints(10);
  cmd.mapvar(numPoints,"numPoints");
  ...

At the command line calling the program to pass the
  variable/s in.
$./main numPoints=50

Definition at line 38 of file commandline.h.


Constructor & Destructor Documentation

commandline::commandline ( int  argc,
char **  argv 
) [explicit]

Supply the command line arguments to the constructor.

Definition at line 63 of file commandline.cpp.

References args, splitstring(), and tokens.

00064 { 
00065   for (uint i=1; i<(uint)argc; ++i)
00066   {
00067     string s1, s2;
00068     if (splitstring(s1,s2,string(argv[i]),'='))
00069     {
00070       mp[s1] = s2;
00071 //cout << "*** s2=*" << s2 << "*" << endl;
00072 //cout << "*** mp[s1]=*" << mp[s1] << "*" << endl;
00073     }
00074     else
00075       tokens.insert(argv[i]);
00076     args.push_back(argv[i]);
00077   }
00078 }

commandline::commandline ( istream &  is  )  [explicit]

Construct the command line from a input stream.

Definition at line 199 of file commandline.cpp.

References read().

00200 {
00201   read(is);
00202 }

commandline::commandline ( stringc arg  )  [explicit]

Read the stream for command line arguments.

Definition at line 194 of file commandline.cpp.

References read().

00195 {
00196   read(arg);
00197 }


Member Function Documentation

ostream & commandline::enablehelp ( ostream os = cout  )  const

Place after mapvar calls.

-h prints the mapvar strings.

Definition at line 204 of file commandline.cpp.

References bindvar, and tokens.

Referenced by integration< D, F >::reset(), rsastate::rsastate(), histogramtest::test01(), commandlinetest::test02(), and commandlinetest::test05().

00205 {
00206   stringc hlp("-h");
00207 
00208   boolc hflag = (tokens.find(hlp)!=tokens.end());
00209   if (hflag)
00210   {
00211     for (uint i=0; i<bindvar.size(); ++i)
00212       os << bindvar[i] << " ";
00213     os << endl;
00214   }
00215 
00216   return os;
00217 }

boolc commandline::hastoken ( stringc token  )  const [inline]

Has the input included the token?

Definition at line 95 of file commandline.h.

References tokens.

Referenced by mkerrorscompile::eval(), mkerrorscompile::eval2(), main(), mkerrorstest::test02(), commandlinetest::test03(), mkerrorstest::test03(), and quickhull2Dtest::test05().

00096     { return (tokens.find(token)!=tokens.end()); }

template<typename T >
boolc commandline::mapcallback ( T x,
void(T::*)()  p,
stringc token 
) [inline]

If the token is present in the command line then the member function is called.

Definition at line 239 of file commandline.h.

Referenced by commandlinetest::test03().

00244 {
00245   bool found = (tokens.find(token)!=tokens.end());
00246   if (found)
00247     (x.*p)();
00248 
00249   return found;
00250 }

template<typename T >
boolc commandline::mapvar ( T var,
stringc key,
T const &  init 
) [inline]

Read a variable, if it does not exist then write init to var.

Definition at line 222 of file commandline.h.

References map_var< T >::foundGet().

00227 {
00228   bindvar.push_back(key);
00229   map_var<T&> t(var,mp,key); 
00230   boolc res = t.foundGet();
00231   if (res==false)
00232     var = init;
00233 
00234   return res;
00235 }

template<typename T >
boolc commandline::mapvar ( T var,
stringc key 
) [inline]

Read a string into a variable.

Definition at line 213 of file commandline.h.

References bindvar, and map_var< T >::foundGet().

Referenced by graphmisc::colornormalize(), createmakefile< CFC >::createmakefile(), explorelinetest01(), explorelinetest02(), explorelinetest03(), explorepdtest01(), explorepdtest02(), vispointxml::handlecommand(), visconvex::handlecommand(), html::insert(), camera::lookat(), main(), maze001::maze001(), maze003::maze003(), maze004::maze004(), maze005::maze005(), minimizecomparetest01(), minimizecomparetest02(), cubegui::prog01(), readfile(), integration< D, F >::reset(), rsastate::rsastate(), simplexD1tessapp01::simplexD1tessapp01(), systemcallseval::systemcallseval(), windowscaleD2test::test002(), spiralindextest::test01(), quickhull3Dtest::test01(), triangletest::test01(), tetrahedrontest::test01(), desystest::test01(), commandlinetest::test01(), planetest::test01(), mathlibtest::test01(), histogramtest::test01(), cireporttest::test01(), planeinttest::test01(), diskinttest::test01(), circleD2test::test01(), boxOBBhalfspaceD2test::test01(), triangles3Tdisplaytest::test01(), delaunaysimpleD2test::test01(), pathlinesegtest::test01(), test01(), vrmltest::test02(), helixtestscope::helixtest::test02(), spiralindextest::test02(), simplexD2tessapp01::test02(), rsatest::test02(), quickhull2Dtest::test02(), triangletest::test02(), tetrahedrontest::test02(), commandlinetest::test02(), mathlibtest::test02(), diskinttest::test02(), triangles3Tdisplaytest::test02(), delaunaysimpleD2test::test02(), test02(), helixtestscope::helixtest::test03(), spiralindextest::test03(), triangletest::test03(), minexpdimtest::test03(), desystest::test03(), planetest::test03(), halfspaceD3test::test03(), diskinttest::test03(), triangles3Tdisplaytest::test03(), pathlinesegtest::test03(), test03(), spiralindextest::test04(), quickhull2Dtest::test04(), minexpdimtest::test04(), desystest::test04(), commandlinetest::test04(), mathlibtest::test04(), makestatetest::test04(), triangles3Tdisplaytest::test04(), test04(), quickhull2Dtest::test05(), minexpdimtest::test05(), desystest::test05(), commandlinetest::test05(), makestatetest::test05(), triangles3Tdisplaytest::test05(), treeindexedD2test::test05(), partitionstest::test06(), quickhull2Dtest::test06(), mathlibtest::test06(), makestatetest::test06(), mathlibtest::test07(), test08(), test09(), test11(), treeindexedD2test::unittest01(), pathlinesegtest::unittest01(), boxOBBhalfspaceD2test::unittest02(), and writearcgeometry().

00214 {
00215   bindvar.push_back(key);
00216   map_var<T&> t(var,mp,key); 
00217   return t.foundGet();
00218 }

void commandline::read ( istream &  is  ) 

Definition at line 109 of file commandline.cpp.

References args, splitstring(), and tokens.

00110 {
00111   string s;
00112   args.clear();
00113   vector<string>& vi(args);
00114 
00115   if (is.eof()==false)
00116     is >> s;
00117 
00118   for (; is.eof()==false; )
00119   {
00120     vi.push_back(s);
00121     s.clear();
00122     is >> s;
00123   }
00124   if (s.size()!=0)
00125     vi.push_back(s);
00126   
00127 //  for (uint i=0; i<vi.size(); ++i)
00128 //  {
00129 //cout << "*vi[" << i << "]=" <<  vi[i] << "*" << endl;
00130 //  }
00131 
00132 //Converting v to v2
00133 //*vi[0]=s1=abc*
00134 //*vi[1]=points="1*
00135 //*vi[2]=19*
00136 //*vi[3]=32*
00137 //*vi[4]=4"*
00138 //*vi[5]=N=350*
00139 //*v2[0]=s1=abc*
00140 //*v2[1]=points=1 19 32 4*
00141 //*v2[2]=N=350*
00142 
00143   if (vi.size()==0)
00144     return;
00145 
00146   vector<string> v2;
00147   bool quote=false;
00148   
00149   string::size_type k;
00150   for (uint i=0; i<vi.size(); ++i)
00151   { 
00152     string current(vi[i]);
00153     k = current.find('"');
00154  
00155     // Looking for closing quote
00156     if (quote)
00157     {
00158       if(k!=string::npos)    
00159       {
00160         current.erase(k,1);
00161         quote=false;
00162       }      
00163       v2[v2.size()-1] += (string(" ")+current);
00164     }
00165     else
00166     {
00167       if (k!=string::npos)
00168       {
00169         current.erase(k,1);
00170         quote=true;
00171       } 
00172       v2.push_back(current);
00173     }  
00174   }
00175 
00176 //  for (uint i=0; i<v2.size(); ++i)
00177 //  {
00178 //cout << "*v2[" << i << "]=" <<  v2[i] << "*" << endl;
00179 //  }
00180 
00181 
00182   // Now do parsing
00183   for (uint i=0; i<v2.size(); ++i)
00184   {
00185     string s1, s2;
00186     if (splitstring(s1,s2,v2[i],'='))
00187       mp[s1] = s2;
00188     else
00189       tokens.insert(v2[i]);
00190   }
00191 }

void commandline::read ( stringc arg  ) 

Read the string as a command line.

Separate parsing rules to actual command line which pre-parses text.

Definition at line 80 of file commandline.cpp.

Referenced by commandline(), and readfile().

00081 {
00082   stringstream ss(arg);
00083   read(ss);
00084 
00085 //cout << SHOW(arg) << endl;
00086 //  assert(false);
00087 }

void commandline::readfile ( stringc  var  ) 

var=filename , reading the file in.

Use: load command line arguments from an argument pointing to a file at the command line!

Definition at line 48 of file commandline.cpp.

References mapvar(), and read().

Referenced by rsastate::rsastate(), and commandlinetest::test02().

00049 {
00050   if (var.empty())
00051     return;
00052 
00053   string file;
00054   mapvar(file,var.c_str());
00055   if (!file.empty())
00056   {
00057     ifstream f1(file.c_str());
00058     read(f1);
00059   }
00060 }


Member Data Documentation

vector<string> commandline::args

Sequential list of arguments.

Definition at line 48 of file commandline.h.

Referenced by commandline(), read(), and mkerrorstest::test02().

vector<string> commandline::bindvar

List of attempted binds.

Definition at line 99 of file commandline.h.

Referenced by enablehelp(), and mapvar().

multiset<string> commandline::tokens

Input strings having no = sign are defined as tokens.

Definition at line 93 of file commandline.h.

Referenced by commandline(), enablehelp(), hastoken(), and read().


The documentation for this class was generated from the following files:

Generated on Fri Mar 4 00:49:52 2011 for Chelton Evans Source by  doxygen 1.5.8