00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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 ≺
00235
00239 vector<CmdLineAtom> desc;
00240
00244 string progName;
00245 };
00246
00247 #endif