OpenADFortTk (basic)
src/whirl2sexp/Args.cxx
Go to the documentation of this file.
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 static const char* version_info = "version .289";
00014 
00015 static const char* usage_summary =
00016 "[options] <whirl-file>\n";
00017 
00018 static const char* usage_details =
00019 "Given a WHIRL file, generates S-expressions representing the IR.  By\n"
00020 "default, output is sent to stdout.\n"
00021 "\n"
00022 "Options:\n"
00023 "  -o, --output <file> send output to <file> instead of stdout\n"
00024 "  -V, --version       print version information\n"
00025 "  -h, --help          print this help\n"
00026 "  -n, --noCleanUp     only for development: do not perform whirl cleanup \n"
00027 "                      needed for OpenAD\n"
00028 "      --debug [lvl]   only for development: debug mode at level `lvl'\n";
00029 
00030 
00031 #define CLP CmdLineParser
00032 
00033 CmdLineParser::OptArgDesc Args::optArgs[] = {
00034   // Options
00035   { 'o', "output",   CLP::ARG_REQ , CLP::DUPOPT_ERR,  NULL },
00036   { 'V', "version",  CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00037   { 'n', "noCleanUp",CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00038   { 'h', "help",     CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00039   {  0 , "debug",    CLP::ARG_OPT,  CLP::DUPOPT_CLOB, NULL },
00040   CmdLineParser::OptArgDesc_NULL
00041 };
00042 
00043 #undef CLP
00044 
00045 //***************************************************************************
00046 // Args
00047 //***************************************************************************
00048 
00049 Args::Args()  : debug(0), myNoCleanUpFlag(false) {
00050 }
00051 
00052 Args::Args(int argc, const char* const argv[]) : debug(0), myNoCleanUpFlag(false) {
00053   Parse(argc, argv);
00054 }
00055 
00056 Args::~Args()
00057 {
00058 }
00059 
00060 
00061 void 
00062 Args::PrintVersion(std::ostream& os) const
00063 {
00064   os << GetCmd() << ": " << version_info << endl;
00065 }
00066 
00067 
00068 void 
00069 Args::PrintUsage(std::ostream& os) const
00070 {
00071   os << "Usage: " << GetCmd() << " " << usage_summary << endl
00072      << usage_details << endl;
00073 } 
00074 
00075 
00076 void 
00077 Args::PrintError(std::ostream& os, const char* msg) const
00078 {
00079   os << GetCmd() << ": " << msg << endl
00080      << "Try `" << GetCmd() << " --help' for more information." << endl;
00081 }
00082 
00083 void 
00084 Args::PrintError(std::ostream& os, const std::string& msg) const
00085 {
00086   PrintError(os, msg.c_str());
00087 }
00088 
00089 
00090 void
00091 Args::Parse(int argc, const char* const argv[])
00092 {
00093   try {
00094     // -------------------------------------------------------
00095     // Parse the command line
00096     // -------------------------------------------------------
00097     parser.Parse(optArgs, argc, argv);
00098     
00099     // -------------------------------------------------------
00100     // Sift through results, checking for semantic errors
00101     // -------------------------------------------------------
00102     
00103     // Special options that should be checked first
00104     if (parser.IsOpt("debug")) { 
00105       debug = 1; 
00106       if (parser.IsOptArg("debug")) {
00107         const string& arg = parser.GetOptArg("debug");
00108         debug = (int)CmdLineParser::ToLong(arg);
00109       }
00110     }
00111     if (parser.IsOpt("help")) { 
00112       PrintUsage(std::cerr); 
00113       exit(1);
00114     }
00115     if (parser.IsOpt("version")) { 
00116       PrintVersion(std::cerr);
00117       exit(1);
00118     }
00119     
00120     // Check for other options
00121     if (parser.IsOpt("output")) { 
00122       sexpFileNm = parser.GetOptArg("output");
00123     }
00124     
00125     if (parser.IsOpt("noCleanUp")) { 
00126       myNoCleanUpFlag = true;
00127     }
00128 
00129     // Check for required arguments
00130     if (parser.GetNumArgs() != 1) {
00131       PrintError(std::cerr, "Invalid number of arguments!");
00132       exit(1);
00133     }
00134     whirlFileNm = parser.GetArg(0);
00135   } 
00136   catch (CmdLineParser::ParseError& e) {
00137     PrintError(std::cerr, e.GetMessage());
00138     exit(1);
00139   }
00140 }
00141 
00142 
00143 void 
00144 Args::Dump(std::ostream& os) const
00145 {
00146   parser.Dump(os);
00147 }
00148 
00149 void 
00150 Args::DDump() const
00151 {
00152   Dump(std::cerr);
00153 }
00154 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines