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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 #ifndef ARGS_h
00084 #define ARGS_h
00085
00086
00087
00088
00089 #include <iostream>
00090 using namespace std;
00091
00092
00093
00094
00095
00096
00097 class ARGS;
00098 class argsAction;
00099 extern ostream& operator << (ostream& os, ARGS*);
00100 extern ostream& operator << (ostream& os, ARGS&);
00101
00102
00103 class unionClass
00104 {
00105 friend class ARGS;
00106 friend class argsAction;
00107 friend ostream& operator << (ostream&, ARGS*);
00108 friend ostream& operator << (ostream&, ARGS&);
00109
00110 public:
00111 enum argType
00112 { bool_t, char_t, str_t, int_t, uint_t, float_t, double_t, action_t };
00113
00114 private:
00115 union argU
00116 {
00117 bool boolean;
00118 char ch;
00119 char* string;
00120 int integer;
00121 unsigned int uinteger;
00122 float single_prec;
00123 double double_prec;
00124 argsAction* action;
00125 };
00126
00127 argU* ptr;
00128 argType type;
00129 static char* typeStr(unionClass::argType);
00130 friend ostream& operator << (ostream&, unionClass::argType&);
00131
00132 public:
00133 unionClass(bool& b) { ptr = (argU*)&b; type = bool_t; }
00134 unionClass(char& c) { ptr = (argU*)&c; type = char_t; }
00135 unionClass(char*& s) { ptr = (argU*)&s; type = str_t; }
00136 unionClass(int& i) { ptr = (argU*)&i; type = int_t; }
00137 unionClass(unsigned int& i) { ptr = (argU*)&i; type = uint_t; }
00138 unionClass(float& f) { ptr = (argU*)&f; type = float_t; }
00139 unionClass(double& d) { ptr = (argU*)&d; type = double_t; }
00140 unionClass(argsAction& a) { ptr = (argU*)&a; type = action_t; }
00141 };
00142
00143
00144 extern ostream& operator << (ostream&, unionClass::argType&);
00145
00146 class argsAction
00147 {
00148 enum ActionRetCode { OK, ERR };
00149 public:
00150 virtual ~argsAction() {}
00151 private:
00152 ActionRetCode setArg(char*, unionClass::argType, unionClass::argU);
00153
00154 protected:
00155 ActionRetCode setArg(char*, bool);
00156 ActionRetCode setArg(char*, char);
00157 ActionRetCode setArg(char*, char*);
00158 ActionRetCode setArg(char*, int);
00159 ActionRetCode setArg(char*, float);
00160 ActionRetCode setArg(char*, double);
00161 ActionRetCode satisfy(char*);
00162
00163 public:
00164 virtual void act() {}
00165 virtual void act(bool& b) { b = b;}
00166 virtual void act(char& c) { c = c; }
00167 virtual void act(char*& str) { str = str; }
00168 virtual void act(int& i) { i = i; }
00169 virtual void act(unsigned int& ui) { ui = ui; }
00170 virtual void act(float& f) { f = f; }
00171 virtual void act(double& d) { d = d; }
00172 };
00173
00174
00175
00176 class ARGS
00177 {
00178 friend class unionClass;
00179 friend class argsAction;
00180
00181 public:
00182
00183 enum argKind { Opt, Req, Tog };
00184 enum ArgsRetCode { ARG_MISSING, ARG_OK, ARG_ERR };
00185 static ARGS Args[];
00186 static ARGS END;
00187
00188 private:
00189
00190 static char* const NOFLAG;
00191 static char* const NOFL_FOUND;
00192 static const char CommentChar;
00193 static bool usage_called;
00194 static int numArgs;
00195 static bool* found;
00196 static char* progName;
00197 static bool ignoreUnknownSwitch;
00198
00199
00200 char* flag;
00201 argKind arg_kind;
00202 unionClass uc;
00203 char* description;
00204 argsAction* action;
00205
00206
00207 static argsAction no_action;
00208 static bool __args__dummy__;
00209
00210 static ostream* pout;
00211
00212 public:
00213 ARGS(char*,argKind, unionClass, char*d=NULL, argsAction&a=no_action);
00214 ARGS(argKind, unionClass, char*d=NULL, argsAction&a=no_action);
00215 ARGS(unionClass, char*d=NULL, argsAction&a=no_action);
00216 ARGS(const ARGS&);
00217 ARGS();
00218
00219 static void cleanUp();
00220 static ArgsRetCode parseFromCommandLine(int,char**);
00221 static ArgsRetCode parseFromFile(char*f="argsFile");
00222 static void parse(int, char**, char*&, ostream* prout=NULL);
00223 static void parse(int i, char**c, ostream* prout=NULL)
00224 { char*d=NULL; parse(i,c,d, prout); }
00225 static void printMissing() { checkMissing(true); }
00226 static void usage(bool force=false);
00227 static void ignoreUnknownFlag() { ignoreUnknownSwitch = true; }
00228 static void regardUnknownFlag() { ignoreUnknownSwitch = false; }
00229
00230 private:
00231 void init(char*, argKind, char*, argsAction&);
00232 static void Error(const char*, const char*s2=NULL, const char*s3=NULL,
00233 const char*s4=NULL, const char*s5=NULL);
00234
00235 static bool noFlagP(char *);
00236 static ArgsRetCode argsSwitch(ARGS*, char*, int&, bool&, char*);
00237 static ARGS* findMatch(ARGS*, char*);
00238 static void makeFound();
00239 static bool checkMissing(bool printMessage=false);
00240 static bool boolable(char* string, bool&value);
00241 friend ostream& operator << (ostream&,ARGS*);
00242 friend ostream& operator << (ostream&,ARGS&);
00243
00244 };
00245
00246 #endif