00001 /* Copyright (C) Kwee Heong Tan 2002 - 2003 00002 Permission is granted to use this code without restriction as 00003 long as this copyright notice appears in all source files. 00004 */ 00005 // $Id: CmdLine.h,v 1.14 2002/11/28 18:21:11 khtan Exp khtan $ 00006 #if defined(WIN32) && !defined( __COMO__) 00007 #pragma warning(disable:4786) // only need this here, included elsewhere 00008 #endif 00009 #include <string> 00010 #include <map> 00011 #include <vector> 00012 #ifdef STL96 00013 #include <iostream.h> 00014 #define STD 00015 #else 00016 #include <iostream> 00017 #define STD std:: 00018 #endif 00019 00020 class CmdLine{ 00021 /* USAGE DOCUMENTATION 00022 General 00023 -------- 00024 The CmdLine class is used to specify the options a program or class wishes 00025 to handle, and then process the {argc,argv} arguments of a standard main 00026 call. 00027 00028 Parameter types 00029 --------------- 00030 There are 2 types of parameter switches - standalone and parameterized. 00031 A standalone switch takes no values, eg -h or -help 00032 A parameterized switch takes a value, eg -s J:in 00033 00034 Since the switch is just a string value, you are not restricted to the <dash><letter> 00035 syntax, eg -h. For example, --help or ==help or **help are all acceptable. 00036 00037 Setup 00038 ----- 00039 The user decides which parameter types and string values to represent them. 00040 The member functions addStandaloneSwitch() and addParameterSwitch() are used 00041 to set them. For a parameterized switch, a string initialValue can be specified 00042 as a default. 00043 00044 The member function process(argc,argv) is used to go over the argv array 00045 to check whether the switches are set, and in the case of parameterized 00046 switches, to take in the parameter value. 00047 00048 Query 00049 ------ 00050 In use, the member function switchValue() returns the values "set" or "unset" 00051 for standalone switches, and the parameter value or its initial value, 00052 for parameterized switches. 00053 00054 The NumberOfRemainingParameters is used to detect any parameters in argv 00055 that were not processed. This allows CmdLine to ignore switches it is not 00056 interested in and still allows the user to process them later, via the 00057 RemainingParamter(int index). 00058 00059 Additional utilities 00060 --------------------- 00061 printSwitches and dump are self explanatory. 00062 00063 */ 00064 friend STD ostream& operator<<(STD ostream &s,const CmdLine& c); 00065 public : 00066 CmdLine(); 00067 void addParameterSwitch(const STD string flag,const STD string initialValue,const STD string help); 00068 void addStandaloneSwitch(const STD string flag,const STD string help); 00069 const STD string& switchValue(const STD string flag); 00070 const STD string& switchHelp(const STD string flag); 00071 void process(int argc,char** argv); 00072 int NumberOfRemainingParameters(); 00073 const char* RemainingParameter(int index); 00074 void printSwitches(STD ostream& s=STD cout); 00075 void dump(STD ostream& s=STD cout) const; 00076 void dump(STD ostream& s=STD cout); 00077 private: 00078 typedef STD map< STD string, STD string, STD less<STD string> > mapType; 00079 mapType m; 00080 mapType h; 00081 STD vector< char* > l; 00082 bool debug; 00083 };