Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

cmdline_parser.hh

Go to the documentation of this file.
00001 /*
00002  *  File:       cucu.c
00003  *              $Id: cmdline_parser.hh,v 1.3 2002/06/26 20:46:03 alec Exp $
00004  *
00005  *  Author:     Alec Panoviciu (alecu@email.com)
00006  *
00007  *  Comments:
00008  *
00009  *  Revision history:
00010  *
00011  *  $Log: cmdline_parser.hh,v $
00012  *  Revision 1.3  2002/06/26 20:46:03  alec
00013  *  g++ 3.x happy
00014  *
00015  *  Revision 1.2  2002/04/29 09:34:10  alec
00016  *  scanner ptree building compiles
00017  *
00018  */
00019 
00020 /* 
00021   Copyright (C) 2002 Alexandru Panoviciu (alecu@email.com)
00022 
00023   This program is free software; you can redistribute it and/or modify
00024   it under the terms of the GNU General Public License as published by
00025   the Free Software Foundation; either version 2 of the License, or
00026   (at your option) any later version.
00027 
00028   This program is distributed in the hope that it will be useful,
00029   but WITHOUT ANY WARRANTY; without even the implied warranty of
00030   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00031   GNU General Public License for more details.
00032 
00033   You should have received a copy of the GNU General Public License
00034   along with this program; if not, write to the Free Software
00035   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00036 
00037  */
00038 
00039 
00040 #ifndef __CMDLINE_PARSER_HH__
00041 #define __CMDLINE_PARSER_HH__
00042 
00043 #include <iostream>
00044 #include <string>
00045 #include <vector>
00046 using namespace std;
00047 
00048 #include "prop_registry.hh"
00049 #include "debug.h"
00050 
00051 
00055 class CmdLineParseException : public exception
00056 {
00057 public:
00058   CmdLineParseException (const string &message_) :
00059     message(message_)
00060   {}
00061   
00062   CmdLineParseException () :
00063     message("Command Line Parse Exception")
00064   {}
00065   
00066   virtual const char* what() const throw()
00067   {
00068     return message.c_str();
00069   }
00070 
00071   virtual operator string() const
00072   {
00073     return message;
00074   }
00075 
00076   ~CmdLineParseException () throw ()
00077   {}
00078   
00079 protected:
00080   
00081   string message;
00082   
00083 };
00084 
00085 inline ostream& operator << (ostream& os, CmdLineParseException &ex)
00086 {
00087   return os << ex.what();
00088 }
00089 
00090 
00098 class CmdLineParser
00099 {
00100 
00141   typedef struct t_CmdLineAtom
00142   {
00143     enum {k_string, k_bool, k_int, k_float} tag;
00144     bool multi;
00145     bool optional;
00146     string sOpt;
00147     string lOpt;
00148     string prop;
00149     string description;
00150 
00151     t_CmdLineAtom (const string &tag_, bool multi_, bool optional,
00152                    const string &sOpt_, const string &lOpt_,
00153                    const string &prop_, const string &description_) :
00154       multi(multi_), sOpt(sOpt_), lOpt(lOpt_),
00155       prop(prop_), description(description_)
00156     {
00157       if (tag_ == "s") tag = k_string;
00158       else if (tag_ == "b") tag = k_bool;
00159       else if (tag_ == "i") tag = k_int;
00160       else if (tag_ == "f") tag = k_float;
00161       else ASSERT(0, "bad tag string, should be \"s\"/\"b\"/\"i\"/\"f\"");
00162       ASSERT((sOpt != "") || (lOpt != ""),
00163              "both long and short option strings are empty");
00164     }
00165 
00169     operator string () const;
00170 
00175     ostream& dumpDescription (ostream &os) const;
00176     
00177   } CmdLineAtom;
00178   
00179 public:
00180 
00186   CmdLineParser (const string &progName_, PropRegistry &pr_) :
00187     progName(progName_), pr(pr_)
00188   {}
00189 
00201   int parse (int argc, char *argv[]) throw (CmdLineParseException);
00202 
00203 
00212   CmdLineParser& add (const CmdLineAtom &atom)
00213   {
00214     desc.push_back(atom);
00215     return *this;
00216   }
00217 
00218   CmdLineParser& add (const string &tag, bool multi, bool optional,
00219                       const string &sOpt, const string &lOpt,
00220                       const string &prop, const string &description)
00221   {
00222     return add(CmdLineAtom(tag, multi, optional, sOpt, lOpt,
00223                            prop, description));
00224   }
00226 
00227   void printUsage (ostream &os);
00228   
00229 private:
00230 
00234   PropRegistry &pr;
00235 
00239   vector<CmdLineAtom> desc;
00240 
00244   string progName;
00245 };
00246 
00247 #endif /* #ifndef __CMDLINE_PARSER_HH__ */

Generated at Tue Jul 9 21:05:43 2002 for CppCC by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001