OpenADFortTk (basic)
src/sexp2whirl/Args.cxx
Go to the documentation of this file.
00001 // ##########################################################
00002 // # This file is part of OpenADFortTk.                     #
00003 // # The full COPYRIGHT notice can be found in the top      #
00004 // # level directory of the OpenADFortTk source tree.       #
00005 // # For more information visit                             #
00006 // # http://www.mcs.anl.gov/openad                          #
00007 // ##########################################################
00008 
00009 #include "Open64IRInterface/Open64BasicTypes.h"
00010 #include "file_util.h"      // New_Extension, Last_Pathname_Component
00011 
00012 #include "Args.h"
00013 
00014 using std::cerr;
00015 using std::endl;
00016 using std::string;
00017 
00018 
00019 static const char* version_info = "version .289";
00020 
00021 static const char* usage_summary =
00022 "[options] <whirl-sexp-file>\n";
00023 
00024 static const char* usage_details =
00025 "Given a WHIRL S-expression file, generates a WHIRL binary file. By\n"
00026 "default, output is sent to the filename formed by replacing the extension\n"
00027 "of <whirl-sexp-file> with 's2w.B'.\n"
00028 "\n"
00029 "Options:\n"
00030 "  -o, --output <file> send output to <file> instead of stdout\n"
00031 "  -V, --version       print version information\n"
00032 "  -h, --help          print this help\n"
00033 "      --debug [lvl]   debug mode at level `lvl'\n";
00034 
00035 
00036 #define CLP CmdLineParser
00037 
00038 CmdLineParser::OptArgDesc Args::optArgs[] = {
00039   // Options
00040   { 'o', "output",   CLP::ARG_REQ , CLP::DUPOPT_ERR,  NULL },
00041   { 'V', "version",  CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00042   { 'h', "help",     CLP::ARG_NONE, CLP::DUPOPT_CLOB, NULL },
00043   {  0 , "debug",    CLP::ARG_OPT,  CLP::DUPOPT_CLOB, NULL },
00044   CmdLineParser::OptArgDesc_NULL
00045 };
00046 
00047 #undef CLP
00048 
00049 
00050 Args::Args()
00051 {
00052   Ctor();
00053 }
00054 
00055 Args::Args(int argc, const char* const argv[])
00056 {
00057   Ctor();
00058   Parse(argc, argv);
00059 }
00060 
00061 void
00062 Args::Ctor()
00063 {
00064   debug = 0;      // default: 0 (off)
00065 }
00066 
00067 Args::~Args()
00068 {
00069 }
00070 
00071 
00072 void 
00073 Args::PrintVersion(std::ostream& os) const
00074 {
00075   os << GetCmd() << ": " << version_info << endl;
00076 }
00077 
00078 
00079 void 
00080 Args::PrintUsage(std::ostream& os) const
00081 {
00082   os << "Usage: " << GetCmd() << " " << usage_summary << endl
00083      << usage_details << endl;
00084 } 
00085 
00086 
00087 void 
00088 Args::PrintError(std::ostream& os, const char* msg) const
00089 {
00090   os << GetCmd() << ": " << msg << endl
00091      << "Try `" << GetCmd() << " --help' for more information." << endl;
00092 }
00093 
00094 void 
00095 Args::PrintError(std::ostream& os, const std::string& msg) const
00096 {
00097   PrintError(os, msg.c_str());
00098 }
00099 
00100 
00101 void
00102 Args::Parse(int argc, const char* const argv[])
00103 {
00104   try {
00105     // -------------------------------------------------------
00106     // Parse the command line
00107     // -------------------------------------------------------
00108     parser.Parse(optArgs, argc, argv);
00109     
00110     // -------------------------------------------------------
00111     // Sift through results, checking for semantic errors
00112     // -------------------------------------------------------
00113     
00114     // Special options that should be checked first
00115     if (parser.IsOpt("debug")) { 
00116       debug = 1; 
00117       if (parser.IsOptArg("debug")) {
00118         const string& arg = parser.GetOptArg("debug");
00119         debug = (int)CmdLineParser::ToLong(arg);
00120       }
00121     }
00122     if (parser.IsOpt("help")) { 
00123       PrintUsage(std::cerr); 
00124       exit(1);
00125     }
00126     if (parser.IsOpt("version")) { 
00127       PrintVersion(std::cerr);
00128       exit(1);
00129     }
00130     
00131     // Check for other options
00132     if (parser.IsOpt("output")) { 
00133       whirlFileNm = parser.GetOptArg("output");
00134     }
00135     
00136     // Check for required arguments
00137     if (parser.GetNumArgs() != 1) {
00138       PrintError(std::cerr, "Invalid number of arguments!");
00139       exit(1);
00140     }
00141     sexpFileNm = parser.GetArg(0);
00142   } 
00143   catch (CmdLineParser::ParseError& e) {
00144     PrintError(std::cerr, e.GetMessage());
00145     exit(1);
00146   }
00147   
00148   // -------------------------------------------------------
00149   // Postprocess
00150   // -------------------------------------------------------
00151   if (whirlFileNm.empty()) {
00152     // FIXME: should we place in current directory?
00153     whirlFileNm = New_Extension(sexpFileNm.c_str(), ".s2w.B");
00154   }
00155 }
00156 
00157 
00158 void 
00159 Args::Dump(std::ostream& os) const
00160 {
00161   parser.Dump(os);
00162 }
00163 
00164 void 
00165 Args::DDump() const
00166 {
00167   Dump(std::cerr);
00168 }
00169 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines