Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgCmdLineOpt.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgCmdLineOpt.cpp
00003     See SgCmdLineOpt.h */
00004 //----------------------------------------------------------------------------
00005 
00006 #include "SgSystem.h"
00007 #include "SgCmdLineOpt.h"
00008 
00009 #include <algorithm>
00010 #include <sstream>
00011 #include "SgDebug.h"
00012 #include "SgException.h"
00013 
00014 using namespace std;
00015 
00016 //----------------------------------------------------------------------------
00017 
00018 SgCmdLineOpt::SgCmdLineOpt()
00019 {
00020 }
00021 
00022 bool SgCmdLineOpt::Contains(const char* option) const
00023 {
00024     return (m_map.find(option) != m_map.end());
00025 }
00026 
00027 const vector<string>& SgCmdLineOpt::GetArguments() const
00028 {
00029     return m_args;
00030 }
00031 
00032 double SgCmdLineOpt::GetDouble(const char* option) const
00033 {
00034     return GetDouble(option, 0.0);
00035 }
00036 
00037 double SgCmdLineOpt::GetDouble(const char* option, double defaultValue) const
00038 {
00039     map<string, string>::const_iterator it = m_map.find(option);
00040     if (it == m_map.end())
00041         return defaultValue;
00042     string s = it->second;
00043     istringstream in(s);
00044     double value;
00045     in >> value;
00046     if (! in)
00047         throw SgException(string("Option ") + option + " needs float value");
00048     return value;
00049 }
00050 
00051 
00052 int SgCmdLineOpt::GetInteger(const char* option) const
00053 {
00054     return GetInteger(option, 0);
00055 }
00056 
00057 int SgCmdLineOpt::GetInteger(const char* option, int defaultValue) const
00058 {
00059     map<string, string>::const_iterator it = m_map.find(option);
00060     if (it == m_map.end())
00061         return defaultValue;
00062     string s = it->second;
00063     istringstream in(s);
00064     int value;
00065     in >> value;
00066     if (! in)
00067         throw SgException(string("Option ") + option
00068                           + " needs integer value");
00069     return value;
00070 }
00071 
00072 string SgCmdLineOpt::GetString(const char* option) const
00073 {
00074     return GetString(option, "");
00075 }
00076 
00077 string SgCmdLineOpt::GetString(const char* option,
00078                                const string& defaultValue) const
00079 {
00080     map<string, string>::const_iterator it = m_map.find(option);
00081     if (it == m_map.end())
00082         return defaultValue;
00083     return it->second;
00084 }
00085 
00086 void SgCmdLineOpt::Parse(int argc, char* argv[],
00087                          const vector<std::string>& specs)
00088 {
00089     m_args.clear();
00090     m_map.clear();
00091     int n = 1;
00092     bool endOfOptions = false;
00093     while (n < argc)
00094     {
00095         string s = argv[n];
00096         ++n;
00097         if (! endOfOptions && s.size() > 0 && s[0] == '-')
00098         {
00099             if (s == "--")
00100             {
00101                 endOfOptions = true;
00102                 continue;
00103             }
00104             s = s.substr(1);
00105             bool needsArg = false;
00106             vector<string>::const_iterator spec;
00107             spec = find(specs.begin(), specs.end(), s);
00108             if (spec == specs.end())
00109             {
00110                 spec = find(specs.begin(), specs.end(), s + ":");
00111                 if (spec == specs.end())
00112                     throw SgException("Unknown option -" + s);
00113                 needsArg = true;
00114             }
00115             string value;
00116             if (needsArg)
00117             {
00118                 if (n >= argc)
00119                     throw SgException("Option -" + s + " needs value");
00120                 value = argv[n];
00121                 ++n;
00122                 if (value.size() > 0 && value[0] == '-')
00123                     throw SgException("Option -" + s + " needs value");
00124             }
00125             m_map.insert(pair<string, string>(s, value));
00126         }
00127         else
00128             m_args.push_back(s);
00129     }
00130 }
00131 
00132 void SgCmdLineOpt::Parse(int argc, const char* argv[],
00133                          const vector<std::string>& specs)
00134 {
00135     Parse(argc, const_cast<char**>(argv), specs);
00136 }
00137 
00138 //----------------------------------------------------------------------------
00139 


Sun Mar 13 2011 Doxygen 1.7.1