|
OpenADFortTk (basic)
|
00001 #include <cstdlib> 00002 00003 #include "Args.h" 00004 00005 //*************************** Forward Declarations ************************** 00006 00007 using std::cerr; 00008 using std::endl; 00009 using std::string; 00010 00011 00012 //*************************************************************************** 00013 00014 static const char* version_info = "version .289"; 00015 00016 static const char* usage_summary = 00017 "[mode] [options] <whirl-file>\n"; 00018 00019 static const char* usage_details = 00020 "Given a WHIRL file and a mode, do something.\n" 00021 "\n" 00022 "Modes:\n" 00023 " --ir run a test routine on IR\n" 00024 " --oa-ujnum test OA UJ numbering\n" 00025 " --whirl2f test whirl2f\n" 00026 "\n" 00027 "Options:\n" 00028 " -d, --dump dump the WHIRL IR\n" 00029 " -V, --version print version information\n" 00030 " -h, --help print this help\n" 00031 " --debug [lvl] debug mode at level `lvl'\n"; 00032 00033 00034 #define CLP CmdLineParser 00035 00036 CmdLineParser::OptArgDesc Args::optArgs[] = { 00037 // Modes 00038 { 0 , "ir", CLP::ARG_NONE, CLP::DUPOPT_ERR, NULL }, 00039 { 0 , "oa-ujnum", CLP::ARG_NONE, CLP::DUPOPT_ERR, NULL }, 00040 { 0 , "whirl2f", CLP::ARG_NONE, CLP::DUPOPT_ERR, NULL }, 00041 00042 // Options 00043 { 'd', "dump", CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL }, 00044 { 'V', "version", CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL }, 00045 { 'h', "help", CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL }, 00046 { 0 , "debug", CLP::ARG_OPT, CLP::DUPOPT_CLOB, NULL }, 00047 CmdLineParser::OptArgDesc_NULL 00048 }; 00049 00050 #undef CLP 00051 00052 //*************************************************************************** 00053 // Args 00054 //*************************************************************************** 00055 00056 Args::Args() 00057 { 00058 Ctor(); 00059 } 00060 00061 Args::Args(int argc, const char* const argv[]) 00062 { 00063 Ctor(); 00064 Parse(argc, argv); 00065 } 00066 00067 void 00068 Args::Ctor() 00069 { 00070 debug = 0; // default: 0 (off) 00071 } 00072 00073 Args::~Args() 00074 { 00075 } 00076 00077 00078 void 00079 Args::PrintVersion(std::ostream& os) const 00080 { 00081 os << GetCmd() << ": " << version_info << endl; 00082 } 00083 00084 00085 void 00086 Args::PrintUsage(std::ostream& os) const 00087 { 00088 os << "Usage: " << GetCmd() << " " << usage_summary << endl 00089 << usage_details << endl; 00090 } 00091 00092 00093 void 00094 Args::PrintError(std::ostream& os, const char* msg) const 00095 { 00096 os << GetCmd() << ": " << msg << endl 00097 << "Try `" << GetCmd() << " --help' for more information." << endl; 00098 } 00099 00100 void 00101 Args::PrintError(std::ostream& os, const std::string& msg) const 00102 { 00103 PrintError(os, msg.c_str()); 00104 } 00105 00106 00107 void 00108 Args::Parse(int argc, const char* const argv[]) 00109 { 00110 try { 00111 // ------------------------------------------------------- 00112 // Parse the command line 00113 // ------------------------------------------------------- 00114 parser.Parse(optArgs, argc, argv); 00115 00116 // ------------------------------------------------------- 00117 // Sift through results, checking for semantic errors 00118 // ------------------------------------------------------- 00119 00120 // Special options that should be checked first 00121 if (parser.IsOpt("debug")) { 00122 debug = 1; 00123 if (parser.IsOptArg("debug")) { 00124 const string& arg = parser.GetOptArg("debug"); 00125 debug = (int)CmdLineParser::ToLong(arg); 00126 } 00127 } 00128 if (parser.IsOpt("help")) { 00129 PrintUsage(std::cerr); 00130 exit(1); 00131 } 00132 if (parser.IsOpt("version")) { 00133 PrintVersion(std::cerr); 00134 exit(1); 00135 } 00136 00137 // Check for mode 00138 if (parser.IsOpt("ir")) { runMode = 1; } 00139 if (parser.IsOpt("oa-ujnum")) { runMode = 2; } 00140 if (parser.IsOpt("whirl2f")) { runMode = 3; } 00141 00142 // Check for other options 00143 if (parser.IsOpt("dump")) { dumpIR = true; } 00144 00145 // Check for required arguments 00146 if (parser.GetNumArgs() != 1) { 00147 PrintError(std::cerr, "Invalid number of arguments!"); 00148 exit(1); 00149 } 00150 whirlFileNm = parser.GetArg(0); 00151 } 00152 catch (CmdLineParser::ParseError& e) { 00153 PrintError(std::cerr, e.GetMessage()); 00154 exit(1); 00155 } 00156 } 00157 00158 00159 void 00160 Args::Dump(std::ostream& os) const 00161 { 00162 parser.Dump(os); 00163 } 00164 00165 void 00166 Args::DDump() const 00167 { 00168 Dump(std::cerr); 00169 } 00170