Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:52

0001 #ifndef Utilities_Utilities_h
0002 #define Utilities_Utilities_h
0003 
0004 #include "CondCore/CondDB/interface/Exception.h"
0005 #include <boost/program_options.hpp>
0006 #include <sstream>
0007 #include <set>
0008 
0009 namespace edm {
0010   class ServiceToken;
0011 }
0012 
0013 namespace cond {
0014 
0015   std::string getpass(const std::string& prompt, bool show_asterisk = true);
0016   std::string getpassForUser(const std::string& userName);
0017 
0018   class UtilitiesError : public Exception {
0019   public:
0020     UtilitiesError(const std::string& message);
0021     ~UtilitiesError() throw() override;
0022   };
0023 
0024   class Utilities {
0025   public:
0026     Utilities(const std::string& commandName, std::string positionalParameter = std::string(""));
0027 
0028     virtual ~Utilities();
0029 
0030     virtual int execute();
0031     int run(int argc, char** argv);
0032 
0033     void addConnectOption(std::string const& fullName, std::string const& shortName, std::string const& helpEntry);
0034     void addAuthenticationOptions();
0035     void addConfigFileOption();
0036 
0037     template <typename T>
0038     void addOption(const std::string& fullName, const std::string& shortName, const std::string& helpEntry);
0039 
0040     void parseCommand(int argc, char** argv);
0041 
0042     std::string getAuthenticationPathValue();
0043     std::string getUserValue();
0044     std::string getPasswordValue();
0045     std::string getConnectValue();
0046     std::string getLogDBValue();
0047     std::string getDictionaryValue();
0048     std::string getConfigFileValue();
0049     template <typename T>
0050     T getOptionValue(const std::string& fullName);
0051     bool hasOptionValue(const std::string& fullName);
0052     bool hasDebug();
0053     void initializePluginManager();
0054     //cond::DbSession openDbSession( const std::string& connectionParameterName, bool readOnly=false );
0055     //cond::DbSession openDbSession( const std::string& connectionParameterName, const std::string& role, bool readOnly=false );
0056 
0057   protected:
0058     //cond::DbSession newDbSession(  const std::string& connectionString, bool readOnly=false );
0059     //cond::DbSession newDbSession(  const std::string& connectionString, const std::string& role, bool readOnly=false );
0060     //void initializeForDbConnection();
0061 
0062   private:
0063     std::string getValueIfExists(const std::string& fullName);
0064     void sendException(const std::string& message);
0065     void sendError(const std::string& message);
0066 
0067   protected:
0068     edm::ServiceToken* m_currentToken = nullptr;
0069 
0070   private:
0071     std::string m_name;
0072     //boost::program_options::options_description m_description;
0073     boost::program_options::options_description m_options;
0074     boost::program_options::positional_options_description m_positionalOptions;
0075     boost::program_options::variables_map m_values;
0076     //cond::DbConnection* m_dbConnection;
0077     //std::set<std::string> m_dbSessions;
0078   };
0079 
0080   template <typename T>
0081   inline void Utilities::addOption(const std::string& fullName,
0082                                    const std::string& shortName,
0083                                    const std::string& helpEntry) {
0084     std::stringstream optInfo;
0085     optInfo << fullName;
0086     if (!shortName.empty())
0087       optInfo << "," << shortName;
0088     m_options.add_options()(optInfo.str().c_str(), boost::program_options::value<T>(), helpEntry.c_str());
0089   }
0090 
0091   template <>
0092   inline void Utilities::addOption<bool>(const std::string& fullName,
0093                                          const std::string& shortName,
0094                                          const std::string& helpEntry) {
0095     std::stringstream optInfo;
0096     optInfo << fullName;
0097     if (!shortName.empty())
0098       optInfo << "," << shortName;
0099     m_options.add_options()(optInfo.str().c_str(), helpEntry.c_str());
0100   }
0101 
0102 }  // namespace cond
0103 
0104 template <typename T>
0105 inline T cond::Utilities::getOptionValue(const std::string& fullName) {
0106   const void* found = m_options.find_nothrow(fullName, false);
0107   if (!found) {
0108     std::stringstream message;
0109     message << "Utilities::getOptionValue: option \"" << fullName << "\" is not known by the command.";
0110     sendException(message.str());
0111   }
0112 
0113   if (!m_values.count(fullName)) {
0114     std::stringstream message;
0115     message << "Error: Option \"" << fullName << "\" has not been provided.";
0116     sendError(message.str());
0117   }
0118   return m_values[fullName].as<T>();
0119 }
0120 
0121 #endif