00001 #ifndef CSU_COMMAND_LINE_INCLUDED 00002 #define CSU_COMMAND_LINE_INCLUDED 00003 00004 namespace csu{ 00005 #define CL_YES_STRING "YES" 00006 #define CL_NO_STRING "NO" 00007 00008 #define CL_YES 1 00009 #define CL_NO 0 00010 00011 /* 00012 * These functions are used to parse basic command line options 00013 * argc and argv are basic command line parameters 00014 * i is the current scanning location in the command line 00015 * opt is a string giving the option to look for in the command line 00016 * additional parameters are option parameters that follow the 00017 * option string. 00018 * 00019 * Optional arguments should always be parsed before required arguments. 00020 * 00021 * The functions are maid to be called within a four loop that inc i. 00022 * If more that one argument is parsed i is updated to point at the last 00023 * argument read to allow exicution to proceed normally on next loop. 00024 * 00025 * Here is an example code illistrating how to parse command lines 00026 * with this api: 00027 * 00028 * int i; int num = 0; 00029 * 00030 * Set up default values here. 00031 * 00032 * for(i = 0; i < argc; i++){ 00033 * if (readOption(argc, argv, &i, "-verbose")) { verbose = 1; } 00034 * else if (readOptionInt(argc, argv, &i, "-debuglevel",&debuglevel)) {} 00035 * else if (checkBadOption(argc, argv, &i)) {} 00036 * else if (readReqiredString(argc, argv, &i, &num, 0, filename)) {} 00037 * } 00038 * 00039 * then check to see if the correct number of required arguments were parsed 00040 * if(num != 1){ clParseError(argc, argv, i, "Wrong number of command line arguments."); } 00041 * 00042 * This code also expects that you have a useage function define that takes argv[0] 00043 * as its only arguments and exits with value 1. 00044 */ 00045 00046 /* prototype to use for an external usage statement */ 00047 void usage(const char* ); 00048 00049 /* returns true if opt is parsed and i+=0 */ 00050 int readOption(int argc, char** argv, int *i, const char* opt); 00051 00052 /* returns true if opt is parsed and sets arg to point to location in argv that is after flag (the next string) i+=1 */ 00053 int readOptionString(int argc, char** argv, int *i, const char* opt, char** arg); 00054 00055 /* returns true if opt is parsed and sets arg to point to location in argv that is after flag (the next string) i+=1 */ 00056 int readOptionYesNo(int argc, char** argv, int *i, const char* opt, int* flag); 00057 00058 /* returns true only if opt is parsed and following argument string matches arg 00059 * This is used to test for options like -hist PRE -hist POST -hist NONE i+=1*/ 00060 int readOptionMatch(int argc, char** argv, int *i, const char* opt, const char* arg); 00061 00062 /* this reads in an option and an int. i+=1 */ 00063 int readOptionInt(int argc, char** argv, int *i, const char* opt, int *arg); 00064 00065 /* this reads in an option and two ints. i+=2 */ 00066 int readOptionInt2(int argc, char** argv, int *i, const char* opt, int *arg1, int *arg2); 00067 00068 /* this reads in an option and a double. i+=1 */ 00069 int readOptionDouble(int argc, char** argv, int *i, const char* opt, double *arg); 00070 00071 /* this reads in an option and two doubles. i+=2 */ 00072 int readOptionDouble2(int argc, char** argv, int *i, const char* opt, double *arg1, double *arg2); 00073 00074 /* this reads in an option and four doubles. i+=4 */ 00075 int readOptionDouble4(int argc, char** argv, int *i, const char* opt, double *arg1, double *arg2, double *arg3, double *arg4); 00076 00077 /* Reads in a required argument, only if num == arg_num i += 0*/ 00078 int readRequiredString(int argc, char** argv, int *i, int *num, int arg_num, char** arg); 00079 00080 /* Reads in a required argument, only if num == arg_num i += 0*/ 00081 int readRequiredInt(int argc, char** argv, int *i, int *num, int arg_num, int* arg); 00082 00083 /* check to see if current argument starts with a dash and if it does output an error and exit. otherwize return 0 */ 00084 int checkBadOption(int argc, char** argv, int *i); 00085 00086 /* Output a command line parsing error */ 00087 void clParseError(int argc, char **argv, int i, char* message); 00088 } 00089 #endif 00090 00091 00092 00093
1.2.14 written by Dimitri van Heesch,
© 1997-2002